原文:http://www.javashuo.com/article/p-fwsrbbmk-mr.htmlhtml
The Android Studio build system is based on Gradle, and the Android plugin for Gradle adds several features that are specific to building Android apps.
Android Studio基于Gradle构建系统,为了构建Android应用,Android gradle 插件添加了构建Android应用程序特有的几项功能。java
build.gradle
文件中buildscript {
repositories {
/**Gradle 4.1 and higher include support for Google's Maven repo using the google() method. And you need to include this repo to download Android plugin 3.0.0 or higher.*/ jcenter() google() ... } dependencies { classpath 'com.android.tools.build:gradle:3.1.1' } } allprojects { repositories { jcenter() google() } }
buildscript定义了全局的相关属性
repositories
定义仓库:一个仓库表明着依赖包的来源,例如jcenter仓库dependencies
用来定义构建过程:仅仅须要定义默认的Android插件
,该插件能够让你执行相关Android的tasks,注意:不该该在该方法体内定义子模块的依赖包。allprojects
用来定义各个模块的默认属性:不单单局限于默认的配置,也能够本身创造tasks在allprojects方法体内,这些tasks将会在全部模块中可见。android
gradle/wrapper/gradle-wrapper.properties
文件中distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
Android Studio升级为3.1,Gradle 4.4,buildToolsVersion 27.0.3所带来的问题
使用implementation或者api代替compile
nginx
dependencies { compile 'com.google.dagger:dagger:2.11' compile 'com.google.dagger:dagger-android:2.11' debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.4' releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' }
dependencies { implementation 'com.google.dagger:dagger:2.11' implementation 'com.google.dagger:dagger-android:2.11' debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4' debugImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4' }
api和implementation的区别:
api:模块的依赖对外公开,可被依赖包所引用(彻底等同于compile指令)
implementation:依赖只做用于当前的module
,将该模块的依赖隐藏在内部,而不对外部公开(使用implementation指令的依赖不会传递)git
有一个module_A依赖于glide(module_A使用的是implementation 指令来依赖glide)github
implementation 'com.github.bumptech.glide:glide:3.7.0'
另外一个module_B,依赖于module_A:编程
implementation project(':module_A')
此时module_B里边不能引用glide,module_B要想引用glide,就在module_A使用的是api来引用glide
api
api 'com.github.bumptech.glide:glide:3.7.0'
用implementation指令编译,Glide依赖对Module是module_B是不可见的
用api指令编译,Glide依赖对Module是module_B是可见的 android-studio
建议:在Google IO 中提到了一个建议,依赖首先应该设置为implementation的,若是没有错,那就用implementation,若是有错,那么使用api指令。`使用implementation会使编译速度有所增快。`
将instrumentTest改成 androidTest
新版本Gradle对instrumentTest作了重命名ruby
旧版本 | 新版本 |
---|---|
instrumentTestCompile | androidTestCompile |
instrumentTest | androidTest |
项目里使用了me.tatarka:gradle-retrolambda,
dependencies { classpath 'me.tatarka:gradle-retrolambda:3.2.5' }
对retrolambda进行了升级,解决了问题
dependencies { classpath 'me.tatarka:gradle-retrolambda:3.7.0' }
dependencies {
//classpath 'com.novoda:bintray-release:0.5.0' classpath 'com.novoda:bintray-release:0.8.0' }
升级com.novoda.bintray-release版本