Skip to content

Commit

Permalink
introduce decompile/build as RAW type
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Jul 22, 2023
1 parent f5b1bba commit 5082403
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/reandroid/apkeditor/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected void parseType(String[] args) throws ARGException {
parseType(args, TYPE_JSON);
}
protected void parseType(String[] args, String def) throws ARGException {
String[] choices = new String[]{TYPE_JSON, TYPE_XML, TYPE_SIG};
String[] choices = new String[]{TYPE_JSON, TYPE_XML, TYPE_RAW, TYPE_SIG};
this.type = parseType(ARG_type, args, choices, def);
}
protected void parseSignaturesDir(String[] args) throws ARGException {
Expand Down Expand Up @@ -238,6 +238,7 @@ protected boolean containsArg(String argSwitch, boolean ignore_case, String[] ar

public static final String TYPE_SIG = "sig";
public static final String TYPE_JSON = "json";
public static final String TYPE_RAW = "raw";
public static final String TYPE_XML = "xml";
public static final String TYPE_TEXT = "text";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import com.reandroid.apkeditor.Options;
import com.reandroid.apkeditor.utils.StringHelper;
import com.reandroid.commons.command.ARGException;
import com.sun.org.apache.xpath.internal.Arg;

import java.io.File;

public class BuildOptions extends Options {
public boolean validateResDir;
public String resDirName;
public boolean isXml;
public boolean isRaw;
public boolean noCache;
public BuildOptions(){
}
Expand Down
52 changes: 44 additions & 8 deletions src/main/java/com/reandroid/apkeditor/compile/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import com.reandroid.apkeditor.smali.SmaliCompiler;
import com.reandroid.archive.ArchiveFile;
import com.reandroid.archive.block.ApkSignatureBlock;
import com.reandroid.archive.writer.ApkWriter;
import com.reandroid.archive.writer.ApkFileWriter;
import com.reandroid.arsc.chunk.TableBlock;
import com.reandroid.arsc.coder.xml.XmlCoder;
import com.reandroid.commons.command.ARGException;
import com.reandroid.arsc.chunk.xml.AndroidManifestBlock;
Expand All @@ -40,7 +41,9 @@ public void run() throws IOException {
restoreSignatures();
return;
}
if(options.isXml){
if(options.isRaw){
buildRaw();
}else if(options.isXml){
buildXml();
}else {
buildJson();
Expand All @@ -50,7 +53,7 @@ private void restoreSignatures() throws IOException {
logMessage("Restoring signatures ...");
BuildOptions options = getOptions();
ArchiveFile archive = new ArchiveFile(options.inputFile);
ApkWriter apkWriter = new ApkWriter(options.outputFile, archive.getInputSources());
ApkFileWriter apkWriter = new ApkFileWriter(options.outputFile, archive.getInputSources());
apkWriter.setAPKLogger(this);
ApkSignatureBlock apkSignatureBlock = new ApkSignatureBlock();
apkSignatureBlock.scanSplitFiles(options.signaturesDirectory);
Expand Down Expand Up @@ -117,13 +120,42 @@ public void buildXml() throws IOException {
loadedModule.close();
logMessage("Saved to: " + options.outputFile);
}
public void buildRaw() throws IOException {
logMessage("Scanning Raw directory ...");
ApkModuleRawEncoder encoder = new ApkModuleRawEncoder();
encoder.setApkLogger(this);

BuildOptions options = getOptions();

SmaliCompiler smaliCompiler = new SmaliCompiler(options.noCache);
smaliCompiler.setApkLogger(this);

encoder.setDexEncoder(smaliCompiler);
ApkModule loadedModule = encoder.getApkModule();
loadedModule.setAPKLogger(this);

loadedModule.setPreferredFramework(options.frameworkVersion);
if(options.frameworks != null){
for(File file : options.frameworks){
loadedModule.addExternalFramework(file);
}
}
encoder.scanDirectory(options.inputFile);
loadedModule = encoder.getApkModule();
logMessage("Writing apk...");
loadedModule.writeApk(options.outputFile, null);
loadedModule.close();
logMessage("Saved to: " + options.outputFile);
}
public static void execute(String[] args) throws ARGException, IOException {
if(Util.isHelp(args)){
throw new ARGException(BuildOptions.getHelp());
}
BuildOptions option=new BuildOptions();
option.parse(args);
if(isJsonInDir(option.inputFile)){
if(isRawInDir(option.inputFile)){
option.isRaw = true;
}else if(isJsonInDir(option.inputFile)){
option.inputFile = getJsonInDir(option.inputFile);
}else if (isXmlInDir(option.inputFile)){
option.isXml=true;
Expand All @@ -143,11 +175,15 @@ public static void execute(String[] args) throws ARGException, IOException {
}
builder.run();
}
private static boolean isXmlInDir(File dir){
File manifest=new File(dir, AndroidManifestBlock.FILE_NAME);
if(!manifest.isFile()){
manifest=new File(dir, AndroidManifestBlock.FILE_NAME_BIN);
private static boolean isRawInDir(File dir){
File file=new File(dir, AndroidManifestBlock.FILE_NAME_BIN);
if(!file.isFile()){
file = new File(dir, TableBlock.FILE_NAME);
}
return file.isFile();
}
private static boolean isXmlInDir(File dir){
File manifest = new File(dir, AndroidManifestBlock.FILE_NAME);
return manifest.isFile();
}
private static boolean isJsonInDir(File dir) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public static String getHelp(){
private static final String ARG_split_resources="-split-json";
private static final String ARG_DESC_split_resources="splits resources.arsc into multiple parts as per type entries (use this for large files)";

private static final String ARG_DESC_type = "Decode types: \n 1) json \n 2) xml \n 3) sig \n default=" + TYPE_XML;
private static final String ARG_DESC_type = "Decode types: \n 1) json \n 2) xml \n 3) raw \n 4) sig \n default=" + TYPE_XML;


private static final String ARG_keep_res_path = "-keep-res-path";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ private ApkModuleDecoder getApkModuleDecoder(ApkModule apkModule){
if(DecompileOptions.TYPE_JSON.equals(options.type)){
decoder = new ApkModuleJsonDecoder(apkModule, options.splitJson);
decoder.setDexDecoder(getSmaliDecompiler(apkModule.getTableBlock()));
}else if(DecompileOptions.TYPE_RAW.equals(options.type)){
decoder = new ApkModuleRawDecoder(apkModule);
decoder.setDexDecoder(getSmaliDecompiler(apkModule.getTableBlock()));
}else{
ApkModuleXmlDecoder xmlDecoder = new ApkModuleXmlDecoder(apkModule);
xmlDecoder.setKeepResPath(options.keepResPath);
Expand Down

0 comments on commit 5082403

Please sign in to comment.