Skip to content

Instantly share code, notes, and snippets.

@dzungpv
Forked from ok3141/build.gradle
Created July 23, 2016 14:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dzungpv/497eb2175cf577ff09966c6876a5f2da to your computer and use it in GitHub Desktop.
Save dzungpv/497eb2175cf577ff09966c6876a5f2da to your computer and use it in GitHub Desktop.
Gradle obfuscate string
apply from: "$rootDir/utils.gradle"
android {
defaultConfig {
buildConfigField 'String', 'SECRET_KEY', toJavaCodeString(SECRET_KEY)
}
}
SECRET_KEY=ab76239ef64454356754239ef6210898
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