Last active
August 2, 2024 11:07
-
-
Save arcao/16d51cce70dadec54bef to your computer and use it in GitHub Desktop.
Run JarJar task to repackage Gson library in Android Gradle build script
This file contains hidden or 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
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
classpath 'com.android.tools.build:gradle:0.13.0' | |
} | |
} | |
apply plugin: 'com.android.application' | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
// Add the support lib that is appropriate for SDK 7 | |
compile 'com.android.support:support-v4:20.0.0' | |
compile 'com.android.support:gridlayout-v7:19.1.0' | |
compile 'com.android.support:appcompat-v7:20.0.0' | |
compile 'com.google.code.gson:gson:2.3' | |
} | |
android { | |
compileSdkVersion 19 | |
buildToolsVersion '20' | |
compileOptions { | |
sourceCompatibility JavaVersion.VERSION_1_7 | |
targetCompatibility JavaVersion.VERSION_1_7 | |
} | |
packagingOptions { | |
// removed because of conflict in creating package | |
exclude 'META-INF/LICENSE' | |
exclude 'META-INF/NOTICE' | |
// unused files, removed to minimize APK file | |
exclude 'META-INF/LICENSE.txt' | |
exclude 'META-INF/NOTICE.txt' | |
exclude 'META-INF/DEPENDENCIES' | |
exclude 'org/apache/http/version.properties' | |
exclude 'templates/release-notes.vm' | |
} | |
buildTypes { | |
release { | |
runProguard true | |
proguardFile getDefaultProguardFile('proguard-android-optimize.txt') | |
proguardFile 'proguard.cfg' | |
} | |
} | |
} | |
// JarJar stuffs start here | |
configurations { | |
// create new configuration for JarJar | |
jarjar | |
} | |
dependencies { | |
// add dependecies to jarjar configuration | |
jarjar 'com.googlecode.jarjar:jarjar:1.3' | |
} | |
// for all variants | |
android.applicationVariants.all { variant -> | |
// only for release | |
if (variant.name != 'release') | |
return; | |
// only when proguard is enabled | |
if (!variant.buildType.runProguard) | |
return; | |
// create task name | |
String jarJarTaskName = "jarJar${variant.name.capitalize()}"; | |
// prepare variables | |
def workingDir = new File('build/intermediates/jarjar'); | |
// proguard generate one jar file. This jar file is in Dex.inputFiles | |
// (see sources com/android/build/gradle/BasePlugin.groovy) | |
def inputLibrary = variant.dex.inputFiles[0]; | |
def outputLibrary = new File(workingDir, 'classes.jar').getCanonicalFile() | |
// define task | |
def jarJarTask = task("${jarJarTaskName}") { | |
// debug print | |
logger.info '****************************' | |
logger.info "* Prepare ${jarJarTaskName}" | |
logger.info '****************************' | |
logger.info "inputLibrary: ${inputLibrary}" | |
logger.info "outputLibrary: ${outputLibrary}" | |
logger.info '****************************' | |
// support for up-to-date feature | |
inputs.file inputLibrary | |
outputs.file outputLibrary | |
doLast { | |
// in Ant | |
project.ant { | |
// define jarjar task, for classpath is used path from jarjar configuration | |
taskdef name: 'jarjar', classname: 'com.tonicsystems.jarjar.JarJarTask', classpath: configurations.jarjar.asPath | |
// start jarjar task | |
jarjar(jarfile: outputLibrary) { | |
// input is our inputLibrary | |
zipfileset(src: inputLibrary) | |
// rule to repackage gson to new package | |
rule pattern: 'com.google.gson.**', result: 'com.google.repacked.gson.@1' | |
} | |
} | |
// replace jar generated by Proguard with jar gemerated by JarJar | |
variant.dex.inputFiles = [outputLibrary] | |
variant.dex.libraries = [] | |
} | |
} | |
// plan task to be started between Proguard task and Dex task | |
variant.dex.dependsOn jarJarTask | |
jarJarTask.dependsOn variant.variantData.obfuscationTask | |
} |
This file contains hidden or 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
-keepnames class com.google.gson.** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment