Android 主Module引用依賴Module,卻無法使用里面的依賴庫


如果模塊化開發中遇到

多模塊的AndroidManifest.xml沒有合並
or
多模塊的資源文件沒有合並
or
模塊A include了模塊B,而無法使用模塊B內依賴的其他aar包中的類的時候
or
提示Support包版本不一致
這篇文章可能就是你要的解決方案~

舉個栗子:

比如我們現在有一個App模塊設計為:
主工程: app
模塊: ui , framework

引入模塊的方式:在settings.gradle中,指定正確的模塊路徑

include ':app', ':framework', ':ui'
project(':framework').projectDir = new File('../framework')
project(':ui').projectDir = new File('../ui')


如果現在framework引入了一些依賴庫,假設如下:

// Retrofit 網絡框架依賴
implementation "com.squareup.retrofit2:retrofit:2.5.0"

// Gson 依賴
implementation 'com.google.code.gson:gson:2.8.5'

// ARouter解耦框架
implementation 'com.alibaba:arouter-api:1.4.1'
annotationProcessor 'com.alibaba:arouter-compiler:1.2.2'


如果這樣寫的話,主工程app中將無法調用到這些依賴庫中的類。
因為implementation聲明的依賴只能在本module模塊內使用,跨module使用就要使用api聲明(其實就是曾經的compile,但是如果設置成compile,AS又要給你叫叫叫)!!改成如下即可

// Retrofit 網絡框架依賴
api "com.squareup.retrofit2:retrofit:2.5.0"

// Gson 依賴
api 'com.google.code.gson:gson:2.8.5'

// ARouter解耦框架
api 'com.alibaba:arouter-api:1.4.1'
annotationProcessor 'com.alibaba:arouter-compiler:1.2.2'

同理,這是模塊里引用別的aar,這樣配置完了,那么緊接着需要在依賴framework的app模塊中,
在build.gradle的依賴處加入對應的引入哈。

api project(":framework")


當然如果全部做完,由於很多aar里使用了不一樣的support版本,一定會提示版本不兼容了(其他aar不兼容的解決方案一樣哈),在主工程app的build.gradle中指定:

android {
    .....
    .....
//指定jdk版本
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    // 如果提示多個重復文件,加這屬性
    packagingOptions {
        exclude 'META-INF/proguard/androidx-annotations.pro'
    }
}

// 用於指定所有aar引用同樣版本的support包
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '28.0.0'
            }
        }
    }
}

 

————————————————
版權聲明:本文為CSDN博主「胖虎」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/ljphhj/article/details/86262031


免責聲明!

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



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