From c9afead1a95863a28f58df4a46d9395c2a918ad5 Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Sat, 15 Sep 2012 22:56:10 -0700 Subject: [PATCH] Implement proguard support --- baksmali/build.gradle | 54 ++++++++++++++++++++++++++++++++++--------- build.gradle | 2 ++ smali/build.gradle | 49 ++++++++++++++++++++++++++++++--------- 3 files changed, 83 insertions(+), 22 deletions(-) diff --git a/baksmali/build.gradle b/baksmali/build.gradle index 99858e6f..f2dd3652 100644 --- a/baksmali/build.gradle +++ b/baksmali/build.gradle @@ -1,22 +1,54 @@ +configurations { + proguard +} + dependencies { compile project(':util') compile project(':dexlib') compile 'commons-cli:commons-cli:1.2' compile 'com.google.code.findbugs:jsr305:1.3.9' -} -processResources.inputs.properties('version': { -> version}) -processResources.expand('version': { -> version}) + proguard 'net.sf.proguard:proguard-base:4.8' +} // We have to do this in taskGraph.whenReady, so that we use the correct // version to resolve the project dependencies gradle.taskGraph.whenReady { - // build a jar containing all dependencies - jar { - from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } + // build a jar containing all dependencies + jar { + from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } - manifest { - attributes("Main-Class": "org.jf.baksmali.main") - } - } -} \ No newline at end of file + manifest { + attributes("Main-Class": "org.jf.baksmali.main") + } + } + + proguard { + def outFile = relativePath(buildDir) + '/libs/' + jar.baseName + '-' + jar.version + '-small' + '.' + jar.extension + + inputs.file jar.archivePath + outputs.file outFile + + args '-injars ' + jar.archivePath + args '-outjars ' + outFile + } + + processResources.inputs.properties('version': { -> version}) + processResources.expand('version': { -> version}) +} + +task proguard(type: JavaExec, dependsOn: jar) { + setStandardOutput(java.lang.System.out); + setErrorOutput(java.lang.System.err); + classpath = files(configurations.proguard.asPath) + main = 'proguard.ProGuard' + args '-libraryjars ' + System.properties['java.home'] + '/lib/rt.jar' + args '-dontobfuscate' + args '-dontoptimize' + args '-keep public class org.jf.baksmali.main { public static void main(java.lang.String[]); }' + args '-keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); }' + args '-dontwarn com.google.common.base.**' + args '-dontnote com.google.common.base.**' +} + +release.dependsOn(proguard) \ No newline at end of file diff --git a/build.gradle b/build.gradle index 801014e5..00ebfba4 100644 --- a/build.gradle +++ b/build.gradle @@ -28,6 +28,8 @@ subprojects { // use -dev for the jar name, rather than the // full commit+dirty string jarVersion = baseVersion + '-dev' + } else { + version = baseVersion } jar { diff --git a/smali/build.gradle b/smali/build.gradle index 257c2703..b5f9cb17 100644 --- a/smali/build.gradle +++ b/smali/build.gradle @@ -1,7 +1,7 @@ configurations { antlr3 - jflex + proguard } ext.antlrSource = 'src/main/antlr3' @@ -28,6 +28,7 @@ dependencies { antlr3 'org.antlr:antlr:3.2' jflex 'de.jflex:jflex:1.4.3' + proguard 'net.sf.proguard:proguard-base:4.8' } task generateAntlrSource { @@ -78,18 +79,44 @@ task generateJflexSource { compileJava.dependsOn generateAntlrSource, generateJflexSource compileTestJava.dependsOn generateTestAntlrSource -processResources.inputs.properties('version': { -> version}) -processResources.expand('version': { -> version}) - // We have to do this in taskGraph.whenReady, so that we use the correct // version to resolve the project dependencies gradle.taskGraph.whenReady { - // build a jar containing all dependencies - jar { - from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } + // build a jar containing all dependencies + jar { + from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } - manifest { - attributes("Main-Class": "org.jf.smali.main") - } - } + manifest { + attributes("Main-Class": "org.jf.smali.main") + } + } + + processResources.inputs.properties('version': version) + processResources.expand('version': version) + + proguard { + def outFile = relativePath(buildDir) + '/libs/' + jar.baseName + '-' + jar.version + '-small' + '.' + jar.extension + + inputs.file jar.archivePath + outputs.file outFile + + args '-injars ' + jar.archivePath + '(!**/TestStringTemplate*.class)' + args '-outjars ' + outFile + } } + +task proguard(type: JavaExec, dependsOn: jar) { + setStandardOutput(java.lang.System.out); + setErrorOutput(java.lang.System.err); + classpath = files(configurations.proguard.asPath) + main = 'proguard.ProGuard' + args '-libraryjars ' + System.properties['java.home'] + '/lib/rt.jar' + args '-dontobfuscate' + args '-dontoptimize' + args '-keep public class org.jf.smali.main { public static void main(java.lang.String[]); }' + args '-keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); }' + args '-dontwarn com.google.common.base.**' + args '-dontnote com.google.common.base.**' +} + +release.dependsOn(proguard) \ No newline at end of file