最近项目方法数量超过6w了,该考虑分包的实现了。
参考各位大神的文章,结合目前项目的情况,最后决定按照FB的思路来实现分包策略。具体的不在这里详述。
参考文章地址:
分包的问题解决之后,在通过控制dex包内方法数量来解决LinearAlloc4M缓存问题的时候遇到了困难。
之前大家的思路都是在dextask执行的时候通过传入参数来实现,代码如下:
afterEvaluate {
tasks.matching {
it.name.startsWith('dex')
}.each { dx ->
if (dx.additionalParameters == null) {
dx.additionalParameters = []
}
dx.additionalParameters += '--multi-dex'
dx.additionalParameters += '--set-max-idx-number=48000'
}
但在我们项目里报以下错误:
Error:(32, 0) Access to the dex task is now impossible, starting with 1.4.0
1.4.0 introduces a new Transform API allowing manipulation of the .class files.
See more information: http://tools.android.com/tech-docs/new-build-system/transform-api
Open File
后来通过查询gradle-build system的1.5.0-beta1的changelog发现了以下的信息:
Starting with 1.5.0-beta1, the Gradle plugin includes a Transform
API allowing 3rd party plugins to manipulate compiled class files
before they are converted to dex files. (The API existed in
1.4.0-beta2 but it’s been completely revamped in 1.5.0-beta1)The goal of this API is to simplify injecting custom class
manipulations without having to deal with tasks, and to offer more
flexibility on what is manipulated. The internal code processing
(jacoco, progard, multi-dex) have all moved to this new mechanism
already in 1.5.0-beta1. Note: this applies only to the javac/dx code
path. Jack does not use this API at the moment.The API doc is here.
To insert a transform into a build, you simply create a new class
implementing one of the Transform interfaces, and register it with
android.registerTransform(theTransform) or
android.registerTransform(theTransform, dependencies).Important notes: The Dex class is gone. You cannot access it anymore
through the variant API (the getter is still there for now but will
throw an exception) Transform can only be registered globally which
applies them to all the variants. We’ll improve this shortly. There’s
no way to control ordering of the transforms. We’re looking for
feedback on the API. Please file bugs or email us on our adt-dev
mailing list.
链接:http://tools.android.com/tech-docs/new-build-system/transform-api
简单来说就是gradle官方从1.5.0-beta1这个版本开始修改了关于分包参数的API,不再允许通过dex task传递参数的方式来控制dex内方法的数量,也无法指定主dex内保存的类了。那这个版本上有没有替代的方法呢。。通过查看gradle源码可以看到:
DexProcessBuilder内的build方法:
@NonNull
public JavaProcessInfo build(
@NonNull BuildToolInfo buildToolInfo,
@NonNull DexOptions dexOptions) throws ProcessException {
checkState(
!mMultiDex
|| buildToolInfo.getRevision().compareTo(MIN_MULTIDEX_BUILD_TOOLS_REV) >= 0,
"Multi dex requires Build Tools " +
MIN_MULTIDEX_BUILD_TOOLS_REV.toString() +
" / Current: " +
buildToolInfo.getRevision().toShortString());
ProcessInfoBuilder builder = new ProcessInfoBuilder();
builder.addEnvironments(mEnvironment);
String dx = buildToolInfo.getPath(BuildToolInfo.PathId.DX_JAR);
if (dx == null || !new File(dx).isFile()) {
throw new IllegalStateException("dx.jar is missing");
}
builder.setClasspath(dx);
builder.setMain("com.android.dx.command.Main");
if (dexOptions.getJavaMaxHeapSize() != null) {