Gradle3.0新指令api、provided、implementation等對比

Android Studio3.0正式版已經出來了,相比2.x的版本,Gradle版本也升級爲了3.x,編譯速度提升了很多。
在gadle3.0以後,默認的依賴由以前的compile更改成implementationjava

  • 對比表

    Android Studio 2.X Android Studio 3.X
    apk runtimeOnly
    provided compileOnly
    compile api
    沒有對應 implementation
    debugCompile debugImplementation
    releaseCompile releaseImplementation
    androidTestCompile androidTestImplementation
  • api (compile)

    • 依賴向上傳遞
    • 若A api B, B api C,C module對A module可見
  • implementation (新指令: 具有依賴可見性)

    • 依賴不向上傳遞android

    • 若A implementation B, B implementation C,C module對A module不可見api

    • 若A implementation B, B api C,C module對A module可見ide

    • 功能同api,區別僅僅是增長了依賴可見性單元測試

  • compileOnly(provided)

    • 只在編譯時有效,不會參與打包
    • 若A implementation C,打包後apk(A + C);而A compileOnly C,打包後apk(A);該指令實質:A module僞裝依賴了C module經過欺騙編譯器編譯時檢測以免java.lang.ClassNotFoundException編譯報錯
    • 使用情形
      • A implementation C,B implementation C,打包時A module生成aar(A + C),B module生成aar(B + C)
      • 若改爲A implementation C,B compileOnlyC,打包時A module生成aar(A + C),B module生成aar(B)
      • 最終apk包(A + B + C),結果一致
      • 雖然aar(B)不真實依賴C module,但B module確實用到了C module的api。沒有運行時錯誤的緣由:aar(A + C)與aar(B)合併生成apk,B module運行時找到並調用aar(A + C)中的C module
  • runtimeOnly(apk)

    只在生成apk的時候參與打包,編譯時不會參與,不多用。測試

  • debugImplementation(debugCompile)

    debugImplementation 只在debug模式的編譯和最終的debug apk打包時有效。debug

  • releaseImplementation(releaseCompile)

    releaseImplementation 僅僅針對release 模式的編譯和最終的release apk打包。3d

  • androidTestImplementation(androidTestCompile)

    androidTestImplementation 只在單元測試代碼的編譯以及最終打包測試apk時有效。編譯器

做者:Dsiner 連接:https://www.jianshu.com/p/83ddb81e73f9 來源:簡書 著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。
相關文章
相關標籤/搜索