-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1a56ff
commit 9c4b17e
Showing
6 changed files
with
134 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
repositories { | ||
mavenCentral() | ||
google() | ||
} | ||
|
||
dependencies { | ||
implementation libs.plugin.android | ||
implementation libs.plugin.kotlin | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
rootProject.name = 'build-support' | ||
|
||
dependencyResolutionManagement { | ||
versionCatalogs { | ||
libs { | ||
from(files('../gradle/libs.versions.toml')) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import com.android.build.gradle.internal.publishing.AndroidArtifacts | ||
import org.gradle.api.artifacts.transform.TransformParameters | ||
import org.gradle.api.internal.artifacts.transform.UnzipTransform | ||
import org.gradle.work.DisableCachingByDefault | ||
import java.nio.file.Files | ||
import java.util.zip.ZipFile | ||
|
||
configurations { | ||
unzip { | ||
attributes.attribute(AndroidArtifacts.ARTIFACT_TYPE, ArtifactTypeDefinition.DIRECTORY_TYPE) | ||
} | ||
} | ||
|
||
configureAarAsJarForConfiguration("compileOnly") | ||
configureAarAsJarForConfiguration("testImplementation") | ||
|
||
dependencies { | ||
registerTransform(UnzipTransform) { | ||
from.attribute(AndroidArtifacts.ARTIFACT_TYPE, ArtifactTypeDefinition.JAR_TYPE) | ||
to.attribute(AndroidArtifacts.ARTIFACT_TYPE, ArtifactTypeDefinition.DIRECTORY_TYPE) | ||
} | ||
registerTransform(IdentityTransform) { | ||
from.attribute(AndroidArtifacts.ARTIFACT_TYPE, "jar") | ||
to.attribute(AndroidArtifacts.ARTIFACT_TYPE, "aarAsJar") | ||
} | ||
registerTransform(ExtractClassesJarTransform) { | ||
from.attribute(AndroidArtifacts.ARTIFACT_TYPE, "aar") | ||
to.attribute(AndroidArtifacts.ARTIFACT_TYPE, "aarAsJar") | ||
} | ||
} | ||
|
||
def configureAarAsJarForConfiguration(String configuration) { | ||
def aarAsJar = project.configurations | ||
.create("${configuration}AarAsJar") { | ||
canBeResolved = true | ||
attributes.attribute( | ||
AndroidArtifacts.ARTIFACT_TYPE, | ||
"release" | ||
) | ||
attributes.attribute( | ||
Usage.USAGE_ATTRIBUTE, | ||
project.getObjects().named(Usage, Usage.JAVA_API) | ||
) | ||
}.incoming | ||
.artifactView { viewConfiguration -> | ||
viewConfiguration.attributes.attribute(AndroidArtifacts.ARTIFACT_TYPE, "aarAsJar") | ||
} | ||
.files | ||
|
||
project.configurations | ||
.getByName(configuration) | ||
.dependencies | ||
.add(project.dependencies.create(aarAsJar)) | ||
} | ||
|
||
@DisableCachingByDefault | ||
abstract class IdentityTransform implements TransformAction<TransformParameters.None> { | ||
@PathSensitive(PathSensitivity.ABSOLUTE) | ||
@InputArtifact | ||
abstract Provider<FileSystemLocation> getInputArtifact() | ||
|
||
@Override | ||
void transform(TransformOutputs transformOutputs) { | ||
def input = getInputArtifact().get().asFile | ||
if (input.isDirectory()) { | ||
transformOutputs.dir(input) | ||
} else if (input.isFile()) { | ||
transformOutputs.file(input) | ||
} else { | ||
throw new IllegalArgumentException( | ||
"File/directory does not exist: ${input.absolutePath}" | ||
) | ||
} | ||
} | ||
} | ||
|
||
@DisableCachingByDefault | ||
abstract class ExtractClassesJarTransform implements TransformAction<TransformParameters.None> { | ||
@PathSensitive(PathSensitivity.NAME_ONLY) | ||
@InputArtifact | ||
abstract Provider<FileSystemLocation> getPrimaryInput() | ||
|
||
@Override | ||
void transform(TransformOutputs outputs) { | ||
ZipFile zip = null | ||
try { | ||
def file = getPrimaryInput().get().asFile | ||
def aarFilename = file.name.substring(0, file.name.lastIndexOf('.')) | ||
zip = new ZipFile(file) | ||
def entries = zip.entries() | ||
while (entries.hasMoreElements()) { | ||
def item = entries.nextElement() | ||
if (item.isDirectory()) continue | ||
|
||
if (item.name == "classes.jar") { | ||
def output = outputs.file(aarFilename + "-" + item.name) | ||
def inputStream = null | ||
try { | ||
inputStream = zip.getInputStream(item) | ||
Files.copy(inputStream, output.toPath()) | ||
} finally { | ||
if (inputStream != null) { | ||
inputStream.close() | ||
} | ||
} | ||
} | ||
} | ||
} finally { | ||
if (zip != null) { | ||
zip.close() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters