Gradle1.5.0之后如何控制dex包内的方法数上限?

最近项目方法数量超过6w了,该考虑分包的实现了。
参考各位大神的文章,结合目前项目的情况,最后决定按照FB的思路来实现分包策略。具体的不在这里详述。
参考文章地址:

Android Dex分包之旅–鱼动动是一只码农BBB

dex 分包变形记–李金涛

其实你不知道MultiDex到底有多坑–总悟君

分包的问题解决之后,在通过控制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) {
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值