Android Studio 升级为3.1 踩到的坑

原文:http://www.javashuo.com/article/p-fwsrbbmk-mr.htmlhtml

 

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

  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() } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

buildscript定义了全局的相关属性 
repositories定义仓库:一个仓库表明着依赖包的来源,例如jcenter仓库dependencies用来定义构建过程:仅仅须要定义默认的Android插件,该插件能够让你执行相关Android的tasks,注意:不该该在该方法体内定义子模块的依赖包。 
allprojects用来定义各个模块的默认属性:不单单局限于默认的配置,也能够本身创造tasks在allprojects方法体内,这些tasks将会在全部模块中可见。android

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

Android Studio 升级为3.1遇到的问题

Android Studio升级为3.1,Gradle 4.4,buildToolsVersion 27.0.3所带来的问题
  • 1
  • 2

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

使用implementation或者api代替compilenginx

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' } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这里写图片描述

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' } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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' 
  • 1

另外一个module_B,依赖于module_A:编程

implementation project(':module_A')
  • 1

此时module_B里边不能引用glide,module_B要想引用glide,就在module_A使用的是api来引用glideapi

api 'com.github.bumptech.glide:glide:3.7.0' 
  • 1

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

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

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

将instrumentTest改成 androidTest 
新版本Gradle对instrumentTest作了重命名ruby

旧版本 新版本
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,

dependencies { classpath 'me.tatarka:gradle-retrolambda:3.2.5' }
  • 1
  • 2
  • 3

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

dependencies { classpath 'me.tatarka:gradle-retrolambda:3.7.0' } 
  • 1
  • 2
  • 3
  • 4

问题四:解决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' }
  • 1
  • 2
  • 3
  • 4

升级com.novoda.bintray-release版本

相关文章
相关标签/搜索