Android Studio 升级为3.1 踩到的坑

Android Studio升级为3.1,Gradle 4.4,buildToolsVersion 27.0.3

AndroidStudio、gradle、buildToolsVersion的关系

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应用程序特有的几项功能。

  1. AndroidStudio:是Google官方基于IntelliJ IDEA开发的一款Android应用开发工具
  2. Gradle:是一个工具,也是一个编程框架。使用Gradle可完成app的编译打包等工作。
  3. buildToolsVersion: android构建工具的版本,其中包含打包工具aapt、dx等。buildToolsVersion在安装的android sdk路径下的/build-tools/

Android Studio gradle插件版本和gradle版本对应关系

  • gradle插件版本配置:project对应的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将会在所有模块中可见。

  • gradle版本配置
    gradle/wrapper/gradle-wrapper.properties文件中
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

Android Studio 升级为3.1遇到的问题

问题一:Configuration ‘compile’ is obsolete and has been replaced with ‘implementation’ and ‘api’.`

使用implementation或者api代替compile

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指令的依赖不会传递)

有一个module_A依赖于glide(module_A使用的是implementation 指令来依赖glide)

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 'com.github.bumptech.glide:glide:3.7.0'  

用implementation指令编译,Glide依赖对Module是module_B是不可见的
用implementation指令编译,Glide依赖对Module是module_B是不可见的
用api指令编译,Glide依赖对Module是module_B是可见的
用api指令编译,Glide依赖对Module是module_B是可见的

建议:在Google IO 中提到了一个建议,依赖首先应该设置为implementation的,如果没有错,那就用implementation,如果有错,那么使用api指令。`使用implementation会使编译速度有所增快。`

问题二:The SourceSet ‘instrumentTest’ is not recognized by the Android Gradle Plugin. Perhaps you misspelled something?

将instrumentTest改为 androidTest
新版本Gradle对instrumentTest做了重命名

旧版本 新版本
instrumentTestCompile androidTestCompile
instrumentTest androidTest

问题三:extractDebugAnnotations is incompatible with java 8 sources and has been disabled.extractReleaseAnnotations is incompatible with java 8 sources and has been disabled

这里写图片描述
项目里使用了me.tatarka:gradle-retrolambda,

扫描二维码关注公众号,回复: 50534 查看本文章
dependencies {
      classpath 'me.tatarka:gradle-retrolambda:3.2.5'
}

对retrolambda进行了升级,解决了问题

dependencies {
       classpath 'me.tatarka:gradle-retrolambda:3.7.0'
}

问题四:解决 No such property: FOR_RUNTIME for class: org.gradle.api.attributes.Usage

dependencies {
    //classpath 'com.novoda:bintray-release:0.5.0'  
    classpath 'com.novoda:bintray-release:0.8.0'
}

升级com.novoda.bintray-release版本

猜你喜欢

转载自blog.csdn.net/xiariluoxue/article/details/80050700