Skip to content

Commit

Permalink
Update ARSCLib-1.3.0
Browse files Browse the repository at this point in the history
 - Dex file info printing
 - Dump/build with archive-info
 - fixes #80
  • Loading branch information
REAndroid committed Dec 29, 2023
1 parent 957c5c9 commit ca87f27
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 46 deletions.
Binary file modified libs/ARSCLib.jar
Binary file not shown.
Binary file modified libs/smali.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.reandroid.apkeditor.common;

import com.reandroid.apk.APKLogger;
import com.reandroid.app.AndroidManifest;
import com.reandroid.arsc.chunk.xml.AndroidManifestBlock;
import com.reandroid.arsc.chunk.xml.ResXmlAttribute;
import com.reandroid.arsc.chunk.xml.ResXmlElement;
Expand All @@ -34,7 +35,7 @@ public static List<ResXmlElement> listSplitRequired(ResXmlElement parentElement)
return EmptyList.of();
}
return CollectionUtil.toList(parentElement.getElements(element -> {
if(!element.equalsName(AndroidManifestBlock.TAG_meta_data)){
if(!element.equalsName(AndroidManifest.TAG_meta_data)){
return false;
}
ResXmlAttribute nameAttribute = CollectionUtil.getFirst(element
Expand Down Expand Up @@ -65,7 +66,7 @@ public static void removeAttributeFromManifestAndApplication(AndroidManifestBloc
}
int removed = manifestElement.removeAttributesWithId(resourceId);
ResXmlElement applicationElement = manifestElement.getElementByTagName(
AndroidManifestBlock.TAG_application);
AndroidManifest.TAG_application);
if(removed > 1){
if(logger != null){
logger.logMessage("Duplicate attributes on <manifest> removed: "
Expand Down Expand Up @@ -109,8 +110,8 @@ public static String getNamedValue(ResXmlElement element) {
return attribute.decodeValue();
}
static boolean isNameResourceId(ResXmlAttribute attribute){
int resourceId = attribute.getNameResourceID();
return resourceId == AndroidManifestBlock.ID_name;
int resourceId = attribute.getNameId();
return resourceId == AndroidManifest.ID_name;
}
static final Predicate<ResXmlAttribute> NAME_FILTER = attribute -> {
if(!isNameResourceId(attribute)){
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/reandroid/apkeditor/compile/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import com.reandroid.apkeditor.BaseCommand;
import com.reandroid.apkeditor.Util;
import com.reandroid.apkeditor.smali.SmaliCompiler;
import com.reandroid.app.AndroidManifest;
import com.reandroid.archive.ArchiveFile;
import com.reandroid.archive.block.ApkSignatureBlock;
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;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -176,14 +176,14 @@ public static void execute(String[] args) throws ARGException, IOException {
builder.run();
}
private static boolean isRawInDir(File dir){
File file=new File(dir, AndroidManifestBlock.FILE_NAME_BIN);
File file=new File(dir, AndroidManifest.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);
File manifest = new File(dir, AndroidManifest.FILE_NAME);
return manifest.isFile();
}
private static boolean isJsonInDir(File dir) {
Expand Down Expand Up @@ -220,11 +220,7 @@ private static boolean isModuleDir(File dir){
if(!dir.isDirectory()){
return false;
}
File manifest=new File(dir,AndroidManifestBlock.FILE_NAME+".json");
if(manifest.isFile()){
return true;
}
File file=new File(dir, "AndroidManifest.xml.json");
File file = new File(dir, AndroidManifest.FILE_NAME_JSON);
return file.isFile();
}
public static boolean isCommand(String command){
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/com/reandroid/apkeditor/info/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.reandroid.arsc.coder.ReferenceString;
import com.reandroid.arsc.coder.ValueCoder;
import com.reandroid.arsc.model.ResourceEntry;
import com.reandroid.dex.model.DexDirectory;
import com.reandroid.utils.HexUtil;
import com.reandroid.arsc.value.AttributeDataFormat;
import com.reandroid.arsc.value.Entry;
Expand Down Expand Up @@ -72,7 +73,7 @@ private void print(ApkModule apkModule) throws IOException {
printSourceFile();

printPackage(apkModule);
AndroidManifestBlock manifest = apkModule.getAndroidManifestBlock();
AndroidManifestBlock manifest = apkModule.getAndroidManifest();
printVersionCode(manifest);
printVersionName(manifest);
printMinSdkVersion(manifest);
Expand All @@ -88,6 +89,7 @@ private void print(ApkModule apkModule) throws IOException {
printResList(apkModule);

printResources(apkModule);
printDex(apkModule);
}
private void printSourceFile() throws IOException {
InfoOptions options = getOptions();
Expand All @@ -114,6 +116,15 @@ private void printResources(ApkModule apkModule) throws IOException {
infoWriter.writeResources(packageBlock, options.typeFilterList, writeEntries);
}
}
private void printDex(ApkModule apkModule) throws IOException {
InfoOptions options = getOptions();
if(!options.dex){
return;
}
InfoWriter infoWriter = getInfoWriter();
DexDirectory dexDirectory = DexDirectory.readStrings(apkModule.getZipEntryMap());
infoWriter.writeDexInfo(dexDirectory);
}
private void printResList(ApkModule apkModule) throws IOException {
InfoOptions options = getOptions();
if(options.resList.size() == 0){
Expand Down Expand Up @@ -160,7 +171,7 @@ private void printPackage(ApkModule apkModule) throws IOException {
if(!options.packageName){
return;
}
AndroidManifestBlock manifest = apkModule.getAndroidManifestBlock();
AndroidManifestBlock manifest = apkModule.getAndroidManifest();
if(manifest != null){
getInfoWriter().writeNameValue("package" , manifest.getPackageName());
}
Expand Down Expand Up @@ -204,7 +215,7 @@ private void printAppName(ApkModule apkModule) throws IOException {
if(!options.appName){
return;
}
AndroidManifestBlock manifest = apkModule.getAndroidManifestBlock();
AndroidManifestBlock manifest = apkModule.getAndroidManifest();
ResXmlElement application = manifest.getApplicationElement();
ResXmlAttribute attributeLabel = application
.searchAttributeByResourceId(AndroidManifestBlock.ID_label);
Expand All @@ -223,7 +234,7 @@ private void printAppIcon(ApkModule apkModule) throws IOException {
if(!options.appIcon){
return;
}
AndroidManifestBlock manifest = apkModule.getAndroidManifestBlock();
AndroidManifestBlock manifest = apkModule.getAndroidManifest();
ResXmlElement application = manifest.getApplicationElement();
ResXmlAttribute attribute = application
.searchAttributeByResourceId(AndroidManifestBlock.ID_icon);
Expand All @@ -242,7 +253,7 @@ private void printAppRoundIcon(ApkModule apkModule) throws IOException {
if(!options.appRoundIcon){
return;
}
AndroidManifestBlock manifest = apkModule.getAndroidManifestBlock();
AndroidManifestBlock manifest = apkModule.getAndroidManifest();
ResXmlElement application = manifest.getApplicationElement();
int id_roundIcon = 0x0101052c;
ResXmlAttribute attribute = application
Expand All @@ -262,7 +273,7 @@ private void printUsesPermissions(ApkModule apkModule) throws IOException {
if(!options.permissions || !options.verbose){
return;
}
AndroidManifestBlock manifest = apkModule.getAndroidManifestBlock();
AndroidManifestBlock manifest = apkModule.getAndroidManifest();
if(manifest == null){
return;
}
Expand All @@ -280,7 +291,7 @@ private void printActivities(ApkModule apkModule) throws IOException {
if(!options.activities){
return;
}
AndroidManifestBlock manifest = apkModule.getAndroidManifestBlock();
AndroidManifestBlock manifest = apkModule.getAndroidManifest();
if(manifest == null){
return;
}
Expand Down Expand Up @@ -309,7 +320,7 @@ private void printAppClass(ApkModule apkModule) throws IOException {
if(!options.appClass){
return;
}
AndroidManifestBlock manifest = apkModule.getAndroidManifestBlock();
AndroidManifestBlock manifest = apkModule.getAndroidManifest();
if(manifest == null){
return;
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/reandroid/apkeditor/info/InfoOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class InfoOptions extends Options {
public final List<String> resList;
public boolean resources = false;
public final List<String> typeFilterList;
public boolean dex = false;
public InfoOptions(){
super();
this.resList = new ArrayList<>();
Expand Down Expand Up @@ -75,6 +76,7 @@ public void parse(String[] args) throws ARGException {
appClass = containsArg(ARG_app_class, args, appClass);
activities = containsArg(ARG_activities, args, activities);
resources = containsArg(ARG_resources, args, false);
dex = containsArg(ARG_dex, args, false);



Expand Down Expand Up @@ -132,6 +134,7 @@ private void initializeDefaults(String[] args){
permissions = false;
activities = false;
appClass = false;
dex = false;
}
}
private void parseInput(String[] args) throws ARGException {
Expand Down Expand Up @@ -180,6 +183,8 @@ public static String getHelp(){
new String[]{ARG_activities, ARG_DESC_activities},
new String[]{ARG_resources, ARG_DESC_resources},
new String[]{" ", " "},
new String[]{ARG_dex, ARG_DESC_dex},
new String[]{" ", " "},
new String[]{ARG_ALL_help, ARG_DESC_help}
};
StringHelper.printTwoColumns(builder, " ", PRINT_WIDTH, table);
Expand Down Expand Up @@ -266,6 +271,9 @@ public static String getHelp(){
"\n *This applies only when flag '-resources' used." +
"\n *Can be multiple";

private static final String ARG_dex = "-dex";
private static final String ARG_DESC_dex = "Prints dex information";


private static final String[] availableTypes = new String[]{TYPE_TEXT, TYPE_JSON, TYPE_XML};
}
8 changes: 8 additions & 0 deletions src/main/java/com/reandroid/apkeditor/info/InfoWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.reandroid.arsc.coder.ValueCoder;
import com.reandroid.arsc.container.SpecTypePair;
import com.reandroid.arsc.model.ResourceEntry;
import com.reandroid.dex.model.DexDirectory;
import com.reandroid.dex.model.DexFile;
import com.reandroid.utils.HexUtil;
import com.reandroid.arsc.value.*;

Expand All @@ -40,6 +42,12 @@ public void writeResources(PackageBlock packageBlock, List<String> typeFilters,
writeResources(resourceEntry, writeEntries);
}
}
public void writeDexInfo(DexDirectory dexDirectory) throws IOException {
for(DexFile dexFile : dexDirectory){
writeDexInfo(dexFile, true);
}
}
public abstract void writeDexInfo(DexFile dexFile, boolean writeSectionInfo) throws IOException;
public abstract void writeResources(ResourceEntry resourceEntry, boolean writeEntries) throws IOException;
public abstract void writePackageNames(Collection<PackageBlock> packageBlocks) throws IOException;
public abstract void writeEntries(String name, List<Entry> entryList) throws IOException;
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/com/reandroid/apkeditor/info/InfoWriterJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.reandroid.arsc.value.ResTableMapEntry;
import com.reandroid.arsc.value.ResValue;
import com.reandroid.arsc.value.ResValueMap;
import com.reandroid.dex.model.DexFile;
import com.reandroid.dex.sections.Marker;
import com.reandroid.json.JSONWriter;

import java.io.IOException;
Expand All @@ -33,13 +35,26 @@

public class InfoWriterJson extends InfoWriter{
private final JSONWriter mJsonWriter;

public InfoWriterJson(Writer writer) {
super(writer);
JSONWriter jsonWriter = new JSONWriter(writer);
jsonWriter = jsonWriter.array();
this.mJsonWriter = jsonWriter;
}

@Override
public void writeDexInfo(DexFile dexFile, boolean writeSectionInfo) throws IOException {
JSONWriter jsonWriter = mJsonWriter.object()
.key("name").value(dexFile.getFileName())
.key("version").value(dexFile.getVersion())
.key("markers").array();
List<Marker> markersList = dexFile.getMarkers();
for(Marker marker : markersList){
jsonWriter.value(marker.getJsonObject());
}
jsonWriter.endArray().endObject();
}
@Override
public void writeResources(PackageBlock packageBlock, List<String> typeFilters, boolean writeEntries) throws IOException {
packageBlock.sortTypes();
Expand Down Expand Up @@ -126,7 +141,7 @@ private void writeBagEntry(Entry entry) {
private void writeValueMap(ResValueMap resValueMap){
mJsonWriter.object()
.key("name").value(resValueMap.decodeName())
.key("id").value(resValueMap.getNameResourceID())
.key("id").value(resValueMap.getNameId())
.key("value").value(getValueAsString(resValueMap))
.endObject();
}
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/com/reandroid/apkeditor/info/InfoWriterText.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import com.reandroid.arsc.value.ResTableMapEntry;
import com.reandroid.arsc.value.ResValue;
import com.reandroid.arsc.value.ResValueMap;
import com.reandroid.dex.model.DexFile;
import com.reandroid.dex.sections.MapItem;
import com.reandroid.dex.sections.MapList;
import com.reandroid.dex.sections.Marker;
import com.reandroid.utils.HexUtil;

import java.io.IOException;
Expand All @@ -36,6 +40,33 @@ public InfoWriterText(Writer writer) {
super(writer);
}


@Override
public void writeDexInfo(DexFile dexFile, boolean writeSectionInfo) throws IOException {
Writer writer = getWriter();
writer.write("\n");
writeNameValue("Name", dexFile.getFileName());
writeNameValue("Version", dexFile.getVersion());
List<Marker> markersList = dexFile.getMarkers();
if(markersList.size() != 0){
writer.write("Markers:");
for(Marker marker : markersList){
writer.write("\n");
writer.write(ARRAY_TAB);
writer.write(marker.toString());
}
}
writer.write("\n");
MapList mapList = dexFile.getDexLayout().getMapList();
writer.write("Sections:");
for(MapItem mapItem : mapList){
writer.write("\n");
writer.write(ARRAY_TAB);
writer.write(mapItem.toString());
}
writer.write("\n");
writer.flush();
}
@Override
public void writeResources(PackageBlock packageBlock, List<String> typeFilters, boolean writeEntries) throws IOException {
Writer writer = getWriter();
Expand Down Expand Up @@ -138,7 +169,7 @@ private void writeValueMap(ResValueMap resValueMap) throws IOException {
writer.write(name);
writer.write("(");
}
writer.write(HexUtil.toHex8(resValueMap.getNameResourceID()));
writer.write(HexUtil.toHex8(resValueMap.getNameId()));
if(name != null){
writer.write(")");
}
Expand Down
Loading

0 comments on commit ca87f27

Please sign in to comment.