Permalink
Browse files

update

  • Loading branch information...
1 parent 6c277b6 commit 77f6263a0bdc1fa76a057e887b178bd0a24a7f04 @achellies committed Nov 3, 2016
@@ -1,4 +1,4 @@
--useuniqueclassmembernames
+#-useuniqueclassmembernames
-keep interface com.android.tools.fd.runtime.IncrementalChange { *; }
-keep class * implements com.android.tools.fd.runtime.IncrementalChange { *; }
-keep class com.android.tools.fd.** {*;}
View
@@ -16,6 +16,7 @@ dependencies {
compile 'com.android.tools.build:gradle:2.2.0'
compile 'org.ow2.asm:asm:5.0'
compile 'org.javassist:javassist:3.20.0-GA'
+ compile 'net.sf.proguard:proguard-base:5.3.1'
compile fileTree(dir: "./src/main/libs", include: ['*.jar'])
}
@@ -1,9 +1,17 @@
package com.achellies.gradle.ultron
+import com.android.SdkConstants
import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.internal.pipeline.TransformTask
import com.android.build.gradle.internal.transforms.ProGuardTransform
import com.android.utils.XmlUtils
+import javassist.CannotCompileException
+import javassist.ClassPool
+import javassist.CtClass
+import javassist.CtMethod
+import javassist.expr.ExprEditor
+import javassist.expr.MethodCall
+import org.apache.commons.io.IOUtils
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.tooling.BuildException
@@ -12,13 +20,21 @@ import org.w3c.dom.Element
import org.w3c.dom.Node
import org.w3c.dom.NodeList
import org.xml.sax.SAXException
+import proguard.ProGuard
+import proguard.obfuscate.MemberNameCollector
+import proguard.obfuscate.MemberNameConflictFixer
+import proguard.obfuscate.MemberObfuscator
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.Transformer
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
+import java.util.jar.JarEntry
+import java.util.jar.JarFile
+import java.util.jar.JarOutputStream
+import java.util.zip.ZipEntry
import static com.android.SdkConstants.ATTR_PACKAGE
import static com.android.SdkConstants.TAG_APPLICATION
@@ -47,6 +63,11 @@ class UltronPlugin implements Plugin<Project> {
}
}
+ new ProGuard(null);
+ new MemberObfuscator(false, null, null);
+ new MemberNameCollector(false, null);
+ new MemberNameConflictFixer(false, null, null, null);
+
applyExtension(project);
UltronPlugin plugin = this;
@@ -58,6 +79,71 @@ class UltronPlugin implements Plugin<Project> {
// // FIXME: 为了支持ProGuard,这里需要在ProGuard完成后再进行处理,所以放弃Transform
// GradleUtils.getAndroidExtension(project).registerTransform(new UltronTransform(this, project))
}
+
+ }
+
+ String trickDxJarForRuntimeVisibleAnnotations(String jarPath) {
+
+ def dxJarFile = new File(jarPath)
+ def jar = new JarFile(dxJarFile)
+
+ def trickedJar = new File(dxJarFile.parentFile, "${dxJarFile.name}_bat.jar")
+ if (!trickedJar.exists()) {
+
+ JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(trickedJar));
+
+ Enumeration enumeration = jar.entries();
+ while (enumeration.hasMoreElements()) {
+ JarEntry jarEntry = (JarEntry) enumeration.nextElement();
+
+ String entryName = jarEntry.name;
+
+ ZipEntry zipEntry = new ZipEntry(entryName);
+
+ InputStream inputStream = jar.getInputStream(jarEntry);
+
+ if (entryName.endsWith(SdkConstants.DOT_CLASS) && (entryName.startsWith("proguard"))) {
+// try {
+// def classPool = new ClassPool(null)
+// classPool.appendSystemPath()
+// classPool.appendClassPath(dxJarFile.absolutePath)
+//
+// CtClass ctClass = classPool.getOrNull("proguard.classfile.ClassPool")
+// if (ctClass != null) {
+// CtMethod ctMethod = ctClass.getDeclaredMethod("parseClass")
+// ctMethod.instrument(new ExprEditor() {
+// @Override
+// void edit(MethodCall m) throws CannotCompileException {
+// super.edit(m)
+// if (m.methodName.contentEquals("getMagic") && m.className.contentEquals("com.android.dx.cf.direct.DirectClassFile")) {
+// m.replace("if (\$0.getMajorVersion0() >= 52) {\n" +
+// "com.android.dx.command.DxConsole.err.println(name);};\n" +
+// "\$_ = \$proceed(\$\$);")
+// }
+// }
+// })
+// }
+//
+// jarOutputStream.putNextEntry(zipEntry);
+// jarOutputStream.write(ctClass.toBytecode())
+// ctClass.detach()
+// } catch (Exception e) {
+ jarOutputStream.putNextEntry(zipEntry);
+ jarOutputStream.write(IOUtils.toByteArray(inputStream));
+// }
+ }
+// else {
+// jarOutputStream.putNextEntry(zipEntry);
+// jarOutputStream.write(IOUtils.toByteArray(inputStream));
+// }
+ inputStream.close()
+ jarOutputStream.closeEntry();
+ }
+
+ jarOutputStream.close();
+ }
+
+ return trickedJar.absolutePath
}
void configUltronTask(UltronPlugin plugin) {
@@ -69,6 +155,19 @@ class UltronPlugin implements Plugin<Project> {
def proguard = (TransformTask) project.tasks[proguardTaskName]
def pt = (ProGuardTransform) proguard.getTransform()
configureProguard(variant, proguard, pt)
+
+ proguard.doFirst {
+ applyProGuardMapping(project);
+
+// println "applyMapping = " + pt.configuration.applyMapping;
+// println "testedMappingFile = " + pt.testedMappingFile;
+ }
+
+// proguard.doLast {
+// println "********************************"
+// println "applyMapping = " + pt.configuration.applyMapping;
+// println "testedMappingFile = " + pt.testedMappingFile;
+// }
}
if (!variant.getOutputs().isEmpty()) {
@@ -95,7 +194,10 @@ class UltronPlugin implements Plugin<Project> {
// pt.keep("interface com.android.tools.fd.runtime.IncrementalChange { *; }");
// pt.keep("class * implements com.android.tools.fd.runtime.IncrementalChange { *; }");
// pt.keep("class com.android.tools.fd.** {*;}");
+ applyProGuardMapping(project);
+ }
+ void applyProGuardMapping(Project project) {
UltronExtension extension = getConfig(project);
if (extension.applyProGuardMappingFilePath != null && !extension.applyProGuardMappingFilePath.isEmpty()) {
def mappingFile = new File(extension.applyProGuardMappingFilePath);
View
@@ -81,4 +81,5 @@ dependencies {
ultron {
supportPackageFilters = ["com.achellies."]
// applyClassHashFilePath = "hash.txt"
+ applyProGuardMappingFilePath = "mapping.txt"
}
Oops, something went wrong.

0 comments on commit 77f6263

Please sign in to comment.