【问题标题】:Resolve duplicated external libraries in Android Studio解决 Android Studio 中重复的外部库
【发布时间】:2021-05-24 17:12:30
【问题描述】:

当我检查我的 Android Studio 项目中的外部库时,我看到不同版本的重复库。

有什么办法可以找出原因。

我绝对不会添加多个依赖项。但不确定,是什么原因造成的。 这是我的 build.gradle 文件;

// 顶级构建文件,您可以在其中添加所有子项目/模块通用的配置选项。

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.1'
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:4.3.8'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.6.1'

    }


}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
        mavenCentral()
        maven() {
            url "https://oss.sonatype.org/content/repositories/snapshots"
        }
        maven {
            url "s3://android.maven.s3.amazonaws.com"
            credentials(AwsCredentials) {
                accessKey ""
                secretKey ""
            }
        }
    }
}

subprojects {

    project.evaluationDependsOn(':_ABT')
//[PART TO ADD START]
    project.configurations.all {

        resolutionStrategy.eachDependency { details ->

            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex') ) {
                details.useVersion "27.1.0"
            }
        }
    }
//[PART TO ADD END]
}

ext {
    supportLibVersion = "28.0.0"
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是build.gradle的依赖文件;

apply plugin: 'com.android.library'

android {
    compileSdkVersion 30
    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildToolsVersion '30.0.3'
    android {
        lintOptions {
            checkReleaseBuilds false
            abortOnError false
        }
    }
    useLibrary 'android.test.runner'

    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'
    compileOptions {
    sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    api 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    androidTestImplementation 'androidx.test:rules:1.4.0-beta01'
    implementation 'androidx.annotation:annotation:1.2.0'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.loopj.android:android-async-http:1.4.9'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.google.zxing:core:3.3.2'
    implementation('com.journeyapps:zxing-android-embedded:3.6.0') { transitive = false }
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.gms:play-services-plus:17.0.0'
    implementation 'com.google.firebase:firebase-core:19.0.0'
    implementation 'com.google.firebase:firebase-messaging:22.0.0'
    implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
    implementation 'com.stepstone.stepper:material-stepper:4.3.1'
    implementation 'com.github.santalu:mask-edittext:1.0.5'
    implementation 'com.google.android.gms:play-services-base:17.6.0'
    implementation 'com.google.android.gms:play-services-identity:17.0.0'
    implementation 'com.google.android.gms:play-services-auth:19.0.0'
    implementation 'com.google.android.gms:play-services-auth-api-phone:17.5.0'
    implementation('com.github.ozodrukh:CircularReveal:2.0.1@aar') {
        transitive = true;
    }
    implementation 'joda-time:joda-time:2.10'
    implementation 'com.google.android.gms:play-services-maps:17.0.1'
    implementation 'com.google.firebase:firebase-database:20.0.0'
    implementation 'com.google.android.gms:play-services-location:18.0.0'

    //Bibliotecas necessárias para rodar secure mobile token
    implementation 'com.madgag.spongycastle:bctls-jdk15on:1.58.0.0'
    implementation 'com.yakivmospan:scytale:1.0.1'
    implementation 'com.github.instacart.truetime-android:library:3.3'

    implementation group: 'br.com.martonis.library', name: 'PhoneNumberFormatter', version:'1.4', ext: 'aar', classifier: 'release'
    implementation group: 'br.com.martonis.library', name: 'KeyboardStateWatcher', version: '1.2', ext: 'aar', classifier: 'release'
    implementation group: 'br.com.martonis.library', name: 'SecureMobileToken', version: '3.125', ext: 'aar', classifier: 'release'
    implementation group: 'br.com.martonis.library', name: 'CreditCardCustomView', version: '2.10', ext: 'aar', classifier: 'release'
    implementation group: 'br.com.martonis.library', name: 'Updater', version: '2.2', ext: 'aar', classifier: 'release'

    implementation 'com.shawnlin:number-picker:2.4.11'

    implementation 'androidx.core:core:1.5.0'
    implementation 'androidx.legacy:legacy-support-core-ui:1.0.0'
    implementation 'me.toptas.fancyshowcase:fancyshowcaseview:1.1.5'
    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.31"

    implementation 'androidx.recyclerview:recyclerview:1.2.0'
    implementation 'com.google.android.gms:play-services-vision:20.1.3'
    implementation 'org.greenrobot:eventbus:3.1.1'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.14'
    implementation 'com.google.android.gms:play-services-maps:17.0.1'
    implementation 'com.ethanhua:skeleton:1.1.2'
    implementation 'io.supercharge:shimmerlayout:2.1.0'
    implementation 'org.apache.commons:commons-lang3:3.4'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
}

apply plugin: 'com.google.gms.google-services'

这些是一些重复的库。例如,我有 3 个用于 android-core 的库:

androidx.core:core:1.3.1@aar
androidx.core:core:1.3.2@aar
androidx.core:core:1.5.0@aar

你可以在下面看到更多:

有没有什么方法可以查出是谁调用了这些库来找出它们的来源?

提前致谢!

【问题讨论】:

    标签: android firebase android-studio


    【解决方案1】:

    这可能是因为所谓的传递依赖。假设您正在使用 android 核心库版本 1.5.0,并且您还使用另一个库,例如 XyzImage 库。现在 XyzImage 库内部依赖于 android 核心库版本 1.2.0。现在由于这个原因,您的应用程序依赖树如下所示:

    -- Your App
     +-> AndroidCore@1.5.0
     +-> XyzImageLibrary@1.1.0
         +-> AndroidCore@1.2.0
    

    现在 XyzImageLibrary 可以配置它的 gradle 文件,这样 gradle 在处理 XyzImageLibrary 文件时应该只使用 1.2.0 版本的 AndroidCore 库。因此,gradle 需要下载同一个库的多个版本。您可以按照解决方案https://stackoverflow.com/a/35235229/5309486 查找您的Android App 的依赖关系树。

    【讨论】:

    • 非常感谢您的回答,我列出了依赖项,但是在不同的标题下有很多依赖项,分别是 debugAndroidTestCompileClasspathdebugCompileClasspathdebugRuntimeClasspathdebugUnitTestCompileClasspath 等。现在debugAndroidTestCompileClasspathdebugCompileClasspath 具有不同版本的 com.google.firebase:firebase-common:19.4.0 -> 20.0.0 (c) 库。第一个版本是19.4.0,第二个版本是我上面写的。我怎样才能解决这个问题 ?谢谢
    • 这些都是随 com.google.firebase:firebase-bom:26.1.0 库一起提供的。
    • 它们用于不同的构建变体,即debugAndroidTestCompileClasspath 表示 Android 自动化测试的调试构建的依赖项,debugCompileClasspath 表示源文件的调试构建的依赖项(不包括测试)。不同版本的firebase的原因可能是因为您可能为源文件和android测试定义了不同的firebase版本。
    【解决方案2】:

    试试

    plugin{
       id 'com.android.application'
       id 'com.google.gms.google-services'
     }
    

    在您的应用级别 gradle 中,而不是 apply plugin

    【讨论】:

    • 当我删除 apply plugin 时,我收到类似 > Could not find method android() for arguments 的错误
    猜你喜欢
    • 2021-12-12
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    相关资源
    最近更新 更多