为什么 Kotlin 库模块不能依赖于 Android 库模块?

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

我正在尝试拥有这个依赖图:

表示(Android 模块)-> 域(Kotlin 模块)-> 数据(Android 模块)

每当我使域依赖于数据

implementation(projects.data)
时,它都会抛出这样的错误:

Could not determine the dependencies of task ':domain:compileJava'.
> Could not resolve all task dependencies for configuration ':domain:compileClasspath'.
   > Could not resolve project :data.
     Required by:
         project :domain
      > No matching variant of project :data was found. The consumer was configured to find a library for use during compile-time, compatible with Java 21, preferably in the form of class files, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' but:
          - Variant 'debugApiElements' declares a library for use during compile-time, preferably optimized for Android:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm'
              - Other compatible attributes:
                  - Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)
                  - Doesn't say anything about its target Java version (required compatibility with Java 21)
          - Variant 'debugRuntimeElements' declares a library for use during runtime, preferably optimized for Android:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm'
              - Other compatible attributes:
                  - Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)
                  - Doesn't say anything about its target Java version (required compatibility with Java 21)
          - Variant 'debugSourcesElements' declares a component for use during runtime, packaged as a jar, preferably optimized for Android, and its dependencies declared externally:     
              - Incompatible because this component declares documentation, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' and the consumer needed a library, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm'
              - Other compatible attribute:
                  - Doesn't say anything about its target Java version (required compatibility with Java 21)
          - Variant 'releaseApiElements' declares a library for use during compile-time, preferably optimized for Android:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm'
              - Other compatible attributes:
                  - Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)
                  - Doesn't say anything about its target Java version (required compatibility with Java 21)
          - Variant 'releaseRuntimeElements' declares a library for use during runtime, preferably optimized for Android:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm'
              - Other compatible attributes:
                  - Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)
                  - Doesn't say anything about its target Java version (required compatibility with Java 21)
          - Variant 'releaseSourcesElements' declares a component for use during runtime, packaged as a jar, preferably optimized for Android, and its dependencies declared externally:   
rary, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm'
              - Other compatible attribute:
                  - Doesn't say anything about its target Java version (required compatibility with Java 21)

域的build.gradle:

plugins {
    alias(libs.plugins.jetbrains.kotlin.jvm)
}

java {
    sourceCompatibility = JavaVersion.VERSION_21
    targetCompatibility = JavaVersion.VERSION_21
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
    kotlinOptions {
        jvmTarget = "21"
    }
}


dependencies {
    implementation(projects.data)
}

数据的build.gradle

plugins {
    alias(libs.plugins.android.library)
    alias(libs.plugins.jetbrains.kotlin.android)
}

android {
    namespace = "com.your.mom"
    compileSdk = 34

    defaultConfig {
        minSdk = 28

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

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_21
        targetCompatibility = JavaVersion.VERSION_21
    }
    kotlinOptions {
        jvmTarget = "21"
    }
}

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.appcompat)
    implementation(libs.material)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
}
android gradle multi-module
1个回答
0
投票

只是为了防止人们做...令人困惑的事情。

如果 android lib 模块 (M) 会公开某些 android 方法/调用 Kotlin 模块 (K),那么我们可以使用并调用 K 模块中的 android 方法,这会将其转换为 kotlin 模块,可以使用 android 资源/ 方法(基本上是一个 Android Lib 模块)

© www.soinside.com 2019 - 2024. All rights reserved.