Skip to content

Instantly share code, notes, and snippets.

@OleksandrKucherenko
Last active April 11, 2022 21:06
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OleksandrKucherenko/8239dc7c792417eae344708f2e51f1b6 to your computer and use it in GitHub Desktop.
Save OleksandrKucherenko/8239dc7c792417eae344708f2e51f1b6 to your computer and use it in GitHub Desktop.
repack AAR with different types of compatibility things, like: androidx vs support library, include/exclude/repackage aar during compilation
/** Copyright: 2019-*, Oleksandr Kucherenko (olku@artfulbits.se) */
apply plugin: 'com.android.library'
android {
/* ... default android lib OR app configuration ... */
}
configurations {
repack { transitive = false }
compatibility { transitive = false }
}
dependencies {
// include re-packed facebook conceal lib, https://github.com/facebook/conceal/releases
repack "com.facebook.conceal:conceal:1.1.3@aar"
/* https://mvnrepository.com/artifact/androidx.biometric/biometric | src: http://bit.ly/31DhLZG */
compatibility 'androidx.biometric:biometric:1.0.0-rc01@aar'
}
task repackConceal(dependsOn: configurations.repack) {
group = "Workarounds"
description = "repack facebook conceal library, exclude soloader sources"
def concealVersion = configurations.repack.allDependencies.first().version
def generation = "${project.buildDir}/intermediates/repack"
def conceal = "com.facebook.conceal@${concealVersion}"
def destination = "${generation}/${conceal}"
def aarFile = "com.facebook.conceal-${concealVersion}-repack.aar"
project.dependencies {
implementation fileTree(dir: "${generation}", include: ["*-repack.aar"])
}
doLast {
copy {
from { zipTree(configurations.repack.singleFile) }
into "${destination}"
}
copy {
from zipTree("${destination}/classes.jar")
into "${destination}/classes"
}
}
task recreateConcealClassesJar(type: Zip) {
archiveFileName = "classes-patched.jar"
destinationDirectory = file("${destination}")
from("${destination}/classes") {
exclude '**/facebook/jni/'
exclude '**/facebook/proguard/'
exclude '**/facebook/soloader/'
}
}
task recreateConcealAar(type: Zip, dependsOn: recreateConcealClassesJar) {
archiveFileName = aarFile
destinationDirectory = file("${generation}")
from("${destination}") {
exclude '**/jni/*/libfb.so'
exclude '**/classes'
exclude 'classes.jar'
rename('classes-patched.jar', 'classes.jar')
}
}
finalizedBy recreateConcealAar
}
task compatibilityBiometric(dependsOn: configurations.compatibility, type: Copy) {
group = "Workarounds"
description = "expand biometric library for androidx/android support compatibility"
def isAndroidX = Boolean.parseBoolean("${project.getProperties()["android.useAndroidX"]}")
def biometricVersion = configurations.compatibility.allDependencies.first().version
def generation = "${project.buildDir}/intermediates/compatibility"
def aarFile = "biometric-${biometricVersion}.aar"
def libPrefix = isAndroidX ? "androidx" : "support"
def supportFile = "biometric-${biometricVersion}-${libPrefix}.aar"
def bin = file("${project.projectDir}/../bin/jetifier-standalone/bin").absolutePath
logger.lifecycle("androidx: ${isAndroidX}, support lib: ${!isAndroidX}")
project.dependencies {
implementation fileTree(dir: "${generation}", include: ["*-${libPrefix}.aar"])
/* We can include extra ldependencies if needed with different conditions. */
if (isAndroidX) {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
}
}
from { configurations.compatibility.singleFile }
into "${generation}"
// https://developer.android.com/studio/command-line/jetifier
task reverseJetifier(type: Exec) {
outputs.cacheIf { true }
outputs.file "${generation}/${supportFile}"
inputs.file "${generation}/${aarFile}"
workingDir "${generation}"
commandLine "${bin}/jetifier-standalone"
args = [(isAndroidX ? "" : "--reversed"),
"--input", "${generation}/${aarFile}",
"--output", "${generation}/${supportFile}",
"--log", "error"]
}
finalizedBy reverseJetifier
}
preBuild.dependsOn([repackConceal, compatibilityBiometric])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment