eclipse 解决方法数超过65535的问题 方案二

参考 https://github.com/mmin18/Dex65536 中利用ant打包apk来解决方法数超限android

上述方法中的本质是将能够分包的jar打包为一个apk文件,放在assets文件夹下,在应用初始化时加载此apk文件git

下面是具体操做步骤:github

 一、将能够分到第二个包的jar文件利用ant合并合并为一个jar包,而后转换为dex文件,命名为classes.dex,添加为压缩文                 件, 并命名为libs.apkide

二、将libs.apk放入项目的assets文件夹下ui

3.将步骤1中的jar包从项目libs文件夹下删除,将合并后的jar包依照android.jar的方式引入项目code

        项目--右键--Build Path --Configure Build Path --- Libraries ---Add External JARsget

四、在项目的Application中添加下面的方法it

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        dexTool();
    }

    /**
     * Copy the following code and call dexTool() after super.onCreate() in
     * Application.onCreate()
     * <p>
     * This method hacks the default PathClassLoader and load the secondary dex
     * file as it's parent.
     */
    @SuppressLint("NewApi")
    private void dexTool() {

        File dexDir = new File(getFilesDir(), "dlibs");
        dexDir.mkdir();
        File dexFile = new File(dexDir, "libs.apk");
        File dexOpt = new File(dexDir, "opt");
        dexOpt.mkdir();
        try {
            InputStream ins = getAssets().open("libs.apk");
            if (dexFile.length() != ins.available()) {
                FileOutputStream fos = new FileOutputStream(dexFile);
                byte[] buf = new byte[4096];
                int l;
                while ((l = ins.read(buf)) != -1) {
                    fos.write(buf, 0, l);
                }
                fos.close();
            }
            ins.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        ClassLoader cl = getClassLoader();
        ApplicationInfo ai = getApplicationInfo();
        String nativeLibraryDir = null;
        if (Build.VERSION.SDK_INT > 8) {
            nativeLibraryDir = ai.nativeLibraryDir;
        } else {
            nativeLibraryDir = "/data/data/" + ai.packageName + "/lib/";
        }
        DexClassLoader dcl = new DexClassLoader(dexFile.getAbsolutePath(),
                dexOpt.getAbsolutePath(), nativeLibraryDir, cl.getParent());

        try {
            Field f = ClassLoader.class.getDeclaredField("parent");
            f.setAccessible(true);
            f.set(cl, dcl);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

        注:Application 中的静态全局变量会比MutiDex instal()方法优先加载,因此建议避免在 Application类中使用引用classes2.dex文件的静态变量io


到此,问题解决。
class

相关文章
相关标签/搜索