Skip to content

Commit

Permalink
support mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
nnjun committed Jan 13, 2022
1 parent 85d39c6 commit f8a5b27
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 60 deletions.
25 changes: 14 additions & 11 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,39 @@ android {

buildTypes {
release {
minifyEnabled false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}

debug {
minifyEnabled false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}

googleplay {
minifyEnabled false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

productFlavors {
app {
applicationId "com.ghosthing.ball"
}

}
compileOptions {
// productFlavors {
// app {
// applicationId "com.ghosthing.ball"
// }
//
// cpp {
// applicationId "com.ghosthing.ball"
// }
// }
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

BlackObfuscator {
enabled true
obfClass = ["MainActivity", "com.bbb"]
obfClass = ["top.niunaijun.blackobfuscator.asplugin", "androidx.core.app"]
}

dependencies {
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/top/niunaijun/blackobfuscator/asplugin/Abx.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package top.niunaijun.blackobfuscator.asplugin;

/**
* Created by Milk on 2022/1/13.
* * ∧_∧
* (`・ω・∥
* 丶 つ0
* しーJ
* 此处无Bug
*/
public class Abx {
public static boolean go() {
return System.currentTimeMillis() > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ protected void onCreate(Bundle savedInstanceState) {
} else {
Log.d("123", "onCreate: aax1xaaa");
}
Log.d("123", "onCreate: " + Abx.go());
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ed7bf432cc57babf03a25c5a0ff95abc
eb035af7c6ce60caf3ab9c762a96e98d
Original file line number Diff line number Diff line change
@@ -1 +1 @@
803f254bf5ef5711f0f6cf94f55b1237d3f8e7c8
eaab39a87530bce1ff233150bed9bd1c786c64d1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<versions>
<version>1.0.0</version>
</versions>
<lastUpdated>20220111141950</lastUpdated>
<lastUpdated>20220113045741</lastUpdated>
</versioning>
</metadata>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ab00298c03962a3ffdc635a350badc08
a4e0ad7fcf21d4047bf1348dbbbeae58
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5472636145974a6e10fa5af7b149bed773731c73
ef65fe9ba949d79e0d61ed67e5197027f3a5221b
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package top.niunaijun.blackobfuscator

import com.android.build.gradle.AppExtension
import com.android.build.gradle.api.ApplicationVariant
import com.android.build.gradle.internal.api.ReadOnlyProductFlavor
import com.android.build.gradle.internal.dsl.BuildType
import com.android.build.gradle.internal.dsl.ProductFlavor
import com.android.build.gradle.internal.tasks.DexMergingTask
Expand All @@ -13,13 +14,15 @@ public class ObfPlugin implements Plugin<Project> {
private String PLUGIN_NAME = "BlackObfuscator"
private Project mProject
public static BlackObfuscatorExtension sObfuscatorExtension
public Map<String, String> mTaskMapping = new HashMap<>()

void apply(Project project) {
this.mProject = project
def android = project.extensions.findByType(AppExtension)
project.configurations.create(PLUGIN_NAME).extendsFrom(project.configurations.implementation)
sObfuscatorExtension = project.extensions.create(PLUGIN_NAME, BlackObfuscatorExtension, project)

mTaskMapping.clear()
project.afterEvaluate {
System.out.println("=====BlackObfuscator=====")
System.out.println(sObfuscatorExtension.toString())
Expand All @@ -36,46 +39,36 @@ public class ObfPlugin implements Plugin<Project> {
task.getOutputs().getFiles().collect().each() { element ->
def file = new File(element.toString())
ObfDex.obf(file.getAbsolutePath(),
sObfuscatorExtension.depth, sObfuscatorExtension.obfClass, sObfuscatorExtension.blackClass)
sObfuscatorExtension.depth,
sObfuscatorExtension.obfClass,
sObfuscatorExtension.blackClass,
mTaskMapping.get(task.name))
}
}
}
List<Task> tasks = new ArrayList<>()
addTask("mergeDexRelease", tasks)
addTask("mergeDexDebug", tasks)
addTask("mergeLibDexDebug", tasks)
addTask("mergeProjectDexDebug", tasks)
addTask("transformDexArchiveWithDexMergerForDebug", tasks)
addTask("transformDexArchiveWithDexMergerForRelease", tasks)
addTask("minifyReleaseWithR8", tasks)
addTask("minifyDebugWithR8", tasks)

List<String> buildTypes = new ArrayList<>()
List<String> productFlavors = new ArrayList<>()
if (android != null) {
android.applicationVariants.all(new Action<ApplicationVariant>() {
@Override
void execute(ApplicationVariant applicationVariant) {
def name = upperCaseFirst(applicationVariant.buildType.name)
buildTypes.add(name)
}
})
android.productFlavors.all(new Action<ProductFlavor>() {
@Override
void execute(ProductFlavor productFlavor) {
def name = upperCaseFirst(productFlavor.name)
productFlavors.add(name)
File mappingFile = null
if (applicationVariant.buildType.minifyEnabled) {
mappingFile = applicationVariant.mappingFile
}
def buildType = upperCaseFirst(applicationVariant.buildType.name)
boolean empty = true
for (ReadOnlyProductFlavor flavor : applicationVariant.productFlavors) {
def flavorName = upperCaseFirst(flavor.name)
addOtherTask(tasks, flavorName, buildType, mappingFile)
empty = false
}
if (empty) {
addOtherTask(tasks, "", buildType, mappingFile)
}
}
})
}

for (String buildType : buildTypes) {
for (String product : productFlavors) {
addOtherTask(tasks, product, buildType)
}
addOtherTask(tasks, "", buildType)
}

for (Task task : tasks) {
task.doLast(action)
}
Expand All @@ -85,26 +78,29 @@ public class ObfPlugin implements Plugin<Project> {
}
}

private void addOtherTask(List<Task> tasks, String name, String buildType) {
addTask("mergeDex${name}${buildType}", tasks)
addTask("mergeLibDex${name}${buildType}", tasks)
addTask("mergeProjectDex${name}${buildType}", tasks)
addTask("transformDexArchiveWithDexMergerFor${name}${buildType}", tasks)
addTask("minify${name}${buildType}WithR8", tasks)
private void addOtherTask(List<Task> tasks, String name, String buildType, File mappingFile) {
addTask("mergeDex${name}${buildType}", tasks, mappingFile)
addTask("mergeLibDex${name}${buildType}", tasks, mappingFile)
addTask("mergeProjectDex${name}${buildType}", tasks, mappingFile)
addTask("transformDexArchiveWithDexMergerFor${name}${buildType}", tasks, mappingFile)
addTask("minify${name}${buildType}WithR8", tasks, mappingFile)

println("$name$buildType mappingFile $mappingFile")
}

private String upperCaseFirst(String val) {
char[] arr = val.toCharArray();
arr[0] = Character.toUpperCase(arr[0]);
return new String(arr);
char[] arr = val.toCharArray()
arr[0] = Character.toUpperCase(arr[0])
return new String(arr)
}

private void addTask(String name, List<Task> tasks) {
private void addTask(String name, List<Task> tasks, File mappingFile) {
try {
//Protected code
Task task = mProject.tasks.getByName(name)
if (!tasks.contains(task)) {
tasks.add(task)
mTaskMapping.put(task.name, mappingFile.absolutePath)
println("add Task $name")
}
} catch(UnknownTaskException e1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package top.niunaijun.blackobfuscator.core;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by Milk on 2022/1/12.
* * ∧_∧
* (`・ω・∥
* 丶 つ0
* しーJ
* 此处无Bug
*/
public class Mapping {
private File mFile;
private final Map<String, String> mapping = new HashMap<>();

public Mapping(String file) {
if (file == null)
return;
mFile = new File(file);
parser();
}

private void parser() {
if (mFile == null || !mFile.exists())
return;
try {
List<String> strings = FileUtils.readLines(mFile, Charset.defaultCharset());
for (String string : strings) {
if (string.startsWith("#") || string.startsWith(" ")) {
continue;
}
String[] split = string.split(" -> ");
if (split.length != 2) {
continue;
}
// System.out.println("add mapping : " + string);
mapping.put(split[0], split[1].substring(0, split[1].length() - 1));
}
} catch (IOException ignored) {
}
}

public String get(String origClazz) {
return mapping.get(origClazz);
}

public Map<String, String> getMapping() {
return mapping;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import com.googlecode.dex2jar.tools.Jar2Dex;

import org.jf.DexLib2Utils;
import org.jf.util.TrieTree;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import top.niunaijun.obfuscator.ObfuscatorConfiguration;

Expand All @@ -19,25 +22,26 @@
* 此处无Bug
*/
public class ObfDex {
public static void obf(String dir, int depth, String[] obfClass, String[] blackClass) {
public static void obf(String dir, int depth, String[] obfClass, String[] blackClass, String mappingFile) {
File file = new File(dir);
Mapping mapping = new Mapping(mappingFile);
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files == null)
return;
for (File input : files) {
if (input.isFile()) {
handleDex(input, depth, obfClass, blackClass);
handleDex(input, depth, obfClass, blackClass, mapping);
} else {
obf(input.getAbsolutePath(), depth, obfClass, blackClass);
obf(input.getAbsolutePath(), depth, obfClass, blackClass, mappingFile);
}
}
} else {
handleDex(file, depth, obfClass, blackClass);
handleDex(file, depth, obfClass, blackClass, mapping);
}
}

private static void handleDex(File input, int depth, String[] obfClass, String[] blackClass) {
private static void handleDex(File input, int depth, String[] obfClass, String[] blackClass, Mapping mapping) {
if (!input.getAbsolutePath().endsWith(".dex"))
return;
File tempJar = null;
Expand All @@ -47,7 +51,34 @@ private static void handleDex(File input, int depth, String[] obfClass, String[]
tempJar = new File(input.getParent(), System.currentTimeMillis() + "obf" + input.getName() + ".jar");
splitDex = new File(input.getParent(), System.currentTimeMillis() + "split" + input.getName() + ".dex");
obfDex = new File(input.getParent(), System.currentTimeMillis() + "obf" + input.getName() + ".dex");
long l = DexLib2Utils.splitDex(input, splitDex, Arrays.asList(obfClass), Arrays.asList(blackClass));
List<String> obfClassList = arrayToList(obfClass);
List<String> blackClassList = arrayToList(blackClass);

TrieTree whiteListTree = new TrieTree();
whiteListTree.addAll(obfClassList);

for (String aClass : mapping.getMapping().keySet()) {
if (whiteListTree.search(aClass)) {
String orig = mapping.get(aClass);
if (orig != null) {
System.out.println("mapping : " + aClass + " ---> " + orig);
obfClassList.add(orig);
}
}
}

TrieTree blackListTree = new TrieTree();
blackListTree.addAll(blackClassList);
for (String aClass : blackClassList) {
if (blackListTree.search(aClass)) {
String orig = mapping.get(aClass);
if (orig != null) {
System.out.println("mapping : " + aClass + " ---> " + orig);
blackClassList.add(orig);
}
}
}
long l = DexLib2Utils.splitDex(input, splitDex, obfClassList, blackClassList);
if (l <= 0) {
System.out.println("Obfuscator Class not found");
return;
Expand Down Expand Up @@ -75,4 +106,9 @@ public boolean accept(String className, String methodName) {
obfDex.delete();
}
}

private static List<String> arrayToList(String[] array) {
List<String> list = Arrays.asList(array);
return new ArrayList<>(list);
}
}

0 comments on commit f8a5b27

Please sign in to comment.