Android 升级gradle 插件到 3.0.+

前言

新事物出现,肯定会带来一些新的优化,android studio 3.0 +版本带来了新功能同时,对配套插件进行了升级。本文旨在解决升级构成中遇到的问题。

1,如果build.gradle中配置了productFlavors 那么首先会遇到一个error。

这里写图片描述
图片1

解决方法就是需要定义:

flavorDimensions “apple”,

然后针对不同flavors制定以下 dimension “xxxx”

tips:具体规则,如图

这里写图片描述
图二

tip建议定义一种 flavorDimensions,当然不排除后期会有定义多种维度的需求

这里写图片描述
图三


      flavorDimensions "apple"

    productFlavors {
        qq {
            dimension "apple"

        }
        oppo {
            dimension "apple"
        }
        xiaomi {
            dimension "apple"
        }
    }
}

flavorDimensions 后面名字按照自己喜好定义。

2.自定义apk输出名称

之前自定义输出apk文件名字,在gradle 3.0后不好使了。无法编译通过

原因是之前的api官方做了收敛,不让直接调用了,如下是方式可以达到之前的效果

   applicationVariants.all { variant ->
        variant.outputs.all {
            def newName
            newName = 'npl‘ + '-' + variant.buildType.name + "-" + '.apk'
            outputFileName = newName
        }
    }
3.module新的引用方式

如果主工程有多个Modue引用,有采用如下方式进行引用module的,也会报异常

经常会使用local module的方式引入第三方开源库,比如:android-Ultra-Pull-To-Refresh

因为使用local module的方式,可以方便我们随时根据自己的需要修改第三方源码。

    dependencies {
        debugCompile project(path: ':foo', configuration: 'debug')//就像上面介绍的会报错
    }
会报如下错误
    Error:Could not resolve all dependencies for configuration
      ':app:prodDebugCompileClasspath'.
    Project :app declares a dependency from configuration 'compile'
      to configuration 'debug' which is not declared in the descriptor
      for project :foo.

错误原因就是:

plugin 3.0 提供了自动匹配variant的机制,所以不需要手动指定variant的配置(debug,release)

正确配置(官网)

 dependencies {

    // This is the old method and no longer works for local
    // library modules:
    // debugCompile project(path: ':foo', configuration: 'debug')
    // releaseCompile project(path: ':foo', configuration: 'release')

    // Instead, simply use the following to take advantage of
    // variant-aware dependency resolution. You can learn more about
    // the 'implementation' configuration in the section about
    // new dependency configurations.
    implementation project(':foo')

    // You can, however, keep using variant-specific configurations when
    // targeting external dependencies. The following line adds 'app-magic'
    // as a dependency to only the 'debug' version of your module.

    debugImplementation 'com.example.android:app-magic:12.3' //指定引用具体类型debug或者release
   }
4.采用新的Java library plugin

java_library_plugin

下图描述了使用Java库插件时的主要配置设置。因为plugin 3.0使用了gradle 4.0版本,所以有必要先介绍下,Gradle 3.4 推出了新的Java library plugin 配置:

允许控制发布以编译和运行时类路径(用于模块间依赖)

这里写图片描述

1.绿色的配置是用户应该用来声明依赖关系的配置
2.粉色方块:组件对library:编译时或者运行时
3.蓝色:组件内部使用,注意这里是不用传递的,比如a依赖b, b依赖c, 但是a不能获取c的配置
4.白色:配置集成自java plugin

了解gradle 3.4的新java plugin library依赖关系,有助于理解下面新的依赖关系标示的含义

新配置对应的过时配置描述
implementationcompilemodule编译时可用,module的使用者运行时可用,对于大量使用library的项目,可以显著提高编译时间,因为它可以减少构建系统重新编译一些module.大多数app/test因为使用这种配置
apicompilemodule编译时可用,module的使用者编译和运行时可用,这个和过时的compile一样的。一般是library模块会使用它,如果app模块一定要使用它,必须是在它想暴露api给test模块使用
compileOnlyprovidedmodule 编译时可用,但是module的使用者,在编译和运行时均不可用。跟过时的provided一样的。
runtimeOnlyapkmodule和它的使用者,运行时可用.它跟过时的apk是一样.

下图描述了测试配置的设置:

这里写图片描述

eg:
从Java插件继承的compile,testCompile,runtime和testRuntime配置仍然可用,但不建议使用。你应该避免使用它们,因为它们只是为了向后兼容而保留的。

5.使用annotationProcessor

使用annotationPorcessor代替apt

dependencies {
    ...
    annotationProcessor 'com.google.dagger:dagger-compiler:<version-number>'
}

注意:目前apt可用,但是未来稳定版本会移除。

6 禁用错误检查

如果包含了不需要的annotationProcessor编译路径,可以如下配置禁用错误检查提示

android {
    ...
    defaultConfig {
        ...
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath false
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灯塔@kuaidao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值