Jeff Lockhart
05/09/2023, 8:37 PMcompileOnly
dependency in an intermediate android + jvm source set. The android dependency is what gets compiled for the android target. With the default dependency priority order, the IDE shows a false positive error for a conflicting API between the two libraries.Jeff Lockhart
05/09/2023, 8:39 PMephemient
05/09/2023, 10:42 PMephemient
05/09/2023, 10:46 PMsettings.gradle.kts
, change dependencyResolutionManagement
to dependencies
if you're putting it in build.gradle.kts
) this should cause Gradle to redirect the dependency java→android or android→java depending on the target, and IntelliJ will use what Gradle ended up resolvingJeff Lockhart
05/10/2023, 2:56 AMJeff Lockhart
05/20/2023, 4:30 AMephemient
05/20/2023, 4:57 AMephemient
05/21/2023, 4:36 AMJeff Lockhart
05/21/2023, 10:02 PMandroid-ktx
Kotlin extensions). There's another closed-source ee
module, which I'm assuming looks similar to this ce
module (I don't work for Couchbase), with the same java
, android
, and android-ktx
modules with the additional enterprise APIs.Jeff Lockhart
05/21/2023, 10:06 PMjava
module, adding the android
variant with a dependency on the android
module's artifact. If I understand correctly how your injected workaround code works, it sees another variant published in the Gradle Module Metadata with the same attribute with a different platform value, so it then excludes this variant's dependencies, which is the other platform's artifact?Jeff Lockhart
05/21/2023, 10:09 PMJeff Lockhart
05/21/2023, 10:10 PM:ce:java:ce_java
build.gradle:
def androidApiConfig = configurations.create("androidApi").tap {
canBeResolved = false
canBeConsumed = true
attributes {
attribute(TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE, objects.named(TargetJvmEnvironment.class, TargetJvmEnvironment.ANDROID))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.androidJvm)
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.class, Usage.JAVA_API))
}
}
dependencies {
androidApi("com.couchbase.lite:couchbase-lite-android:$version")
}
def androidRuntimeConfig = configurations.create("androidRuntime").tap {
extendsFrom(androidApiConfig)
canBeResolved = false
canBeConsumed = true
attributes {
attribute(TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE, objects.named(TargetJvmEnvironment.class, TargetJvmEnvironment.ANDROID))
attribute(KotlinPlatformType.attribute, KotlinPlatformType.androidJvm)
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.class, Usage.JAVA_RUNTIME))
}
}
(components.java as AdhocComponentWithVariants).with {
addVariantsFromConfiguration(androidApiConfig) {
mapToMavenScope("compile")
}
addVariantsFromConfiguration(androidRuntimeConfig) {
mapToMavenScope("runtime")
}
}
Jeff Lockhart
05/21/2023, 10:12 PM{
"name": "androidApi",
"attributes": {
"org.gradle.jvm.environment": "android",
"org.gradle.usage": "java-api",
"org.jetbrains.kotlin.platform.type": "androidJvm"
},
"dependencies": [
{
"group": "com.couchbase.lite",
"module": "couchbase-lite-android",
"version": {
"requires": "3.1.0-SNAPSHOT"
}
}
]
},
{
"name": "androidRuntime",
"attributes": {
"org.gradle.jvm.environment": "android",
"org.gradle.usage": "java-runtime",
"org.jetbrains.kotlin.platform.type": "androidJvm"
},
"dependencies": [
{
"group": "com.couchbase.lite",
"module": "couchbase-lite-android",
"version": {
"requires": "3.1.0-SNAPSHOT"
}
}
]
}
Jeff Lockhart
05/21/2023, 10:17 PMorg.gradle.jvm.environment
and org.jetbrains.kotlin.platform.type
attributes. The java
module adds both, although the android
module is only adding org.jetbrains.kotlin.platform.type
to the -published
configurations that get published. The equivalent configurations without the -published
suffix also includes the other attributes, as well as a few others that are removed from the published version for some reason. I'm assuming this is something the Android gradle plugin is doing, and I'm not sure how to change this behavior. But it does seem Gradle can disambiguate using either of these attributes, as I tested the workaround with the other attribute as well.Jeff Lockhart
05/21/2023, 10:22 PMjava
module (I didn't get a build error or anything about the dependency not existing, even after deleting the SNAPSHOT build from local maven). I just need to figure out how to do the same thing for the android
module. I ran into this error trying similar code, but with the components.release
component:
A problem occurred configuring project 'ceandroid:ce_android'.
> No signature of method: org.jetbrains.kotlin.gradle.plugin.mpp.AbstractKotlinTarget$buildAdhocComponentsFromKotlinVariants$1$2.addVariantsFromConfiguration() is applicable for argument types: (org.gradle.api.internal.artifacts.configurations.DefaultConfiguration_Decorated...) values: [configuration 'ceandroidce androidjvmApi', build_826wsussfhbm4sf80h1ug05zw$_run_closure18$_closure74@1790be3f]So it seems
components.release
is not an AdhocComponentWithVariants
with the ability to call addVariantsFromConfiguration()
. So far I haven't been able to find an example of how to do this in an Android module.Jeff Lockhart
05/22/2023, 4:10 AMAdhocComponentWithVariants
interface, but AGP is no longer doing this.Jeff Lockhart
05/22/2023, 4:37 AMadhocVariant
that it wraps.Jeff Lockhart
05/22/2023, 4:57 PMAdhocComponentWithVariants
captured variable reference via reflection, this works to publish the Java variant for the Android module too.
def adhocField = components.release.class.getDeclaredFields()
.find { it.getType() == AdhocComponentWithVariants }
adhocField.setAccessible(true)
AdhocComponentWithVariants adhocComponent = adhocField.get(components.release)
adhocComponent.addVariantsFromConfiguration(...) {
...
Jeff Lockhart
05/22/2023, 4:59 PM{
"name": "androidApi",
"attributes": {
"org.gradle.jvm.environment": "android",
"org.gradle.usage": "java-api",
"org.jetbrains.kotlin.platform.type": "androidJvm"
},
"dependencies": [
{
"group": "com.couchbase.lite",
"module": "couchbase-lite-android",
"version": {
"requires": "3.1.0-SNAPSHOT"
}
}
]
},
{
"name": "androidRuntime",
"attributes": {
"org.gradle.jvm.environment": "android",
"org.gradle.usage": "java-runtime",
"org.jetbrains.kotlin.platform.type": "androidJvm"
},
"dependencies": [
{
"group": "com.couchbase.lite",
"module": "couchbase-lite-android",
"version": {
"requires": "3.1.0-SNAPSHOT"
}
}
]
}
Jeff Lockhart
05/22/2023, 5:00 PMJeff Lockhart
05/22/2023, 5:19 PMJeff Lockhart
05/23/2023, 4:14 PMJeff Lockhart
05/23/2023, 4:27 PMavailable-at
to point to the location of the published artifact's files, rather than making the artifact a dependency of the variant. This would keep it out of the .pom too and be more correct.
So instead of:
{
"name": "javaApi",
"attributes": {
"org.gradle.jvm.environment": "standard-jvm",
"org.gradle.usage": "java-api",
"org.jetbrains.kotlin.platform.type": "jvm"
},
"dependencies": [
{
"group": "com.couchbase.lite",
"module": "couchbase-lite-java",
"version": {
"requires": "3.1.0-SNAPSHOT"
}
}
]
},
{
"name": "javaRuntime",
"attributes": {
"org.gradle.jvm.environment": "standard-jvm",
"org.gradle.usage": "java-runtime",
"org.jetbrains.kotlin.platform.type": "jvm"
},
"dependencies": [
{
"group": "com.couchbase.lite",
"module": "couchbase-lite-java",
"version": {
"requires": "3.1.0-SNAPSHOT"
}
}
]
}
it'd be:
{
"name": "javaApi",
"attributes": {
"org.gradle.jvm.environment": "standard-jvm",
"org.gradle.usage": "java-api",
"org.jetbrains.kotlin.platform.type": "jvm"
},
"available-at": {
"url": "../../couchbase-lite-java/3.1.0-SNAPSHOT/couchbase-lite-java-3.1.0-SNAPSHOT.module",
"group": "com.couchbase.lite",
"module": "couchbase-lite-java",
"version": "3.1.0-SNAPSHOT"
}
},
{
"name": "javaRuntime",
"attributes": {
"org.gradle.jvm.environment": "standard-jvm",
"org.gradle.usage": "java-runtime",
"org.jetbrains.kotlin.platform.type": "jvm"
},
"available-at": {
"url": "../../couchbase-lite-java/3.1.0-SNAPSHOT/couchbase-lite-java-3.1.0-SNAPSHOT.module",
"group": "com.couchbase.lite",
"module": "couchbase-lite-java",
"version": "3.1.0-SNAPSHOT"
}
}
I'm not sure if there's an API for this though.