Last active
August 9, 2021 11:36
-
-
Save ok3141/68f4fe360be0edac95e4 to your computer and use it in GitHub Desktop.
Gradle obfuscate string
This file contains 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
apply from: "$rootDir/utils.gradle" | |
android { | |
defaultConfig { | |
buildConfigField 'String', 'SECRET_KEY', toJavaCodeString(SECRET_KEY) | |
} | |
} |
This file contains 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
SECRET_KEY=ab76239ef64454356754239ef6210898 |
This file contains 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
class Utils { | |
static def r = new Random(System.currentTimeMillis()) | |
} | |
def String toJavaCodeString(String string) { | |
byte[] b = string.getBytes(); | |
int c = b.length; | |
StringBuilder sb = new StringBuilder(); | |
sb.append("(new Object() {"); | |
sb.append("int t;"); | |
sb.append("public String toString() {"); | |
sb.append("byte[] buf = new byte["); | |
sb.append(c); | |
sb.append("];"); | |
for (int i = 0; i < c; ++i) { | |
int t = Utils.r.nextInt(); | |
int f = Utils.r.nextInt(24) + 1; | |
t = (t & ~(0xff << f)) | (b[i] << f); | |
sb.append("t = "); | |
sb.append(t); | |
sb.append(";"); | |
sb.append("buf["); | |
sb.append(i); | |
sb.append("] = (byte) (t >>> "); | |
sb.append(f); | |
sb.append(");"); | |
} | |
sb.append("return new String(buf);"); | |
sb.append("}}.toString())"); | |
return sb.toString(); | |
} | |
ext.toJavaCodeString = this.&toJavaCodeString |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original repo is here.