I'm using a Gradle Platform (specifically java-platform
) to control my transitive dependencies. I have the following modules:
platform/build.gradle
plugins {
id 'java-platform'
}
dependencies {
constraints {
api('foo:bar:123')
}
}
app/build.gradle
:
plugins {
id 'com.android.application'
}
android {
buildToolsVersion rootProject.buildToolsVersion
compileSdkVersion rootProject.compileSdkVersion
defaultConfig {
maxSdkVersion rootProject.maxSdkVersion
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
versionCode 1
versionName '1.0'
}
compileOptions {
sourceCompatibility rootProject.sourceCompatibility
targetCompatibility rootProject.targetCompatibility
}
}
dependencies {
api(platform(project(':platform')))
annotationProcessor('foo:bar')
}
But when I run ./gradlew :app:dependencies
, gradle fails to find my annotationProcessor
version:
debugAnnotationProcessorClasspath - Resolved configuration for annotation-processor for variant: debug
\--- foo:bar FAILED
If I specify the version explicitly, then everything works:
app/build.gradle
:
plugins {
id 'com.android.application'
}
android {
buildToolsVersion rootProject.buildToolsVersion
compileSdkVersion rootProject.compileSdkVersion
defaultConfig {
maxSdkVersion rootProject.maxSdkVersion
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
versionCode 1
versionName '1.0'
}
compileOptions {
sourceCompatibility rootProject.sourceCompatibility
targetCompatibility rootProject.targetCompatibility
}
}
dependencies {
annotationProcessor('foo:bar:123')
}
And then I see the version in ./gradlew :app:dependencies
:
debugAnnotationProcessorClasspath - Resolved configuration for annotation-processor for variant: debug
\--- foo:bar:123
CodePudding user response:
I have to specify a Gradle Platform for every configuration I want to use it with, so
annotationProcessor(platform(project(':platform')))
This gradle issue provided a clue: https://github.com/gradle/gradle/issues/8723
These module definitions worked:
platform/build.gradle
plugins {
id 'java-platform'
}
dependencies {
constraints {
api('foo:bar:123')
}
}
app/build.gradle
:
plugins {
id 'com.android.application'
}
android {
buildToolsVersion rootProject.buildToolsVersion
compileSdkVersion rootProject.compileSdkVersion
defaultConfig {
maxSdkVersion rootProject.maxSdkVersion
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
versionCode 1
versionName '1.0'
}
compileOptions {
sourceCompatibility rootProject.sourceCompatibility
targetCompatibility rootProject.targetCompatibility
}
}
dependencies {
annotationProcessor(platform(project(':platform')))
annotationProcessor('foo:bar')
}
CodePudding user response:
The annotationProcessor
configuration represents a different dependency graph from your application classpath. It's completely independent, because it's dependencies which are "compiler plugins". So dependencies declared as implementation ...
will not be found on the annotationProcessor
path, and they must not. There's nothing different for your platform
dependency here, so it's not because you declare a platform dependency on implementation
that it will magically appear on the annotationProcessor
path: there's no reason to do this.
Long story short: you need to declare your platform as a dependency of annotationProcessor
too. Note that in absolute, there's no reason why you'd use the same dependency versions for your compiler classpath (the annotationProcessor
path) and your application classpath: an annotation processor you use may for example depend on a different Guava version than your app, so it's a good idea to keep the platforms separate.