Google 发布了Android Studio 3.0,有很多功能值得我们使用,比如:Android Profiler, Android Plugin for Gradle 3.0.0(com.android.tools.build:gradle:3.0.0-alpha3)等。
今天这篇主要介绍下Android Plugin for Gradle 3.0.0.
Gradle需要更新到4.0-milestone-1,在gradle/wrapper/gradle-wrapper.properties中配置:
distributionUrl=\
https\://services.gradle.org/distributions/gradle-4.0-milestone-1-all.zip
然后就配置Android Plugin的版本
buildscript {
repositories {
...
// You need to add the following repository to download the
// new plugin.
maven {
url 'https://maven.google.com'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
}
}
注意需要增加“https://maven.google.com”,因为google发布了自己的maven库。
Android Plugin3.0的依赖机制:在使用library时会自动匹配variant(debug, release),就是说app的debug会自动匹配library的debug,相信大多数人也像我一样,当library多了,不会手动选择每个Library的variant。现在好了,它会自动匹配了。同样如果使用flavor的时候,比如app的redDebug同样会自动匹配library的readDebug。虽然有这样的优势,但是在使用flavor时,必须定义flavor dimension,否则会提示错误:
Error:All flavors must now belong to a named flavor dimension.
The flavor 'flavor_name' is not assigned to a flavor dimension.
这个错误,我也已经在使用Android Studio3.0以后碰到好多次了。现在使用flavor,必须像下面一样配置:
// Specifies a flavor dimension.
flavorDimensions "color"
productFlavors {
red {
// Assigns this product flavor to the 'color' flavor dimension.
// This step is optional if you are using only one dimension.
dimension "color"
...
}
blue {
dimension "color"
...
}
}
注意:如果library有两个dimensions:color,shape,但是app只有color,那么会如下的编译错误:
Error:Could not resolve all dependencies for configuration ':bar:redDebugCompileClasspath'.
Cannot choose between the following configurations on project :foo:
- redCircleDebugApiElements
- redSquareDebugApiElements
...
在APP使用flavorSelection选定使用某个flavor dimension,注意如下配置:
android {
...
// The flavorSelection property uses the following format:
// flavorSelection 'dimension_name', 'flavor_name'
// Chooses the 'square' flavor from libraries that specify a 'shape'
// dimension.
flavorSelection 'shape', 'square'
}
在我以前开发的项目中,我经常会使用local module的方式引入第三方开源库,比如:AndroidAutoLayout, mosby,android-Ultra-Pull-To-Refresh。因为使用local module的方式,可以方便我们随时根据自己的需要修改第三方源码。
第一节,我们介绍了plugin 3.0 提供了自动匹配variant的机制,所以不需要手动指定variant的配置,比如:redDebugImplementation。
在android plugin 3.0像下面这样配置会报错:
dependencies {
debugCompile project(path: ':foo', configuration: 'debug')//就像上面介绍的会报错
}
报错log:
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.
正确配置:
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'
}
plugin3.0以前,需要对每个wear应用的模块配置,但是plugin3.0会把所有的合并在一起解析。plugin3.0以前我们会像下面这样配置:
dependencies {
// This is the old way of configuring Wear App dependencies.
wearApp project(':wear1')
blueWearApp project(':wear2')
}
plugin3.0一下,如果所有的flavor使用同一个library,使用如下配置:
dependencies {
// If the main app and wearable modules have the same flavors,
// the following configuration uses automatic dependency matching.
wearApp project(':wearable')
}
如果不是上面这种情况,可以为了每个flavor指定配置,如下:
dependencies {
redWearApp project(':wear1')
greenWearApp project(':wear1')
blueWearApp project(':wear2')
}
因为plugin 3.0使用了gradle 4.0版本,所以有必要先介绍下,Gradle 3.4 推出了新的Java library plugin 配置:允许控制发布以编译和运行时类路径(用于模块间依赖)
通过下面一张图来说明下新的Java library plugin 配置关系:
1)绿色方块:使用者可以使用的依赖,比如:app配置library的依赖
2)粉色方块:组件对library:编译时或者运行时
3)蓝色:组件内部使用,注意这里是不用传递的,比如a依赖b, b依赖c, 但是a不能获取c的配置
4)白色:配置集成自java plugin
OK,了解了gradle 3.4的新java plugin library依赖关系,因为android plugin 3.0使用gradle 4.0,所以这些自然也会引入了,下面这张表可以简单说明:
新配置 | 对应的过时配置 | 描述 |
---|---|---|
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是一样. |
注意:compile,provided,apk 这些过时的依赖现在可以使用,但是在下个版本会移除,所以Google给我们一些时间使用。
使用annotationPorcessor代替apt
dependencies {
...
annotationProcessor 'com.google.dagger:dagger-compiler:'
}
注意:目前apt可用,但是未来稳定版本会移除。
如果包含了不需要的annotationProcessor编译路径,可以如下配置禁用错误检查提示
android {
...
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath false
}
}
}
}
其他部分在实际项目开发,可能不会用到,但是有兴趣的童鞋也可以去看英文版的介绍:gradle-plugin-3-0-0-migration