Skip to content

Commit

Permalink
Using AGP's AarToClassTransform class (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzo authored Jan 16, 2025
1 parent f470e37 commit 8fbad53
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.gradle.jvmargs=-Xmx2g
org.gradle.parallel=true
org.gradle.caching=true
org.gradle.configuration-cache=true
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ junit-engine = { module = "org.junit.jupiter:junit-jupiter-engine" }
junit-params = { module = "org.junit.jupiter:junit-jupiter-params" }

[plugins]
android = { id = "com.android.application", version = "8.7.2" }
android = { id = "com.android.application", version = "8.7.3" }
importClasses = { id = "io.github.gmazzo.importclasses", version = "1.1.0" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-samReceiver = { id = "org.jetbrains.kotlin.plugin.sam.with.receiver", version.ref = "kotlin" }
gradle-pluginPublish = { id = "com.gradle.plugin-publish", version = "1.3.0" }
17 changes: 17 additions & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
plugins {
alias(libs.plugins.importClasses)
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.kotlin.samReceiver)
alias(libs.plugins.gradle.pluginPublish)
Expand Down Expand Up @@ -29,9 +30,25 @@ gradlePlugin {
}
}

importClasses {
repackageTo = "io.github.gmazzo.gradle.aar2jar.agp"
keep("com.android.build.gradle.internal.dependency.AarToClassTransform")
keep("com.android.build.gradle.internal.dependency.AarToClassTransform\$Params")
include("**/*.class")
option("-keepclassmembers enum com.android.resources.ResourceType { *; }")
option("-keepclassmembers,allowobfuscation enum * { *; }")
}

dependencies {
fun plugin(plugin: Provider<PluginDependency>) =
plugin.get().run { "$pluginId:$pluginId.gradle.plugin:$version" }

compileOnly(gradleKotlinDsl())

importClasses(plugin(libs.plugins.android))
importClassesLibraries(plugin(libs.plugins.kotlin.jvm))
importClassesLibraries(gradleApi())

testImplementation(gradleKotlinDsl())
testImplementation(gradleTestKit())

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package io.github.gmazzo.gradle.aar2jar

import io.github.gmazzo.gradle.aar2jar.agp.AarToClassTransform
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.type.ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE
import org.gradle.api.artifacts.type.ArtifactTypeDefinition.JAR_TYPE
import org.gradle.api.attributes.LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE
import org.gradle.api.attributes.Usage
import org.gradle.api.attributes.Usage.JAVA_API
import org.gradle.api.attributes.Usage.JAVA_RUNTIME
import org.gradle.api.attributes.Usage.USAGE_ATTRIBUTE
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.kotlin.dsl.add
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.registerTransform
import org.gradle.kotlin.dsl.the

Expand Down Expand Up @@ -39,10 +45,17 @@ class AAR2JARPlugin : Plugin<Project> {
compatibilityRules.add(AARCompatibilityRule::class)
}

registerTransform(AAR2JARTransform::class) {
fun registerTransform(usage: Usage) = registerTransform(AarToClassTransform::class) {
from.attribute(USAGE_ATTRIBUTE, usage)
from.attribute(ARTIFACT_TYPE_ATTRIBUTE, AAR_TYPE)
to.attribute(USAGE_ATTRIBUTE, usage)
to.attribute(ARTIFACT_TYPE_ATTRIBUTE, JAR_TYPE)
parameters.generateRClassJar.value(true)
parameters.forCompileUse.value(usage.name == JAVA_API)
}

registerTransform(objects.named(JAVA_API))
registerTransform(objects.named(JAVA_RUNTIME))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,26 @@ class AAR2JARPluginTest {
}

@ParameterizedTest(name = "{0}")
@CsvSource("runtimeClasspath", "compileClasspath")
fun `can resolve AAR dependencies`(configuration: String): Unit = with(ProjectBuilder.builder().build()) {
gradleIssue31862Workaround()
@CsvSource(
"compileClasspath, runtime-1.1.1-api.jar|common-1.1.1.jar|support-annotations-26.1.0.jar",
"runtimeClasspath, runtime-1.1.1-runtime.jar|common-1.1.1.jar|support-annotations-26.1.0.jar",
)
fun `can resolve AAR dependencies`(configuration: String, expectedArtifacts: String): Unit =
with(ProjectBuilder.builder().build()) {
gradleIssue31862Workaround()

apply(plugin = "java")
apply(plugin = "io.github.gmazzo.aar2jar")
apply(plugin = "java")
apply(plugin = "io.github.gmazzo.aar2jar")

repositories.mavenCentral()
repositories.google()
repositories.mavenCentral()
repositories.google()

dependencies.add("implementation", "android.arch.core:runtime:1.1.1")
dependencies.add("implementation", "android.arch.core:runtime:1.1.1")

val resolvedArtifacts = configurations[configuration].files.mapTo(mutableSetOf()) { it.name }
val resolvedArtifacts = configurations[configuration].files.mapTo(mutableSetOf()) { it.name }

assertEquals(
setOf("runtime-1.1.1.jar", "common-1.1.1.jar", "support-annotations-26.1.0.jar"),
resolvedArtifacts,
)
}
assertEquals(expectedArtifacts.split('|').toSet(), resolvedArtifacts)
}

// TODO workaround for https://github.com/gradle/gradle/issues/31862
private fun gradleIssue31862Workaround() = ProblemsProgressEventEmitterHolder.init(object : InternalProblems {
Expand Down

0 comments on commit 8fbad53

Please sign in to comment.