Skip to content

我的方法数没有超过65535,但是我想要分包,请问怎么操作? #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Armyoftheworld opened this issue Nov 8, 2016 · 10 comments
Labels

Comments

@Armyoftheworld
Copy link


build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'dexknifePlus'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "com.juziwl.ixiao"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"

    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}
dexKnife{
//必选参数
enabled true //if false,禁用分包插件
//可选参数
//1.如果没有可选参数,将根据enabled决定是否分包。
//2.如果有可选参数,需满足必选参数和可选参数的条件才允许分包
productFlavor 'mock'
buildType 'debug'

/*
*eg:当前productFlavors = dev,buildType = debug,
*参数组合1:enabled = true,productFlavor = dev,buildType = debug 分包
*参数组合2:enabled = true,productFlavor = mock,buildType = debug 不分包
*参数组合1:enabled = true,buildType = debug 所有buildType = debug分包
*参数组合1:enabled = true,productFlavor = dev 所有productFlavor = dev分包
* */

}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'io.reactivex:rxjava:1.1.5'
compile 'io.reactivex:rxandroid:1.2.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.google.code.gson:gson:2.7'
compile 'com.hwangjr.rxbus:rxbus:1.0.4'
compile 'com.jakewharton:butterknife:8.2.1'
apt 'com.jakewharton:butterknife-compiler:8.2.1'
compile 'com.github.Aspsine:SwipeToLoadLayout:1.0.3'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
compile 'com.android.support:multidex:1.0.1'
}

dexknife.txt
#为注释符

#-----------主Dex中必要依赖的脚本配置-----------
#默认保留四大组件中其他三大组件(并计算其依赖树),Activity组件选择性保留(使用-just activity 选项),若为空不保留任何Activity
-just activity com.juziwl.ixiao.main.MainActivity

#-----------附加类-----------

如果你想要某个包路径在maindex中,则使用 -keep 选项,即使他已经在分包的路径中.若为空,不保留任意类

#-keep com.ceabie.demo.**

保留单个类.

#-keep android.support.v7.app.AppCompatDialogFragment.class

这条配置可以指定这个包下类在第二及其他dex中.

#-split android.support.v?.**
#将全部类移出主Dex
-split .

不包含Android gradle 插件自动生成的miandex列表.(不使用建议的依赖树,注释掉表示使用,否则-just activity无效)

#-donot-use-suggest

(分割每个dex的方法数上限) 扩展参数:例如 --set-max-idx-number=50000

如果出现 DexException: Too many classes in --main-dex-list, main dex capacity exceeded:

表明限制的方法数小于main dex的必要方法数,调大到合适数值即可

-dex-param --set-max-idx-number=3000

不进行dex分包, 直到 dex 的id数量超过 65536.(设置自动执行分包策略)

#-auto-maindex

显示miandex的日志.

#-log-mainlist

打包的时候还报了一些错,但是打包成功了
Information:Gradle tasks [:emojitest:assembleDebug, :ixiao:assembleDebug]
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.squareup.haha.guava.base.Joiner$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is not an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.squareup.haha.guava.collect.Iterables$2) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is not an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.squareup.haha.guava.collect.Iterables$3) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is not an inner class.
Information:BUILD SUCCESSFUL
Information:Total time: 43.909 secs
Information:24 errors
Information:0 warnings
Information:See complete output in console

我生成class.dex文件有27928个方法,我现在就想分包,请问怎么写?还有上面的问题是什么原因造成的?

@TangXiaoLv
Copy link
Owner

@Armyoftheworld
1.这个错是编译环境造成的,并非打包。
2.合理设置-dex-param --set-max-idx-number=4000(每个dex最大方法数,这个值必须>main dex必须的方法数)
3.如果studio是2.0+ 确保把instant run关掉,否则打debug包会默认禁用分包功能,由于instant run的机制自动做了增量分包

@Armyoftheworld
Copy link
Author

qq 20161109091851
请问从哪里知道main dex必须的方法数?因为我不知道那些类会在main dex中?
我已经把instant run关掉了,还是没有分包。

@Armyoftheworld
Copy link
Author

还有我想问一下,分包的话是不是默认会混淆,因为我这边一打包就会报混淆时出的错误,我已经在release里面把minifyEnabled设为false了

@TangXiaoLv
Copy link
Owner

@Armyoftheworld 默认不会混淆,混淆不影响分包

@Armyoftheworld
Copy link
Author

@TangXiaoLv 关于还是不能分包的问题,请问通常是怎么判断main dex需要的方法数?

@TangXiaoLv
Copy link
Owner

首次默认运行查看main dex 方法数,然后根据方法数向上取整得出每个dex的方法树,通常默认就好了,只限制main dex的,其他的分包超过65535再自动分。

@Armyoftheworld
Copy link
Author

qq 20161109173753
我想知道为什么你的demo都没有分包?是我这边的问题吗?我的android studio是2.2的,gradle版本是2.2的,instant run已经关了。

@TangXiaoLv
Copy link
Owner

@Armyoftheworld 我本地测试OK的 Gradle version 2.14.4 .Plugin Version 2.2.2

@ibrothergang
Copy link

@Armyoftheworld demo后来能分包吗?我这边始终有问题。

@Armyoftheworld
Copy link
Author

@wanggang1984117 不能,我也没有继续弄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants