前言
新事物出现,肯定会带来一些新的优化,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库插件时的主要配置设置。因为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依赖关系,有助于理解下面新的依赖关系标示的含义
新配置 | 对应的过时配置 | 描述 |
---|---|---|
implementation | compile | module编译时可用,module的使用者运行时可用,对于大量使用library的项目,可以显著提高编译时间,因为它可以减少构建系统重新编译一些module.大多数app/test因为使用这种配置 |
api | compile | module编译时可用,module的使用者编译和运行时可用,这个和过时的compile一样的。一般是library模块会使用它,如果app模块一定要使用它,必须是在它想暴露api给test模块使用 |
compileOnly | provided | module 编译时可用,但是module的使用者,在编译和运行时均不可用。跟过时的provided一样的。 |
runtimeOnly | apk | module和它的使用者,运行时可用.它跟过时的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
}
}
}
}