Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ gr8 {
val compileOnlyDependenciesForGr8 = configurations.create("compileOnlyDependenciesForGr8") {
attributes {
attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, FilterTransform.artifactType)
}
attributes {
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named<Usage>(Usage.JAVA_API))
}
}
Expand Down
89 changes: 44 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ dependencies {
* Create a separate configuration to resolve compileOnly dependencies.
* You can skip this if you have no compileOnly dependencies.
*/
val compileOnlyDependencies: Configuration = configurations.create("compileOnlyDependencies")
compileOnlyDependencies.extendsFrom(configurations.getByName("compileOnly"))
val compileOnlyDependencies by configurations.creating {
extendsFrom(configurations.compileOnly.get())
}

gr8 {
val shadowedJar = create("gr8") {
// program jars are included in the final shadowed jar
addProgramJarsFrom(configurations.getByName("runtimeClasspath"))
addProgramJarsFrom(tasks.getByName("jar"))
addProgramJarsFrom(configurations.runtimeClasspath.get())
addProgramJarsFrom(tasks.named<Jar>("jar"))
// classpath jars are only used by R8 for analysis but are not included in the
// final shadowed jar.
addClassPathJarsFrom(compileOnlyDependencies)
Expand Down Expand Up @@ -85,60 +86,58 @@ Using Gr8 to shadow dependencies in Gradle plugin is a typical use case but requ
To work around this, you can use, `removeGradleApiFromApi()`, `registerTransform()` and custom configurations:

```kotlin
val shadowedDependencies = configurations.create("shadowedDependencies")
val shadowedDependencies by configurations.creating

val compileOnlyDependencies: Configuration = configurations.create("compileOnlyDependencies") {
val compileOnlyDependencies by configurations.creating {
attributes {
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named<Usage>(Usage.JAVA_API))
}
// this attribute is needed to filter out some classes, see https://issuetracker.google.com/u/1/issues/380805015
attributes {

// this attribute is needed to filter out some classes, see https://issuetracker.google.com/u/1/issues/380805015
attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, FilterTransform.artifactType)
}

extendsFrom(configurationscompileOnly.get())
}
compileOnlyDependencies.extendsFrom(configurations.getByName("compileOnly"))

dependencies {
add(shadowedDependencies.name, "com.squareup.okhttp3:okhttp:4.9.0")
add("compileOnly", gradleApi())
shadowedDependencies("com.squareup.okhttp3:okhttp:4.9.0")
compileOnly(gradleApi())
// More dependencies here
}

if (shadow) {
gr8 {
create("default") {
val shadowedJar = create("default") {
addProgramJarsFrom(shadowedDependencies)
addProgramJarsFrom(tasks.getByName("jar"))
// classpath jars are only used by R8 for analysis but are not included in the
// final shadowed jar.
addClassPathJarsFrom(compileOnlyDependencies)

proguardFile("rules.pro")

// for more information about the different options, refer to their matching R8 documentation
// at https://r8.googlesource.com/r8#running-r8

// See https://issuetracker.google.com/u/1/issues/380805015 for why this is required
registerFilterTransform(listOf(".*/impldep/META-INF/versions/.*"))
}

removeGradleApiFromApi()

// Optional: replace the regular jar with the shadowed one in the publication
replaceOutgoingJar(shadowedJar)

// Or if you prefer the shadowed jar to be a separate variant in the default publication
// The variant will have `org.gradle.dependency.bundling = shadowed`
addShadowedVariant(shadowedJar)

// Allow to compile the module without exposing the shadowedDependencies downstream
configurations.getByName("compileOnly").extendsFrom(shadowedDependencies)
configurations.getByName("testImplementation").extendsFrom(shadowedDependencies)
val shadowedJar = create("default") {
addProgramJarsFrom(shadowedDependencies)
addProgramJarsFrom(tasks.named<Jar>("jar"))
// classpath jars are only used by R8 for analysis but are not included in the
// final shadowed jar.
addClassPathJarsFrom(compileOnlyDependencies)

proguardFile("rules.pro")

// for more information about the different options, refer to their matching R8 documentation
// at https://r8.googlesource.com/r8#running-r8

// See https://issuetracker.google.com/u/1/issues/380805015 for why this is required
registerFilterTransform(listOf(".*/impldep/META-INF/versions/.*"))
}

removeGradleApiFromApi()

// Optional: replace the regular jar with the shadowed one in the publication
replaceOutgoingJar(shadowedJar)

// Or if you prefer the shadowed jar to be a separate variant in the default publication
// The variant will have `org.gradle.dependency.bundling = shadowed`
addShadowedVariant(shadowedJar)

// Allow to compile the module without exposing the shadowedDependencies downstream
configurations.compileOnly.get().extendsFrom(shadowedDependencies)
configurations.testImplementation.get().extendsFrom(shadowedDependencies)
}
} else {
configurations.getByName("implementation").extendsFrom(shadowedDependencies)
configurations.implementation.get().extendsFrom(shadowedDependencies)
}
```

Expand All @@ -162,13 +161,13 @@ If you want to keep them, you need to keep `kotlin.Metadata` and `kotlin.Unit`:

You can specify the version of the java runtime to use with `systemClassesToolchain`:

```
```kotlin
gr8 {
val shadowedJar = create("gr8") {
proguardFile("rules.pro")
addProgramJarsFrom(configurations.getByName("runtimeClasspath"))
addProgramJarsFrom(configurations.runtimeClasspath)
systemClassesToolchain {
languageVersion.set(JavaLanguageVersion.of(11))
languageVersion = JavaLanguageVersion.of(11)
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions gr8-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ plugins {
val filteredClasspathDependencies: Configuration = configurations.create("filteredClasspathDependencies") {
attributes {
attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, FilterTransform.artifactType)
}
attributes {
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named<Usage>(Usage.JAVA_API))
}
}
Expand Down
2 changes: 0 additions & 2 deletions test-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ dependencies {
val compileOnlyDependenciesForGr8: Configuration = configurations.create("compileOnlyDependenciesForGr8") {
attributes {
attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, FilterTransform.artifactType)
}
attributes {
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named<Usage>(Usage.JAVA_API))
}
}
Expand Down
Loading