类解决了意外的DEX,如何避免重复导入 [英] Class resolved by unexpected DEX, how to avoid duplicate imports

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

问题描述

我正在尝试构建一个插件系统,该插件系统是根据此处所述从其他apails导入片段。

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

Eclipse不断告诉我:

Eclipse keeps telling me:


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

12-01 15:17:18.609: W/dalvikvm(23425): Class resolved by unexpected 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;验证)

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

这不是一个惊喜,因为我正在导入我的PluginAPI-libary在主机和插件。据我所知,Eclipse似乎害怕,这些库存不一样。有没有办法告诉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;
        }

}

上述每个都是自己的Project(PluginAPI as libary)。这个问题不是很新的(例如这里),但是已经给出的答案建议删除导入,什么在我的情况下似乎不是一个选择,因为我需要双方的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,有时是很明显的。只需将属性 - > Java构建路径 - >添加外部jar插件的外部库中的libary作为extern libary导入,并且一切正常。 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,如何避免重复导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆