Last active
November 14, 2023 16:12
-
-
Save RafhaanShah/a9404697adfed04f09aed8405d34ea35 to your computer and use it in GitHub Desktop.
Gradle task to create an uber jar of all the runtime dependency classes of a package including Android libraries. Does NOT merge resources / XML / Manifest.
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
// create an uber JAR of all the runtime dependencies of a package | |
task uberJar(type: Jar) { | |
from { | |
// build flavour is needed for Android projects, otherwise it's just runtimeClasspath | |
configurations.releaseRuntimeClasspath.collect { original -> | |
// leave folders alone | |
if (original.isDirectory()) { | |
return original | |
} | |
// for an AAR only get the classes.jar | |
if (original.name.endsWith('.aar')) { | |
return zipTree(original) | |
.matching { include "classes.jar" } | |
.files.collect { file -> | |
return zipTree(file) | |
} | |
} | |
// otherwise it's already a JAR | |
return zipTree(original) | |
} | |
} | |
// exclude other contents | |
eachFile { file -> | |
if (file.relativePath.segments.contains('META-INF')) { | |
file.exclude() | |
} | |
if (!file.name.endsWith('.class')) { | |
file.exclude() | |
} | |
} | |
includeEmptyDirs false | |
// default build/private/gradle/libs | |
destinationDir file("$buildDir/dependencies") // $buildDir is build/private/gradle/ | |
archiveName 'dependencies.jar' | |
// outputs.upToDateWhen { false } // never cache output | |
} |
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
// pack in all classes from dependencies into the classes.jar of the release aar | |
task unzipClassesJar(type: Copy) { | |
// directory where classes are included into the aar classes.jar from | |
def unpackDir = "$buildDir/intermediates/classes/release" | |
from { | |
// build flavour is needed for Android projects, otherwise it's just runtimeClasspath | |
configurations.releaseRuntimeClasspath.collect { original -> | |
// leave folders alone | |
if (original.isDirectory()) { | |
return original | |
} | |
// for an AAR only get the classes.jar | |
if (original.name.endsWith('.aar')) { | |
return zipTree(original) | |
.matching { include "classes.jar" } | |
.files.collect { file -> | |
return zipTree(file) | |
} | |
} | |
// otherwise it's already a JAR | |
return zipTree(original) | |
} | |
} | |
// exclude other contents | |
eachFile { file -> | |
if (file.relativePath.segments.contains('META-INF')) { | |
file.exclude() | |
} | |
if (!file.name.endsWith('.class')) { | |
file.exclude() | |
} | |
} | |
includeEmptyDirs false | |
into unpackDir | |
} | |
// run the assembleRelease task automatically after | |
project.afterEvaluate { | |
unzipClassesJar.finalizedBy assembleRelease | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment