Android Studio 升級為3.1 踩到的坑


原文:https://blog.csdn.net/xiariluoxue/article/details/80050700

 

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() } }
  • 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將會在所有模塊中可見。

  • 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代替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' } 
  • 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指令的依賴不會傳遞)

有一個module_A依賴於glide(module_A使用的是implementation 指令來依賴glide)

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來引用glide

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是可見的

建議:在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做了重命名

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM