由意外DEX解决的类,如何避免重复导入

时间:2014-12-01 14:44:01

标签: java android eclipse plugins dex

我正在尝试构建一个插件系统,它正如here所描述的那样从其他apks导入Fragments。

Eclipse一直告诉我:

  

12-01 15:17:18.609:W / dalvikvm(23425):由意外DEX解决的类:Lde / anthropotec / activityapp / firstplugin / UI;(0x42901520):0x57d93000 ref [Lde / anthropotec / activityapp / api / PluginAPI;] Lde / anthropotec / activityapp / api / PluginAPI;(0x428eb2b8):0x527e1000

     

(Lde / anthropotec / activityapp / firstplugin / UI;使用了不同的Lde / anthropotec / activityapp / api / PluginAPI;在预验证期间)

这不是一个惊喜,因为我在Host和Plugin中导入了我的PluginAPI-libary。据我所知,Eclipse似乎害怕,这些库不相同。有没有办法告诉Eclipse不要导入libary,如果它已经存在于另一个apk中?或者还有其他方法来解决这个问题。如果您需要更多信息,请告诉我。这是我的来源:

主持人:

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:

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)。问题不是很新(例如here),但已经给出的答案建议删除导入,在我的情况下似乎不是一个选项,因为我需要双方的API (插件和主机)。

1 个答案:

答案 0 :(得分:0)

啊,有时它太明显了。只需在Properties-&gt; Java Build Path - &gt;中导入libary作为extern libary。为插件添加外部jar,一切正常。 Gnarf!