Android Studio multidex分包笔记

一.为什么要分包?
当方法数超过64k(65536)的上限时,超出缓冲区大小,爆棚。(具体原因再研究,占位)

二:解决办法。(之一)
1.使用android.support.multidex.jar.
2.build.gradle配置

android {

    defaultConfig {
        //...
        multiDexEnabled true;
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

3.配置Application

public class MultidexTestApplication extends MultiDexApplication {

}

三:问题
1.Error:Execution failed for task ‘:multidextest:transformClassesWithJarMergingForDebug’.
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: android/support/multidex/MultiDex$V14.class
这个是jar包冲突引起的,编译版本自带了分包MultiDex jar包,去掉上面引入的jar即可。

四:工具
1.生成这么多方法,我是使用一个简单的Java程序弄的

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MyClass {

    public static void main(String arg[]) {
        String fileDirPath = "D:\\Android\\as\\workspace\\TestJava\\java";
        String javaClassStrStart = "package com.kongge.multidextest.pack%s;\n" +
                "public class TestClass%s {\n";
        String javaClassStrEnd = "    \n" +
                "}";
        String content = "\n\tpublic void mothed%s(int a) {}\n";

        File fileDir = new File(fileDirPath);
        if (fileDir.exists() == false) {
            fileDir.mkdirs();
        }

        for (int i = 0; i < 10; i++) {
                String packageDirPath = fileDirPath + File.separator + "pack" + i;
                File fileDirPackage = new File(packageDirPath);
                if (fileDirPackage.exists() == false) {
                    fileDirPackage.mkdirs();
                }

                for (int k = 0; k < 100; k++) {
                    FileOutputStream fileOutputStream = null;

                    try {
                        File file = new File(packageDirPath + File.separator + "TestClass" + k + ".java");
                        if (file.exists()) {
                            file.delete();
                        } else {
                            file.createNewFile();
                        }

                        String javaClassStrStartTemp = String.format(javaClassStrStart, i, k);
                        fileOutputStream = new FileOutputStream(file);
                        fileOutputStream.write(javaClassStrStartTemp.getBytes());
                        for (int j = 0; j < 100; j++){
                            String itemString = String.format(content, j);
                            fileOutputStream.write(itemString.getBytes());
                        }
                        fileOutputStream.write(javaClassStrEnd.getBytes());

                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (fileOutputStream != null) {
                            try {
                                fileOutputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            fileOutputStream = null;
                        }
                    }
                }

            }


        }
}

保存成MyClass.java文件,直接使用命令行编译运行,保存地址和包名自行修改
javac MyClass.java
java MyClass

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值