Android Studio 升級為3.1 踩到的坑

來源:互聯網
上載者:User

標籤:isa   log   article   gradle外掛程式   注意   eth   EAP   應用開發   keyword   

原文:80050700

  • AndroidStudio、gradle、buildToolsVersion的關係
  • Android Studio gradle外掛程式版本和gradle版本對應關係
  • Android Studio 升級為3.1遇到的問題
    • 問題一:Configuration ‘compile’ is obsolete and has been replaced with ‘implementation’ and ‘api’.`
      • api和implementation的區別:
    • 問題二:The SourceSet ‘instrumentTest’ is not recognized by the Android Gradle Plugin. Perhaps you misspelled something?
    • 問題三:extractDebugAnnotations is incompatible with java 8 sources and has been disabled.extractReleaseAnnotations is incompatible with java 8 sources and has been disabled
    • 問題四:解決No such property: FOR_RUNTIME for class: org.gradle.api.attributes.Usage的問題

 

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
  • gradle外掛程式版本和gradle版本對應如下: 

    官網連結:https://developer.android.google.cn/studio/releases/gradle-plugin.html#updating-plugin
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是不可見的 

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

Android Studio 升級為3.1 踩到的坑

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.