Skip to content

Commit

Permalink
Allow to keep specific classes and resources
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Jun 1, 2024
1 parent eca3c9a commit 0d54699
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
Binary file modified libs/ARSCLib.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class DecompileOptions extends Options {
public boolean noDexDebug;
public boolean dexMarkers;

public File keepClassListFile;
public File keepResourceNameListFile;

public DecompileOptions(){
type=TYPE_XML;
}
Expand All @@ -50,6 +53,8 @@ public void parse(String[] args) throws ARGException {
if(signaturesDirectory == null && type == null){
type = TYPE_XML;
}
parseKeepClassList(args);
parseKeepResourceNameList(args);
super.parse(args);
}
private void parseKeepResPath(String[] args) {
Expand Down Expand Up @@ -105,6 +110,34 @@ private void parseInput(String[] args) throws ARGException {
}
this.inputFile=file;
}
private void parseKeepClassList(String[] args) throws ARGException {
if(!APKEditor.isExperimental()) {
return;
}
this.keepClassListFile = null;
File file = parseFile(ARG_keep_classes, args);
if(file == null){
return;
}
if(!file.isFile()){
throw new ARGException("No such file: "+file);
}
this.keepClassListFile = file;
}
private void parseKeepResourceNameList(String[] args) throws ARGException {
if(!APKEditor.isExperimental()) {
return;
}
this.keepResourceNameListFile = null;
File file = parseFile(ARG_keep_resources, args);
if(file == null){
return;
}
if(!file.isFile()){
throw new ARGException("No such file: "+file);
}
this.keepResourceNameListFile = file;
}

@Override
public String toString(){
Expand Down Expand Up @@ -168,7 +201,9 @@ public static String getHelp(){
new String[]{ARG_split_resources, ARG_DESC_split_resources},
new String[]{ARG_validate_res_dir, ARG_DESC_validate_res_dir},
new String[]{ARG_no_dex_debug, ARG_DESC_no_dex_debug},
new String[]{ARG_dex_markers, ARG_DESC_dex_markers}
new String[]{ARG_dex_markers, ARG_DESC_dex_markers},
new String[]{ARG_keep_classes, ARG_DESC_keep_classes},
new String[]{ARG_keep_resources, ARG_DESC_keep_resources}
};
StringHelper.printTwoColumns(builder, " ", Options.PRINT_WIDTH, table);
String jar = APKEditor.getJarName();
Expand Down Expand Up @@ -218,4 +253,10 @@ public static String getHelp(){
private static final String ARG_dex_markers = "-dex-markers";
private static final String ARG_DESC_dex_markers = "Dumps dex markers (applies only when smali mode)";

private static final String ARG_keep_classes = "-keep-classes";
private static final String ARG_DESC_keep_classes = "(Beta) File containing list of class names to keep, Names should be in dalvik format.";

private static final String ARG_keep_resources = "-keep-resources";
private static final String ARG_DESC_keep_resources = "(Beta) File containing list of resources names to keep, Names should be in reference name format. e.g: @string/app_name";

}
32 changes: 28 additions & 4 deletions src/main/java/com/reandroid/apkeditor/decompile/Decompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.reandroid.common.DiagnosticsReporter;
import com.reandroid.commons.command.ARGException;
import com.reandroid.dex.model.DexDirectory;
import com.reandroid.graph.ApkBuildOption;
import com.reandroid.graph.ApkBuilder;

import java.io.File;
Expand Down Expand Up @@ -81,21 +82,44 @@ private void runFullApkRebuildExperimental(ApkModule apkModule) throws IOExcepti
DiagnosticsReporter reporter = new DiagnosticsReporter() {
@Override
public void report(DiagnosticMessage diagnosticMessage) {
logMessage(diagnosticMessage.toString());
DiagnosticMessage.Type type = diagnosticMessage.type();
String log = diagnosticMessage.toString();
if(type == DiagnosticMessage.Type.DEBUG) {
logVerbose(log);
}else {
logMessage(log);
}
}
@Override
public boolean isVerboseEnabled() {
return false;
return true;
}
@Override
public boolean isDebugEnabled() {
return false;
return true;
}
};
logMessage("Rebuilding apk with new resource id generation ...");
ApkBuilder builder = new ApkBuilder(apkModule, directory);
builder.setReporter(reporter);
builder.build();
ApkBuildOption apkBuildOption = builder.getBuildOption();

File keepClassListFile = getOptions().keepClassListFile;
if(keepClassListFile != null) {
apkBuildOption.readKeepClassesList(keepClassListFile);
}
File keepResourceNameListFile = getOptions().keepResourceNameListFile;
if(keepResourceNameListFile != null) {
apkBuildOption.readKeepResourceNameList(keepResourceNameListFile);
}

// TODO: set values from user command args
apkBuildOption.setMinifyClasses(true);
apkBuildOption.setMinifyMethods(false);
apkBuildOption.setMinifyFields(true);
apkBuildOption.setMinifyResources(true);

builder.apply();
logMessage("Refreshing and merging ...");
directory.refresh();

Expand Down

0 comments on commit 0d54699

Please sign in to comment.