Android Gradle依赖管理,去除重复依赖 Android Gradle依赖管理、去除重复依赖、忽略的方式
wapchief 人气:2常用依赖
//1.直接依赖第三方开源库,一般是托管在 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')
库工程依赖传递问题
1、依赖常用的基本类型有:provided和compile,provided 只在编译生效不会打包到 apk 或 aar 中;compile 是会打包到 apk或 aar 中的(如果是库工程的话有特殊情况,参考下面3).
2、app 工程的当前(compile+) 的依赖都会打包到 app 中
3、库工程中:
1) jar 包:远程依赖不会打包到 aar 中;本地依赖会;
2) aar:远程和本地都不不会打包到 aar中.
3) 如果你要提供你的库工程的 aar 给他人,你需要同时告诉他这个库工程依赖的其他aar 和远程 jar包(因为他们没有打包到 aar 中)
4) 如果通过工程依赖(即compile project(':lib')的方式), 依赖是可以传递的,所以不需要在声明一次依赖.
去掉重复依赖
1.第三方库中同样使用了implementation或者compile依赖相同的库
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.在不同的库中出现相同的so文件
pickFirst只会打包第一个遇到的冲突的so,merge(碰到冲突会合并)和exclude(直接排除匹配到的文件,不建议使用)
packagingOptions { pickFirst 'lib/arm64-v8a/libgnustl_shared.so' pickFirst 'lib/armeabi-v7a/libgnustl_shared.so' }
遇到这种错误可以通过上面方法尝试解决
Error:Execution failed for task ‘:app:transformNativeLibsWithMergeJniLibsForDebug'. > More than one
补充知识:Gradle依赖的统一管理,解决依赖冲突
看见别人在用implementation rootProject.ext.dependencies["xxxx"]不知道是什么意思,上网查了一下,原来是为了解决或者说预防gradle依赖冲突的问题。
在项目开发中我们会经常引入多个Module,然而每个Module中又包含了V4、V7,为了升级新版本依赖包只用更改一次,我们决定采用Gradle依赖的统一管理,避免重复繁琐的劳动。
记录get到的新知识,用法如下:
1.在Project目录下新建config.gradle文件,文件名可自定义
具体内容如下:
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暂时最高只支持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依赖管理无法使其生效 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测试 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' } }
(两个文件中有不对应的依赖方法,that's ok, 只是粘贴代码的时候删除了一些,知道用法就行了)
以上这篇Android Gradle依赖管理、去除重复依赖、忽略的方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
加载全部内容