簡體   English   中英

Android Gradle 在另一個模塊中包含模塊依賴項

[英]Android Gradle include module dependencies in another module

我正在使用最新的 Android Studio 3.0.0-beta6 來構建我的 Android 項目,但存在此依賴性問題。 Gradle鼓勵我更換所有compile's使用implementation's 這是我的項目結構:

項目:

  • 模塊1

    • 模塊2

Module1 依賴於一些庫,module2 依賴於 module1。 但是,模塊 1 中的庫在模塊 2 中不可見。 我不想復制粘貼依賴項,而是希望庫依賴項只聲明一次。 有沒有簡單的解決方案? 謝謝。

module1 的構建等級:

dependencies {
    ....
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    ...
}

module2 的構建等級:

implementation project(':module1')

在你的 Root Project -> build.gradle 你應該有這樣的東西:

allprojects {
    repositories {
        jcenter()
    }
}

在那里添加依賴項!!

更新

如果您不希望所有模塊都具有公共依賴項而只希望特定模塊,則執行以下操作:

  1. 在您的根項目/gradleScript 中創建一個文件夾
  2. 在此文件夾中創建一個 .gradle 文件(例如 gradleScript/dependencies.gradle),如下所示:

     ext { //Version supportLibrary = '22.2.1' //Support Libraries dependencies supportDependencies = [ design : "com.android.support:design:${supportLibrary}", recyclerView : "com.android.support:recyclerview-v7:${supportLibrary}", cardView : "com.android.support:cardview-v7:${supportLibrary}", appCompat : "com.android.support:appcompat-v7:${supportLibrary}", supportAnnotation: "com.android.support:support-annotations:${supportLibrary}", ] }
  3. 在您的根項目 build.gradle 中添加以下行:

     buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.2.3' } } // Load dependencies apply from: 'gradleScript/dependencies.gradle'
  4. 在您的模塊中相應地添加以下內容:

     // Module build file dependencies { //...... compile supportDependencies.appCompat compile supportDependencies.design }

希望這有幫助!!!

老問題,但在我看來,您所追求的只是在您的 gradle 文件中使用api而不是implementation

如果模塊 X 聲明了api依賴項,則它可傳遞給依賴於 X 的任何其他模塊。但是,如果它聲明了implementation依賴項,則該依賴項中的類將放在模塊 X 的類路徑上,但不適用於其中的任何模塊取決於模塊 X。

從 Gradle 的文檔

api 配置中出現的依賴項將傳遞給庫的使用者,因此將出現在使用者的編譯類路徑上。 另一方面,在實現配置中發現的依賴項不會暴露給消費者,因此不會泄漏到消費者的編譯類路徑中。

所以具體來說:

模塊一:

dependencies {
    ....
    api 'io.reactivex.rxjava2:rxandroid:2.0.1'
    api 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    ...
}

模塊二:

implementation project(':module1')

這不是您想要做的最簡單的解決方案嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM