重复类androidx.lifecycle.ViewModelLazy,仅在运行测试时抛出异常

问题描述 投票:0回答:1

我有一个带有 3 个模块的 Android 应用程序:应用程序、记录器和共享。 共享模块有我想要执行的 androidTest 单元测试。但是,如果我尝试执行测试,我会得到异常:

Duplicate class androidx.lifecycle.ViewModelLazy found in modules jetified-lifecycle-viewmodel-ktx-2.2.0-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0) and lifecycle-viewmodel-2.4.0-runtime (androidx.lifecycle:lifecycle-viewmodel:2.4.0)
Duplicate class androidx.lifecycle.ViewModelProviderKt found in modules jetified-lifecycle-viewmodel-ktx-2.2.0-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0) and lifecycle-viewmodel-2.4.0-runtime (androidx.lifecycle:lifecycle-viewmodel:2.4.0)

这种情况不会发生,如果我自己构建应用程序,那么它必须以某种方式与 testImplementations 相关。但我使用的唯一 testImplementation 是 jUnit,我无法想象它如何使用生命周期视图模型。共享模块和记录器模块都不使用生命周期依赖项,我不打算更改它,因为它们不应该弄乱这些事情。 你们中有人知道为什么会出现此问题以及如何解决它吗? 我还尝试在应用程序模块中声明视图模型依赖项(ktx 和 plain),但由于测试不是该模块的一部分,因此没有帮助。

build.gradle(项目):

buildscript {

    repositories {
        mavenCentral()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.4.2'

        // 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.5'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.5.2'
        classpath 'com.google.android.gms:oss-licenses-plugin:0.10.4'
        classpath "io.realm:realm-gradle-plugin:10.9.0"
    }
}

allprojects {
    repositories {
        mavenCentral()
        maven { url "https://jitpack.io" }
        google()
        jcenter() // required for android-adapters realm recyclerview adapter library
    }
    configurations {
        // Exclude XmlPullParser from Smack dependencies, as its now provided by Android
        all {
            exclude group: 'xpp3', module: 'xpp3_min'
            exclude group: 'xpp3', module: 'xpp3'
        }
    }
}

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

build.gradle(应用程序):

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.firebase.crashlytics'

android {

    packagingOptions {// Log4J throws a META-INF/DEPENDENCIES error during compile FIXME
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/DEPENDENCIES'
    }
    compileSdkVersion 33
    buildToolsVersion '30.0.3'
    defaultConfig {
        //Default config
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
    repositories {
        flatDir {
            dirs 'src/main/libs'
        }
    }
    packagingOptions {
        exclude 'META-INF/proguard/androidx-annotations.pro'
    }

    dataBinding {
        enabled = true
    }

}
dependencies {
    implementation project(path: ':shared')

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.fragment:fragment-ktx:1.3.0'
    implementation 'de.hdodenhof:circleimageview:3.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.gridlayout:gridlayout:1.0.0'
    implementation 'androidx.gridlayout:gridlayout:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.exifinterface:exifinterface:1.0.0'
    kapt 'androidx.lifecycle:lifecycle-compiler:2.4.0'

    implementation 'com.google.android.material:material:1.4.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.google.guava:guava:27.1-android'
    implementation 'com.google.firebase:firebase-iid:21.1.0'

    implementation 'com.google.firebase:firebase-crashlytics:17.4.0'
    implementation 'com.google.firebase:firebase-analytics:18.0.2'
    implementation 'com.google.firebase:firebase-analytics-ktx:18.0.2'
    implementation 'com.google.firebase:firebase-crashlytics-ktx:17.4.0'

    debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1'

    implementation 'com.jakewharton.timber:timber:4.7.1'
    implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'
    implementation 'com.github.tinder:statemachine:0.1.0'
    implementation 'com.github.Kunzisoft:Android-SwitchDateTimePicker:2.0'
    implementation 'de.hdodenhof:circleimageview:3.0.0'
    implementation "ch.acra:acra-core:5.7.0"
    implementation 'me.tatarka.bindingcollectionadapter2:bindingcollectionadapter:4.0.0'
    implementation 'me.tatarka.bindingcollectionadapter2:bindingcollectionadapter-recyclerview:4.0.0'

    implementation 'org.igniterealtime.smack:smack-core:4.4.6'
    implementation 'org.igniterealtime.smack:smack-im:4.4.6'
    implementation 'org.igniterealtime.smack:smack-extensions:4.4.6'
    implementation 'org.igniterealtime.smack:smack-tcp:4.4.6'

    implementation 'com.jakewharton.rxrelay2:rxrelay:2.1.0'
    implementation "io.insert-koin:koin-androidx-scope:$koin_version"
    implementation "io.insert-koin:koin-core:$koin_version"
    implementation "io.insert-koin:koin-android-viewmodel:$koin_version"

    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
    implementation "com.squareup.okhttp3:okhttp-urlconnection:4.9.0"
    implementation 'com.squareup.okhttp3:okhttp:4.9.0'

    implementation 'com.squareup.picasso:picasso:2.71828'

    implementation "io.realm:android-adapters:4.0.0"

    implementation 'androidx.exifinterface:exifinterface:1.0.0'

    implementation 'com.google.android.gms:play-services-oss-licenses:17.0.0'

    implementation project(':kotlinLogger')
    implementation "org.apache.logging.log4j:log4j-api:2.17.2"
    runtimeOnly "com.celeral:log4j2-android:1.0.0"
}


apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.android.gms.oss-licenses-plugin'

//
apply plugin: 'realm-android'

build.gradle(共享):

plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'kotlin-parcelize'
    id 'kotlin-kapt'
    id 'realm-android'
}

android {
    //Namespace definitions, etc.

    buildFeatures {
        dataBinding true
    }

    defaultConfig {
        //Sdk definitions
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation project(path: ':logger')
    implementation "ch.acra:acra-core:5.7.0"

    implementation 'androidx.core:core-ktx:1.10.1'
    implementation 'com.jakewharton.timber:timber:4.7.1'

    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
    implementation 'com.jakewharton.rxrelay2:rxrelay:2.1.0'

    // java.time backport for Android
    implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'
    //Date dependency
    implementation 'net.danlew:android.joda:2.10.6'


    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
    implementation "com.squareup.okhttp3:okhttp-urlconnection:4.9.0"
    implementation 'com.squareup.okhttp3:okhttp:4.9.0'

    implementation 'org.igniterealtime.smack:smack-extensions:4.4.6'
    implementation 'org.igniterealtime.smack:smack-im:4.4.6'
    implementation 'org.igniterealtime.smack:smack-sasl-provided:4.4.6'
    implementation 'org.igniterealtime.smack:smack-tcp:4.4.6'
    implementation 'org.igniterealtime.smack:smack-android:4.4.6'
    implementation 'org.igniterealtime.smack:smack-android-extensions:4.4.6'
    implementation 'org.igniterealtime.smack:smack-experimental:4.4.6'
    implementation 'org.igniterealtime.smack:smack-core:4.4.6'
    implementation 'org.igniterealtime.smack:smack-resolver-dnsjava:4.4.6'

    implementation "io.insert-koin:koin-androidx-scope:$koin_version"
    implementation "io.insert-koin:koin-core:$koin_version"

    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-rxjava2:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.paging:paging-runtime-ktx:$paging_version"
    implementation "androidx.paging:paging-rxjava2-ktx:$paging_version"
    implementation "androidx.sqlite:sqlite-ktx:2.3.1"

    implementation "androidx.security:security-crypto:1.1.0-alpha06"

    implementation "androidx.work:work-runtime:2.8.1"

    implementation 'com.google.firebase:firebase-messaging:23.1.2'
    // specify explicitly since firebase messaging still uses old version
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.legacy:legacy-preference-v14:1.0.0'

    implementation 'com.squareup.picasso:picasso:2.71828'

    implementation 'commons-codec:commons-codec:1.15'
    implementation 'org.apache.commons:commons-lang3:3.12.0'
    implementation 'org.apache.commons:commons-text:1.9'

    androidTestImplementation 'junit:junit:4.13.2'
}

和build.gradle(记录器):

plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.20'
}

android {
    compileSdkVersion 33

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        minSdkVersion 26
        targetSdkVersion 33

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

    implementation 'androidx.appcompat:appcompat:1.4.2'
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"
    implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1'
}

我没有发现任何只在运行测试时发生的情况,非常感谢。

编辑#1: 尝试通过显式声明 ktx 和 default-viewmodel 依赖关系来强制执行依赖关系,我收到了一个新错误,告诉我 debugRuntimeClasspath 正在强制执行 2.2.0:

-----------
* What went wrong:
Execution failed for task ':shared:processDebugAndroidTestManifest'.
> Could not resolve all files for configuration ':shared:debugAndroidTestRuntimeClasspath'.
   > Could not resolve androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0.
     Required by:
         project :shared
      > Cannot find a version of 'androidx.lifecycle:lifecycle-viewmodel-ktx' that satisfies the version constraints:
           Dependency path 'project:shared:unspecified' --> 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
           Constraint path 'project:shared:unspecified' --> 'androidx.lifecycle:lifecycle-viewmodel-ktx:{strictly 2.2.0}' because of the following reason: debugRuntimeClasspath uses version 2.2.0
           Dependency path 'project:shared:unspecified' --> 'project:shared:unspecified' (debugRuntimeElements) --> 'io.insert-koin:koin-androidx-scope:2.2.3' (releaseRuntimePublication) --> 'androidx.activity:activity-ktx:1.1.0' (runtime) --> 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
           Dependency path 'project:shared:unspecified' --> 'project:shared:unspecified' (debugRuntimeElements) --> 'io.insert-koin:koin-androidx-scope:2.2.3' (releaseRuntimePublication) --> 'androidx.fragment:fragment-ktx:1.2.5' (runtime) --> 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'

   > Could not resolve androidx.lifecycle:lifecycle-viewmodel-ktx:{strictly 2.2.0}.
     Required by:
         project :shared
      > Cannot find a version of 'androidx.lifecycle:lifecycle-viewmodel-ktx' that satisfies the version constraints:
           Dependency path 'project:shared:unspecified' --> 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
           Constraint path 'project:shared:unspecified' --> 'androidx.lifecycle:lifecycle-viewmodel-ktx:{strictly 2.2.0}' because of the following reason: debugRuntimeClasspath uses version 2.2.0
           Dependency path 'project:shared:unspecified' --> 'project:shared:unspecified' (debugRuntimeElements) --> 'io.insert-koin:koin-androidx-scope:2.2.3' (releaseRuntimePublication) --> 'androidx.activity:activity-ktx:1.1.0' (runtime) --> 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
           Dependency path 'project:shared:unspecified' --> 'project:shared:unspecified' (debugRuntimeElements) --> 'io.insert-koin:koin-androidx-scope:2.2.3' (releaseRuntimePublication) --> 'androidx.fragment:fragment-ktx:1.2.5' (runtime) --> 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'

   > Could not resolve androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0.
     Required by:
         project :shared > androidx.activity:activity-ktx:1.1.0
         project :shared > androidx.fragment:fragment-ktx:1.2.5
      > Cannot find a version of 'androidx.lifecycle:lifecycle-viewmodel-ktx' that satisfies the version constraints:
           Dependency path 'project:shared:unspecified' --> 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
           Constraint path 'project:shared:unspecified' --> 'androidx.lifecycle:lifecycle-viewmodel-ktx:{strictly 2.2.0}' because of the following reason: debugRuntimeClasspath uses version 2.2.0
           Dependency path 'project:shared:unspecified' --> 'project:shared:unspecified' (debugRuntimeElements) --> 'io.insert-koin:koin-androidx-scope:2.2.3' (releaseRuntimePublication) --> 'androidx.activity:activity-ktx:1.1.0' (runtime) --> 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
           Dependency path 'project:shared:unspecified' --> 'project:shared:unspecified' (debugRuntimeElements) --> 'io.insert-koin:koin-androidx-scope:2.2.3' (releaseRuntimePublication) --> 'androidx.fragment:fragment-ktx:1.2.5' (runtime) --> 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'

我真的很困惑这个 debugRuntimeClasspath 来自哪里。

android android-gradle-plugin dependency-management
1个回答
0
投票

当我更新到Giraffe时,出现了这个错误,然后我更新了lifecycle-viewmodel依赖的版本。现在可以用了

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
© www.soinside.com 2019 - 2024. All rights reserved.