1. 程式人生 > 程式設計 >Android Gradle依賴管理、去除重複依賴、忽略的方式

Android Gradle依賴管理、去除重複依賴、忽略的方式

常用依賴

 //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檔案,檔名可自定義

Android 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依賴管理、去除重複依賴、忽略的方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。