Gradle Plugin - dependency on Java module also used in main project
I want that my gradle plugin depends on some common code that can be used inside the gradle plugin as well as inside a android library.
Is there a way to share plain java/kotlin code between a gradle plugin and an android library?
When I try to add a module to the gradle plugin I get "could ':...' could not be found in project" error... I tried different combinations of includeBuild
and include
but they don't solve the issue...
Example:
Gradle Plugin:
plugins {
`kotlin-dsl`
`java-gradle-plugin`
`maven-publish`
}
dependencies {
// fails
implementation(project(":Library:Plugin:Shared"))
//implementation(project(":library:plugin:shared"))
}
Library Module:
plugins {
id("com.android.library")
id("kotlin-android")
id("kotlin-parcelize")
id("maven-publish")
}
android {
// ...
}
dependencies {
api(project(":Library:Plugin:Shared"))
}
Shared Module:
repositories {
mavenCentral()
}
plugins {
id("org.jetbrains.kotlin.jvm")
`java-library`
`maven-publish`
}
dependencies {
}
settings.gradle.kts:
pluginManagement {
includeBuild(File("library/plugin/gradle"))
}
include(":Library:Plugin:Shared")
project(":Library:Plugin:Shared").projectDir = file("library/plugin/shared")
include(":Library:Plugin:Gradle")
project(":Library:Plugin:Gradle").projectDir = file("library/plugin/gradle")
EXAMPLE SETUP
1) root/gradle-plugin/build.gradle.kts
repositories {
mavenCentral()
}
plugins {
`kotlin-dsl-base`
`java-gradle-plugin`
`maven-publish`
}
dependencies {
//implementation(":gradle-shared")
// following does not work inside a gradle plugin module...
// it works in other modules though
implementation(project(":Library:Plugin:Shared"))
}
gradlePlugin {
plugins {
create("changelog-utils") {
id = "changelog-utils"
implementationClass = "<path>.ClassLoaderPlugin"
}
}
}
2) root/gradle-shared/build.gradle.kts
repositories {
mavenCentral()
}
plugins {
`kotlin-dsl-base`
`java-library`
//`maven-publish`
}
3) root/settings.gradle.kts
// --------------
// Gradle Plugin
// --------------
includeBuild("gradle-plugin")
// --------------
// Shared Code (Plugin + Library)
// --------------
include(":MyLibrary:Plugin:Shared")
project(":MyLibrary:Plugin:Shared").projectDir = file("gradle-shared")
Answers
To share common code between a Gradle plugin and an Android library, you can structure your project in such a way that the shared code resides in a separate module. Here's how you can set up your project to achieve this:
-
Create a Shared Module: This module will contain the common code that you want to share between your Gradle plugin and Android library.
-
Update Gradle Plugin and Android Library to Depend on the Shared Module: Both the Gradle plugin and Android library should depend on the shared module to access the common code.
Here's how you can structure your project:
Project Structure
root/
|-- gradle-plugin/
| |-- build.gradle.kts
| |-- src/
|-- gradle-shared/ (Shared Module)
| |-- build.gradle.kts
| |-- src/
|-- android-library/
| |-- build.gradle.kts
| |-- src/
|-- settings.gradle.kts
1. Shared Module (gradle-shared/build.gradle.kts
)
This module contains the common code that you want to share.
plugins {
`java-library`
}
repositories {
mavenCentral()
}
dependencies {
// Define dependencies if needed
}
// Optionally, specify any other configuration
2. Gradle Plugin (gradle-plugin/build.gradle.kts
)
This module represents your Gradle plugin.
plugins {
`java-gradle-plugin`
`maven-publish`
}
dependencies {
implementation(project(":gradle-shared"))
// Other dependencies and configurations for the plugin
}
gradlePlugin {
// Plugin configuration
}
3. Android Library (android-library/build.gradle.kts
)
This module represents your Android library.
plugins {
id("com.android.library")
id("kotlin-android")
id("kotlin-parcelize")
id("maven-publish")
}
android {
// Android-specific configurations
}
dependencies {
implementation(project(":gradle-shared"))
// Other dependencies and configurations for the Android library
}
4. Settings (settings.gradle.kts
)
This file defines the structure of your multi-module project.
include(":gradle-plugin")
include(":gradle-shared")
include(":android-library")
By structuring your project in this way, both your Gradle plugin and Android library can depend on the shared module, allowing them to access the common code. Ensure that the package structure and dependencies are set up correctly in the shared module to be usable by both types of projects.