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版本

相關文章
相關標籤/搜索