├── plugin ├── .gitignore ├── src │ └── main │ │ ├── resources │ │ └── META-INF │ │ │ └── gradle-plugins │ │ │ └── com.catchingnow.robfuscate.properties │ │ └── groovy │ │ └── com │ │ └── catchingnow │ │ └── robfuscate │ │ ├── SavedIntField.groovy │ │ ├── RoPlugin.groovy │ │ ├── RobfuscateUtilDump.groovy │ │ ├── RoTransform.groovy │ │ └── RoInject.groovy └── build.gradle ├── settings.gradle ├── screenshots ├── after.png └── before.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── README.md ├── .gitignore ├── gradlew.bat └── gradlew /plugin/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':plugin' 2 | -------------------------------------------------------------------------------- /screenshots/after.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heruoxin/Robfuscate/HEAD/screenshots/after.png -------------------------------------------------------------------------------- /screenshots/before.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heruoxin/Robfuscate/HEAD/screenshots/before.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/heruoxin/Robfuscate/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /plugin/src/main/resources/META-INF/gradle-plugins/com.catchingnow.robfuscate.properties: -------------------------------------------------------------------------------- 1 | implementation-class=com.catchingnow.robfuscate.RoPlugin 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Mar 20 10:56:28 CST 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip 7 | -------------------------------------------------------------------------------- /plugin/src/main/groovy/com/catchingnow/robfuscate/SavedIntField.groovy: -------------------------------------------------------------------------------- 1 | package com.catchingnow.robfuscate 2 | 3 | public class SavedIntField { 4 | public String className; 5 | public int access 6 | public String name 7 | public String desc 8 | public String signature 9 | public int value 10 | } 11 | -------------------------------------------------------------------------------- /plugin/src/main/groovy/com/catchingnow/robfuscate/RoPlugin.groovy: -------------------------------------------------------------------------------- 1 | package com.catchingnow.robfuscate 2 | 3 | import com.android.build.gradle.AppExtension 4 | import com.android.build.gradle.AppPlugin 5 | import org.gradle.api.Plugin 6 | import org.gradle.api.Project 7 | 8 | /** 9 | * @author heruoxin @ CatchingNow Inc. 10 | * @since 2019-03-20 11 | */ 12 | class RoPlugin implements Plugin { 13 | 14 | void apply(Project project) { 15 | def isApp = project.plugins.hasPlugin(AppPlugin) 16 | if (isApp) { 17 | def android = project.extensions.findByType(AppExtension) 18 | android.registerTransform(new RoTransform(project)) 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /plugin/build.gradle: -------------------------------------------------------------------------------- 1 | //removed java plugin 2 | apply plugin: 'groovy' 3 | apply plugin: 'maven' 4 | 5 | sourceCompatibility = 1.8 6 | targetCompatibility = 1.8 7 | 8 | group='com.catchingnow.robfuscate' 9 | version='1.0.0-SNAPSHOT' 10 | 11 | uploadArchives { 12 | repositories { 13 | mavenDeployer { 14 | //提交到远程服务器: 15 | // repository(url: "http://www.xxx.com/repos") { 16 | // authentication(userName: "admin", password: "admin") 17 | // } 18 | //本地的Maven地址设 19 | repository(url: uri('/tmp/repos')) 20 | } 21 | } 22 | } 23 | 24 | repositories { 25 | mavenCentral() 26 | } 27 | 28 | dependencies { 29 | implementation gradleApi()//gradle sdk 30 | implementation localGroovy()//groovy sdk 31 | implementation 'com.android.tools.build:gradle:3.3.2' 32 | implementation 'com.android.tools.build:gradle-api:3.3.2' 33 | implementation 'org.ow2.asm:asm:7.1' 34 | implementation 'org.ow2.asm:asm-commons:7.1' 35 | implementation fileTree(dir: 'libs', include: ['*.jar']) 36 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Robfuscate 2 | 3 | 4 | Obfuscate the int index of R.id/R.string/R.layout… in the classes.dex of Android project. 5 | 6 | 混淆 Android 项目中 R.id/R.string/R.layout… 等在代码中的 int 索引,可以略微提升破解难度。 7 | 8 | 9 | #### Before 10 | 11 | ![img](screenshots/before.png) 12 | 13 | ### After 14 | 15 | ![img](screenshots/after.png) 16 | 17 | ### Limitation 18 | 19 | It only works for R in the sub modules. For app level module, R.id will be replaced to static int value before Robfuscate so it will not work. 20 | 21 | 22 | ## Usage 23 | 24 | 25 | 1. Add the following into your project level `build.gradle`: 26 | 27 | ```groovy 28 | repositories { 29 | //... 30 | maven { url "https://jitpack.io" } 31 | } 32 | dependencies { 33 | //... 34 | classpath 'com.github.heruoxin:Robfuscate:master' 35 | } 36 | ``` 37 | 38 | 2. Modify your app level `build.gradle`: 39 | 40 | ```groovy 41 | 42 | apply plugin: 'com.android.application' 43 | // NOTICE: Robfuscate must be added AFTER android plugin. 44 | apply plugin: 'com.catchingnow.robfuscate' 45 | ``` 46 | 47 | Done. 48 | 49 | 50 | ## Acknowledgement 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | 10 | # Built application files 11 | *.apk 12 | *.ap_ 13 | *.aab 14 | 15 | # Files for the ART/Dalvik VM 16 | *.dex 17 | 18 | # Java class files 19 | *.class 20 | 21 | # Generated files 22 | bin/ 23 | gen/ 24 | out/ 25 | 26 | # Gradle files 27 | .gradle/ 28 | build/ 29 | 30 | # Local configuration file (sdk path, etc) 31 | local.properties 32 | 33 | # Proguard folder generated by Eclipse 34 | proguard/ 35 | 36 | # Log Files 37 | *.log 38 | 39 | # Android Studio Navigation editor temp files 40 | .navigation/ 41 | 42 | # Android Studio captures folder 43 | captures/ 44 | 45 | # IntelliJ 46 | *.iml 47 | .idea/workspace.xml 48 | .idea/tasks.xml 49 | .idea/gradle.xml 50 | .idea/assetWizardSettings.xml 51 | .idea/dictionaries 52 | .idea/libraries 53 | .idea/caches 54 | # Android Studio 3 in .gitignore file. 55 | .idea/caches/build_file_checksums.ser 56 | .idea/modules.xml 57 | 58 | # Keystore files 59 | # Uncomment the following lines if you do not want to check your keystore files in. 60 | #*.jks 61 | #*.keystore 62 | 63 | # External native build folder generated in Android Studio 2.2 and later 64 | .externalNativeBuild 65 | 66 | # Google Services (e.g. APIs or Firebase) 67 | # google-services.json 68 | 69 | # Freeline 70 | freeline.py 71 | freeline/ 72 | freeline_project_description.json 73 | 74 | # fastlane 75 | fastlane/report.xml 76 | fastlane/Preview.html 77 | fastlane/screenshots 78 | fastlane/test_output 79 | fastlane/readme.md 80 | 81 | # Version control 82 | vcs.xml 83 | 84 | # lint 85 | lint/intermediates/ 86 | lint/generated/ 87 | lint/outputs/ 88 | lint/tmp/ 89 | # lint/reports/ -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /plugin/src/main/groovy/com/catchingnow/robfuscate/RobfuscateUtilDump.groovy: -------------------------------------------------------------------------------- 1 | package com.catchingnow.robfuscate 2 | 3 | import java.util.*; 4 | 5 | import org.objectweb.asm.*; 6 | 7 | public class RobfuscateUtilDump implements Opcodes { 8 | public static final String NAME = "com/catchingnow/base/util/RobfuscateUtil"; 9 | 10 | public static byte[] dump() throws Exception { 11 | 12 | ClassWriter cw = new ClassWriter(0); 13 | FieldVisitor fv; 14 | MethodVisitor mv; 15 | AnnotationVisitor av0; 16 | 17 | cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, NAME, null, "java/lang/Object", null); 18 | 19 | cw.visitSource("RobfuscateUtil.java", null); 20 | 21 | fv = cw.visitField(ACC_PRIVATE + ACC_FINAL + ACC_STATIC, "MAGIC", "I", null, null); 22 | fv.visitEnd(); 23 | 24 | mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null); 25 | mv.visitCode(); 26 | Label ll0 = new Label(); 27 | mv.visitLabel(ll0); 28 | mv.visitLineNumber(7, ll0); 29 | mv.visitVarInsn(ALOAD, 0); 30 | mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V", false); 31 | mv.visitInsn(RETURN); 32 | Label ll1 = new Label(); 33 | mv.visitLabel(ll1); 34 | mv.visitLocalVariable("this", "L"+NAME+";", null, ll0, ll1, 0); 35 | mv.visitMaxs(1, 1); 36 | mv.visitEnd(); 37 | 38 | mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "convert", "(I)I", null, null); 39 | mv.visitCode(); 40 | Label l0 = new Label(); 41 | mv.visitLabel(l0); 42 | mv.visitLineNumber(12, l0); 43 | mv.visitVarInsn(ILOAD, 0); 44 | mv.visitFieldInsn(GETSTATIC, NAME, "MAGIC", "I"); 45 | mv.visitInsn(IXOR); 46 | mv.visitInsn(IRETURN); 47 | Label l1 = new Label(); 48 | mv.visitLabel(l1); 49 | mv.visitLocalVariable("val", "I", null, l0, l1, 0); 50 | mv.visitMaxs(2, 1); 51 | mv.visitEnd(); 52 | 53 | mv = cw.visitMethod(ACC_STATIC, "", "()V", null, null); 54 | mv.visitCode(); 55 | Label la0 = new Label(); 56 | mv.visitLabel(la0); 57 | mv.visitLineNumber(9, la0); 58 | mv.visitLdcInsn("f86r4y"); 59 | mv.visitIntInsn(BIPUSH, 36); 60 | mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;I)I", false); 61 | mv.visitInsn(INEG); 62 | mv.visitFieldInsn(PUTSTATIC, NAME, "MAGIC", "I"); 63 | mv.visitInsn(RETURN); 64 | mv.visitMaxs(2, 0); 65 | mv.visitEnd(); 66 | 67 | cw.visitEnd(); 68 | 69 | return cw.toByteArray(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /plugin/src/main/groovy/com/catchingnow/robfuscate/RoTransform.groovy: -------------------------------------------------------------------------------- 1 | package com.catchingnow.robfuscate 2 | 3 | import com.android.build.api.transform.* 4 | import com.android.build.gradle.internal.pipeline.TransformManager 5 | import com.google.common.collect.Sets 6 | import org.apache.commons.codec.digest.DigestUtils 7 | import org.apache.commons.io.FileUtils 8 | import org.gradle.api.Project 9 | 10 | class RoTransform extends Transform{ 11 | 12 | boolean hasPutConverter = false 13 | Project project 14 | public RoTransform(Project project) { 15 | this.project = project 16 | } 17 | 18 | @Override 19 | String getName() { 20 | return "robfuscate" 21 | } 22 | 23 | @Override 24 | Set getInputTypes() { 25 | return TransformManager.CONTENT_CLASS 26 | } 27 | 28 | @Override 29 | Set getScopes() { 30 | return Sets.immutableEnumSet(QualifiedContent.Scope.PROJECT); 31 | } 32 | 33 | @Override 34 | boolean isIncremental() { 35 | return true 36 | } 37 | 38 | @Override 39 | void transform(Context context, Collection inputs, 40 | Collection referencedInputs, 41 | TransformOutputProvider outputProvider, boolean isIncremental) 42 | throws IOException, TransformException, InterruptedException { 43 | // Transform的inputs有两种类型,一种是目录,一种是jar包,要分开遍历 44 | inputs.each {TransformInput input -> 45 | //对类型为“文件夹”的input进行遍历 46 | input.directoryInputs.each {DirectoryInput directoryInput-> 47 | //文件夹里面包含的是我们手写的类以及R.class、BuildConfig.class以及R$XXX.class等 48 | 49 | // 获取output目录 50 | def dest = outputProvider.getContentLocation(directoryInput.name, 51 | directoryInput.contentTypes, directoryInput.scopes, 52 | Format.DIRECTORY) 53 | 54 | // 修改 R 文件 55 | RoInject.injectDir(project, directoryInput.file.absolutePath); 56 | 57 | // 放入解码类 58 | if (!hasPutConverter) { 59 | hasPutConverter = true 60 | def s = File.separator 61 | def f = new File("${directoryInput.file.absolutePath}${s}${RobfuscateUtilDump.NAME.replace("/", s)}.class") 62 | f.mkdirs() 63 | f.delete() 64 | def os = f.newOutputStream() 65 | os.write(RobfuscateUtilDump.dump()) 66 | os.close() 67 | } 68 | 69 | // 将input的目录复制到output指定目录 70 | FileUtils.copyDirectory(directoryInput.file, dest) 71 | 72 | } 73 | //对类型为jar文件的input进行遍历 74 | input.jarInputs.each {JarInput jarInput-> 75 | 76 | //jar文件一般是第三方依赖库jar文件 77 | 78 | // 重命名输出文件(同目录copyFile会冲突) 79 | def jarName = jarInput.name 80 | def md5Name = DigestUtils.md5Hex(jarInput.file.getAbsolutePath()) 81 | if(jarName.endsWith(".jar")) { 82 | jarName = jarName.substring(0,jarName.length()-4) 83 | } 84 | //生成输出路径 85 | def dest = outputProvider.getContentLocation(jarName+md5Name, 86 | jarInput.contentTypes, jarInput.scopes, Format.JAR) 87 | //将输入内容复制到输出 88 | FileUtils.copyFile(jarInput.file, dest) 89 | } 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /plugin/src/main/groovy/com/catchingnow/robfuscate/RoInject.groovy: -------------------------------------------------------------------------------- 1 | package com.catchingnow.robfuscate 2 | 3 | import org.gradle.api.Project 4 | import org.objectweb.asm.* 5 | import org.objectweb.asm.commons.Method 6 | 7 | class RoInject { 8 | 9 | private static final int MAGIC = 0xc91e8d1e; 10 | 11 | public static void injectDir(Project project, String path) { 12 | File dir = new File(path) 13 | if (dir.isDirectory()) { 14 | dir.eachFileRecurse { File file -> 15 | //确保当前文件是 R.class 文件 16 | if (file.absolutePath.matches(".*([/\\\\])R(\\\$[a-z]*)?\\.class")) { 17 | // 第一次扫描,记录下所有的 id 18 | def scanResult = scanFirst(file) 19 | // 第二次扫描,删方法并生成 static 代码块 20 | scanSecond(file, scanResult) 21 | } 22 | } 23 | } 24 | } 25 | 26 | private static Set scanFirst(File file) { 27 | def is = file.newInputStream() 28 | 29 | ClassReader cr = new ClassReader(is); 30 | ReadClassVisitor reader = new ReadClassVisitor(); 31 | cr.accept(reader, ClassReader.SKIP_FRAMES); 32 | 33 | is.close() 34 | return reader.getFieldSet() 35 | } 36 | 37 | private static void scanSecond(File file, Set fieldSet) { 38 | def file1 = new File(file.absolutePath + 1) 39 | def is = file.newInputStream() 40 | def os = file1.newOutputStream() 41 | 42 | ClassReader cr = new ClassReader(is); 43 | ClassWriter cw = new ClassWriter(cr, 0); 44 | ClassVisitor cv = new WriteClassVisitor(fieldSet, cw); 45 | cr.accept(cv, ClassReader.SKIP_FRAMES); 46 | 47 | def bytes = cw.toByteArray() 48 | os.write(bytes) 49 | is.close() 50 | os.close() 51 | 52 | if (file.exists()) { 53 | file.delete() 54 | file1.renameTo(file) 55 | } 56 | 57 | } 58 | 59 | static class ReadClassVisitor extends ClassVisitor { 60 | String className; 61 | Set mFieldSet = new HashSet<>() 62 | 63 | ReadClassVisitor() { 64 | super(Opcodes.ASM5, null) 65 | } 66 | 67 | public Set getFieldSet() { 68 | return mFieldSet 69 | } 70 | 71 | @Override 72 | public void visit(int version, int access, String name, String signature, 73 | String superName, String[] interfaces) { 74 | super.visit(version, access, name, signature, superName, interfaces); 75 | this.className = name; 76 | } 77 | 78 | @Override 79 | FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { 80 | if ("I".equals(desc) && value != null) { 81 | def field = new SavedIntField() 82 | field.className = this.className 83 | field.access = access 84 | field.name = name 85 | field.desc = desc 86 | field.signature = signature 87 | field.value = Integer.valueOf(value) 88 | mFieldSet.add(field) 89 | } 90 | return super.visitField(access, name, desc, signature, value); 91 | } 92 | 93 | } 94 | 95 | static class WriteClassVisitor extends ClassVisitor { 96 | private static STATIC_INITIALIZER_METHOD = new Method("", Type.VOID_TYPE, new Type[0]) 97 | 98 | String className; 99 | ClassWriter cw; 100 | Set mFieldSet = new HashSet<>() 101 | private boolean hasCalledInitializerMethod = false 102 | 103 | WriteClassVisitor(Set fieldSet, ClassWriter cw) { 104 | super(Opcodes.ASM5, cw) 105 | this.mFieldSet = fieldSet 106 | this.cw = cw 107 | hasCalledInitializerMethod = mFieldSet.size() == 0 108 | } 109 | 110 | @Override 111 | public void visit(int version, int access, String name, String signature, 112 | String superName, String[] interfaces) { 113 | super.visit(version, access, name, signature, superName, interfaces); 114 | this.className = name; 115 | } 116 | 117 | @Override 118 | FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { 119 | if ("I".equals(desc) && value != null) { 120 | return super.visitField(access, name, desc, null, null); 121 | } else { 122 | return super.visitField(access, name, desc, signature, value); 123 | } 124 | } 125 | 126 | @Override 127 | MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { 128 | def mv = super.visitMethod(access, name, desc, signature, exceptions) 129 | if (STATIC_INITIALIZER_METHOD.name.equals(name)) { 130 | if (hasCalledInitializerMethod) return 131 | hasCalledInitializerMethod = true 132 | return new StaticBlockMethodVisitor(mv) 133 | } 134 | return mv 135 | } 136 | 137 | @Override 138 | void visitEnd() { 139 | if (!hasCalledInitializerMethod) { 140 | hasCalledInitializerMethod = true 141 | MethodVisitor mv = super.visitMethod(Opcodes.ACC_STATIC, "", "()V", null, null); 142 | mv = new StaticBlockMethodVisitor(mv); 143 | mv.visitCode(); 144 | mv.visitInsn(Opcodes.RETURN); 145 | mv.visitMaxs(0, 0); 146 | mv.visitEnd(); 147 | } 148 | super.visitEnd() 149 | } 150 | 151 | 152 | class StaticBlockMethodVisitor extends MethodVisitor { 153 | StaticBlockMethodVisitor(MethodVisitor mv) { 154 | super(Opcodes.ASM5, mv); 155 | } 156 | 157 | @Override 158 | public void visitCode() { 159 | super.visitCode(); 160 | 161 | for (def f in mFieldSet) { 162 | def mixVal = f.value ^ MAGIC 163 | 164 | Label l0 = new Label(); 165 | mv.visitLabel(l0); 166 | mv.visitLdcInsn(new Integer(mixVal)); 167 | mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/catchingnow/base/util/RobfuscateUtil", "convert", "(I)I", false); 168 | mv.visitFieldInsn(Opcodes.PUTSTATIC, className, f.name, "I"); 169 | } 170 | } 171 | 172 | @Override 173 | public void visitMaxs(int maxStack, int maxLocals) { 174 | super.visitMaxs(Math.max(1, maxStack), maxLocals) 175 | } 176 | } 177 | } 178 | 179 | } 180 | --------------------------------------------------------------------------------