首页 > 解决方案 > Gradle C++ 依赖项可以针对不同的应用程序进行不同的设置吗?

问题描述

我在同一个项目中有一些应用程序具有相同的依赖项,这些依赖项也在构建中,其中一些需要它们作为静态的,而另一些需要它们共享。我根据https://github.com/gradle/gradle-native/issues/1017上的答案构建了一个非常基本的错误展示,它实际上得到了相同的错误。这是一个基本项目,包含 2 个库:1 和 :2,以及一个位于 :app 的 cpp 应用程序。这吐出以下错误:

Could not determine the dependencies of task ':app:installDebug'.
> Could not resolve all task dependencies for configuration ':app:nativeRuntimeDebug'.
   > Could not resolve project :1.
     Required by:
         project :app
      > Module 'gradletest:1' has been rejected:
           Cannot select module with conflict on capability 'gradletest:1:unspecified' also provided by [gradletest:1:unspecified(debugSharedRuntimeElements), gradletest:1:unspecified(debugStaticRuntimeElements)]
   > Could not resolve project :1.
     Required by:
         project :app > project :2
      > Module 'gradletest:1' has been rejected:
           Cannot select module with conflict on capability 'gradletest:1:unspecified' also provided by [gradletest:1:unspecified(debugSharedRuntimeElements), gradletest:1:unspecified(debugStaticRuntimeElements)]

这是我的样本,我必须打破它。我很确定我遵循了 github 中回答的所有内容,并仔细阅读了 gradle 文档以获取我能找到的所有相关内容。在我目前的非 MVP 情况下,我将有一个 :app2 将 :1 和 :2 链接为共享库,因此仅将 :1 和 :2 设置为静态并不能解决问题。

1/build.gradle.kts

plugins {
    `cpp-library`
    `cpp-unit-test`
}

library {
    linkage.set(listOf(Linkage.STATIC, Linkage.SHARED))
}

2/build.gradle.kts

plugins {
    `cpp-library`
    `cpp-unit-test`
}

library {
    linkage.set(listOf(Linkage.STATIC, Linkage.SHARED))
    dependencies {
        implementation(project(":1"))
    }
}

应用程序/build.gradle.kts

plugins {
    `cpp-application`
    `cpp-unit-test`
}

dependencies {
    implementation(project(":1")) {
        attributes { attribute(Attribute.of("org.gradle.native.linkage", Linkage::class.java), Linkage.STATIC) }
    }
    implementation(project(":2")) {
        attributes { attribute(Attribute.of("org.gradle.native.linkage", Linkage::class.java), Linkage.STATIC) }
    }
}

application {
}

这是在 Gradle 5.4.1 上完成的,不幸的是,要获得更新非常困难,所以我无法测试是否是旧版本的问题。

标签: gradlekotlindependenciesstatic-linkingdynamic-linking

解决方案


推荐阅读