Skip to content

Multidex Configuration Guide

Jason Safaiyeh edited this page Oct 18, 2016 · 9 revisions

There are 3 steps to enable it:

1 . enable multidex in your app/build.gradle:

android {
    ...

    defaultConfig {
        ...
        multiDexEnabled true
        ...
    }
}

dependencies {
    compile 'com.android.support:multidex:1.0.1'
    ...
}

2 . set OkBuck multidex related configuration in your root build.gradle:

NOTE THAT below are example, do not copy these into your configuration! See more at below.

okbuck {
    ...
    linearAllocHardLimit = [
            app: 7194304
    ]
    primaryDexPatterns = [
            app: [
                    '^com/github/piasy/okbuck/example/AppShell^',
                    '^com/github/piasy/okbuck/example/BuildConfig^',
                    '^android/support/multidex/',
                    '^com/facebook/buck/android/support/exopackage/',
                    '^com/github/promeg/xlog_android/lib/XLogConfig^',
                    '^com/squareup/leakcanary/LeakCanary^',
            ]
    ]
    ...
}

3 . linearAllocHardLimit and primaryDexPatterns values should be determined by experiment.

这俩参数目前需要通过实验确定。

The start value could be 65535 and your Application class full classpath, then run your ./buckw install command, (e.g. ./buckw install -r app).

If buck failed with error like:

DexWithClasses buck-out/gen/.okbuck/app/aar__design-23.1.1.aar#aar_prebuilt_jar,
dex.dex.jar with cost 514808 exceeds the max cost 65535 for a secondary dex file.

then you should set linearAllocHardLimit an int value greater than 514808 (the number given by buck), usually you can set a big value like 10000000.

linearAllocHardLimit 可以首先设置一个很大的数字,例如10000000。使用OkBuck生成BUCK配置之后,执行./buckw的install/build命令,如果看到上述提示,就把这个参数改得大于提示的数字即可。

Then run your app, if it crashes with error like:

java.lang.RuntimeException: Unable to instantiate application com.github.piasy.okbuck.example.MyApp:
 java.lang.ClassNotFoundException: Didn't find class "com.github.piasy.okbuck.example.MyApp" on path: 
DexPathList[[zip file "/data/app/com.github.piasy.okbuck.example-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.github.piasy.okbuck.example-2, /vendor/lib, /system/lib]]

Then you should add the missing class path into the primaryDexPatterns list, in format of:

primaryDexPatterns可以先是一个空的数组,如果没有遇到上面的数字错误,或者已经解决了数字错误,安装APP之后运行,如果有上述找不到类的错误,就把相应的类加入到这个参数数组中,对应规则如下:

com.github.piasy.okbuck.example.MyApp ==> ^com/github/piasy/okbuck/example/MyApp^

Clone this wiki locally