如何避免重复进口

如何避免重复进口

本文介绍了由意外DEX决定的类,如何避免重复进口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图构建一个从其他的APK这里描述导入片段的插件系统。

I'm trying to build an plugin-system which is importing Fragments from other apks as described here.

Eclipse中不断告诉我:

Eclipse keeps telling me:

15 12-01:17:18.609:W / dalvikvm(23425):通过类DEX意外解决:LDE / anthropotec / activityapp / firstplugin / UI(0x42901520):0x57d93000文献[LDE / anthropotec / activityapp / API / PluginAPI] LDE / anthropotec / activityapp / API / PluginAPI;(0x428eb2b8):0x527e1000

(LDE / anthropotec / activityapp / firstplugin / UI;使用不同的LDE / anthropotec / activityapp / API / PluginAPI; pre-验证期间)

(Lde/anthropotec/activityapp/firstplugin/UI; had used a different Lde/anthropotec/activityapp/api/PluginAPI; during pre-verification)

这是不是一个惊喜,因为我进口我PluginAPI-libary两个,主机和插件。据我了解,Eclipse的似乎是害怕,这些libaries是不相同的。有没有一种方法来告诉Eclipse无法导入libary,如果它已经存在于其他APK?或者有没有其他的办法去解决这个问题。如果您需要了解更多信息,请告诉我。下面是我的源:

Which isn't a suprise, as I'm importing my PluginAPI-libary in both, the Host and the Plugin. As far as I understand, Eclipse seems to be afraid, that these libaries aren't identical. Is there a way to tell Eclipse to not import the libary, if it's already there in the other apk? Or is there any other way to go around this. Please tell me if you need more information. Here's my source:

主持人:

package de.anthropotec.activityapp.host;

import java.io.File;

import dalvik.system.DexClassLoader;
import de.anthropotec.activityapp.api.PluginAPI;
import de.anthropotec.activtiyapp.host.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;

public class MainActivtiy extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    try {
        Class<?> requiredClass = null;
        final ApplicationInfo info = getPackageManager().getApplicationInfo("de.anthropotec.activityapp.firstplugin",0);
        final String apkPath = info.sourceDir;
        final File dexTemp = getDir("temp_folder", 0);
        final String fullName = "de.anthropotec.activityapp.firstplugin.UI";
        boolean isLoaded = true;

        // Check if class loaded
        try {
            requiredClass = Class.forName(fullName);
        } catch(ClassNotFoundException e) {
            isLoaded = false;
        }

        if (!isLoaded) {
            final DexClassLoader classLoader = new DexClassLoader(apkPath, dexTemp.getAbsolutePath(), null, getApplicationContext().getClassLoader());
            requiredClass = classLoader.loadClass(fullName);
        }

        if (null != requiredClass) {
            // Try to cast to required interface to ensure that it's can be cast
            final PluginAPI holder = PluginAPI.class.cast(requiredClass.newInstance());

            if (null != holder) {
                final Fragment fragment = holder.getFragment();

                if (null != fragment) {
                    final FragmentTransaction trans = getFragmentManager().beginTransaction();

                    trans.add(R.id.pluginPlace, fragment, "MyFragment").commit();
                }
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    }
}

该PluginApi:

The PluginApi:

package de.anthropotec.activityapp.api;

import android.app.Fragment;


public interface PluginAPI {

    public Fragment getFragment();
}

和插件片段本身:

package de.anthropotec.activityapp.firstplugin;

import de.anthropotec.activityapp.api.PluginAPI;
import android.app.Fragment;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;

public class UI extends Fragment implements PluginAPI{

     @Override
        public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
            // Note that loading of resources is not the same as usual, because it loaded actually from another apk
            final XmlResourceParser parser = container.getContext().getPackageManager().getXml("de.anthropotec.testplugin", R.layout.ui, null);
            return inflater.inflate(parser, container, false);
        }

        @Override
        public Fragment getFragment() {
            return this;
        }

}

每个以上的,因为它是自己的项目(如PluginAPI libary)。现在的问题是不是很新的(如 href=\"http://stackoverflow.com/q/22193668/3960095\">这里),但已经给答案,建议对进口删除,什么似乎并不在我的情况下的选择,因为我需要在两侧(插件和主机)的API。

Each of the above as it's own Project (PluginAPI as libary). The question isn't quite new (e.g. here), but the already given answers advice to remove on of the imports, what doesn't seem to be a option in my case, as I need the API on both sides (Plugin and Host).

推荐答案

Ahww,有时它是那么明显。只需导入libary在属性 - 的extern libary> Java构建路径 - >添加外部罐子的插件,一切工作得很好。 Gnarf!

Ahww, sometimes it's so obvious. Just import the libary as extern libary in Properties->Java Build Path -> add external jar for the Plugin, and everything works just fine. Gnarf!

这篇关于由意外DEX决定的类,如何避免重复进口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!