Android Gradle依賴管理、去除重復(fù)依賴、忽略的方式
常用依賴
//1.直接依賴第三方開(kāi)源庫(kù),一般是托管在 jitpack 或者 jcenter implementation ’com.google.code.gson:gson:2.2.4’ implementation ’com.android.support:cardview-v7:25.0.0’ implementation ’com.android.support:design:25.0.0’ //2.直接依賴本地的aar文件,一般是在libs目錄下 implementation(name: ’LiteAVSDK_Professional_5.1.5293’, ext: ’aar’) //3.直接依賴本地的jar文件 implementation files(’libs/bdasr_V3_20170801_60da871.jar’) //4.依賴本地的model implementation project(’:wavelibrary’) implementation project(’:android-ffmpeg’)
庫(kù)工程依賴傳遞問(wèn)題
1、依賴常用的基本類型有:provided和compile,provided 只在編譯生效不會(huì)打包到 apk 或 aar 中;compile 是會(huì)打包到 apk或 aar 中的(如果是庫(kù)工程的話有特殊情況,參考下面3).
2、app 工程的當(dāng)前(compile+) 的依賴都會(huì)打包到 app 中
3、庫(kù)工程中:
1) jar 包:遠(yuǎn)程依賴不會(huì)打包到 aar 中;本地依賴會(huì);
2) aar:遠(yuǎn)程和本地都不不會(huì)打包到 aar中.
3) 如果你要提供你的庫(kù)工程的 aar 給他人,你需要同時(shí)告訴他這個(gè)庫(kù)工程依賴的其他aar 和遠(yuǎn)程 jar包(因?yàn)樗麄儧](méi)有打包到 aar 中)
4) 如果通過(guò)工程依賴(即compile project(’:lib’)的方式), 依賴是可以傳遞的,所以不需要在聲明一次依賴.
去掉重復(fù)依賴
1.第三方庫(kù)中同樣使用了implementation或者compile依賴相同的庫(kù)
implementation(’com.allenliu.versionchecklib:library:2.0.5’) { exclude group: ’com.android.support’, module: ’appcompat-v7’ exclude group: ’com.android.support.constraint’, module: ’constraint-layout’ exclude group: ’org.greenrobot’, module: ’eventbus’ exclude group: ’com.squareup.okhttp3’, module: ’okhttp’ }
2.在不同的庫(kù)中出現(xiàn)相同的so文件
pickFirst只會(huì)打包第一個(gè)遇到的沖突的so,merge(碰到?jīng)_突會(huì)合并)和exclude(直接排除匹配到的文件,不建議使用)
packagingOptions { pickFirst ’lib/arm64-v8a/libgnustl_shared.so’ pickFirst ’lib/armeabi-v7a/libgnustl_shared.so’ }
遇到這種錯(cuò)誤可以通過(guò)上面方法嘗試解決
Error:Execution failed for task ‘:app:transformNativeLibsWithMergeJniLibsForDebug’. > More than one
補(bǔ)充知識(shí):Gradle依賴的統(tǒng)一管理,解決依賴沖突
看見(jiàn)別人在用implementation rootProject.ext.dependencies['xxxx']不知道是什么意思,上網(wǎng)查了一下,原來(lái)是為了解決或者說(shuō)預(yù)防gradle依賴沖突的問(wèn)題。
在項(xiàng)目開(kāi)發(fā)中我們會(huì)經(jīng)常引入多個(gè)Module,然而每個(gè)Module中又包含了V4、V7,為了升級(jí)新版本依賴包只用更改一次,我們決定采用Gradle依賴的統(tǒng)一管理,避免重復(fù)繁瑣的勞動(dòng)。
記錄get到的新知識(shí),用法如下:
1.在Project目錄下新建config.gradle文件,文件名可自定義
具體內(nèi)容如下:
ext { android = [ compileSdkVersion : 27, buildToolsVersion : '27.0.0', minSdkVersion : 21, targetSdkVersion : 27, versionCode : 6, versionName : '1.2.2', renderscriptTargetApi : 21 ] version = [ supportLibraryVersion : '26.1.1', okhttpVersion : '3.9.0', retrofitVersion : '2.3.0', glideVersion : '4.0.0', butterknifeVersion : '8.8.1', fragmentationVersion : '1.1.9', ] dependencies = [ //base 'appcompat-v7' : 'com.android.support:appcompat-v7:${version['supportLibraryVersion']}', 'cardview-v7' : 'com.android.support:cardview-v7:${version['supportLibraryVersion']}', 'design' : 'com.android.support:design:${version['supportLibraryVersion']}', 'constraint-layout' : 'com.android.support.constraint:constraint-layout:1.0.2', //net 'gson' : 'com.google.code.gson:gson:2.8.2', 'okhttp' : 'com.squareup.okhttp3:okhttp:${version['okhttpVersion']}', 'logging-interceptor': 'com.squareup.okhttp3:logging-interceptor:${version['okhttpVersion']}', 'retrofit' : 'com.squareup.retrofit2:retrofit:${version['retrofitVersion']}', 'converter-gson' : 'com.squareup.retrofit2:converter-gson:${version['retrofitVersion']}', 'adapter-rxjava2' : 'com.squareup.retrofit2:adapter-rxjava2:${version['retrofitVersion']}', //dao 'greendao' : 'org.greenrobot:greendao:3.2.2', //rx 'rxjava' : 'io.reactivex.rxjava2:rxjava:2.1.5', 'rxandroid' : 'io.reactivex.rxjava2:rxandroid:2.0.1', 'rxbinding' : 'com.jakewharton.rxbinding2:rxbinding:2.1.0', 'rxpermissions' : 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.5@aar', //di 'javax_annotation' : 'org.glassfish:javax.annotation:10.0-b28', 'butterknife' : 'com.jakewharton:butterknife:${version['butterknifeVersion']}', 'butterknife-compiler' : 'com.jakewharton:butterknife-compiler:${version['butterknifeVersion']}', //multidex 'multidex' : 'com.android.support:multidex:1.0.3', //kotlin 'kotlin-stdlib' : 'org.jetbrains.kotlin:kotlin-stdlib:1.2.10', //ui test 'espresso-core' : 'com.android.support.test.espresso:espresso-core:3.0.2', 'espresso-idling-resource' : 'com.android.support.test.espresso:espresso-idling-resource:3.0.2', //unit test , 為了整合mockito和PowerMockito,mockito暫時(shí)最高只支持2.8.9 'junit' : 'junit:junit:4.12', 'mockito' : 'org.mockito:mockito-core:2.8.9', 'powermock-module-junit4' : 'org.powermock:powermock-module-junit4:1.7.4' ]}
2.在Project的build.gradle中添加
apply from: 'config.gradle'
3.在modle的build.gradle中添加引用
apply plugin: ’com.android.application’android { compileSdkVersion rootProject.ext.android['compileSdkVersion'] buildToolsVersion rootProject.ext.android['buildToolsVersion'] defaultConfig { applicationId 'json.chao.com.wanandroid' minSdkVersion rootProject.ext.android['minSdkVersion'] targetSdkVersion rootProject.ext.android['targetSdkVersion'] versionCode rootProject.ext.android['versionCode'] versionName rootProject.ext.android['versionName'] //AndroidJunitRunner必須要顯示指定在defaultConfig中,使用Gradle依賴管理無(wú)法使其生效 testInstrumentationRunner ’android.support.test.runner.AndroidJUnitRunner’ renderscriptTargetApi rootProject.ext.android['renderscriptTargetApi'] renderscriptSupportModeEnabled true // Enable RS support multiDexEnabled true vectorDrawables.useSupportLibrary = true }} dependencies { implementation fileTree(include: [’*.jar’], dir: ’libs’) //base implementation rootProject.ext.dependencies['appcompat-v7'] implementation rootProject.ext.dependencies['cardview-v7'] implementation rootProject.ext.dependencies['design'] implementation rootProject.ext.dependencies['constraint-layout'] //net implementation rootProject.ext.dependencies['gson'] implementation rootProject.ext.dependencies['okhttp'] implementation rootProject.ext.dependencies['retrofit'] implementation rootProject.ext.dependencies['converter-gson'] implementation rootProject.ext.dependencies['adapter-rxjava2'] //dao implementation rootProject.ext.dependencies['greendao'] //rx implementation rootProject.ext.dependencies['rxjava'] implementation rootProject.ext.dependencies['rxandroid'] implementation rootProject.ext.dependencies['rxbinding'] implementation rootProject.ext.dependencies['rxpermissions'] //UI測(cè)試 androidTestImplementation (rootProject.ext.dependencies['espresso-core']) { exclude group: ’com.android.support’, module: ’support-annotations’ } implementation (rootProject.ext.dependencies['espresso-idling-resource']) { exclude module: ’support-annotations’ }}
(兩個(gè)文件中有不對(duì)應(yīng)的依賴方法,that’s ok, 只是粘貼代碼的時(shí)候刪除了一些,知道用法就行了)
以上這篇Android Gradle依賴管理、去除重復(fù)依賴、忽略的方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python 的 __str__ 和 __repr__ 方法對(duì)比2. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法3. Spring security 自定義過(guò)濾器實(shí)現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實(shí)例代碼)4. IntelliJ IDEA設(shè)置背景圖片的方法步驟5. docker /var/lib/docker/aufs/mnt 目錄清理方法6. Python TestSuite生成測(cè)試報(bào)告過(guò)程解析7. 學(xué)python最電腦配置有要求么8. JAMon(Java Application Monitor)備忘記9. Python Scrapy多頁(yè)數(shù)據(jù)爬取實(shí)現(xiàn)過(guò)程解析10. Python OpenCV去除字母后面的雜線操作
