properly store package information (manifest AND resources.arsc info) in apktool.yml for renamed packages

This commit is contained in:
Connor Tumbleson 2012-12-13 21:14:41 -06:00
parent 4410e466f5
commit f065a5be92
6 changed files with 802 additions and 785 deletions

1
.gitignore vendored
View File

@ -26,3 +26,4 @@ build*
*.settings *.settings
*.setting *.setting
bin/ bin/
*.iml

View File

@ -32,258 +32,257 @@ import java.util.*;
* @author Ryszard Wiśniewski <brut.alll@gmail.com> * @author Ryszard Wiśniewski <brut.alll@gmail.com>
*/ */
public class ApkDecoder { public class ApkDecoder {
public ApkDecoder() { public ApkDecoder() {
this(new Androlib()); this(new Androlib());
} }
public ApkDecoder(Androlib androlib) { public ApkDecoder(Androlib androlib) {
mAndrolib = androlib; mAndrolib = androlib;
} }
public ApkDecoder(File apkFile) { public ApkDecoder(File apkFile) {
this(apkFile, new Androlib()); this(apkFile, new Androlib());
} }
public ApkDecoder(File apkFile, Androlib androlib) { public ApkDecoder(File apkFile, Androlib androlib) {
mAndrolib = androlib; mAndrolib = androlib;
setApkFile(apkFile); setApkFile(apkFile);
} }
public void setApkFile(File apkFile) { public void setApkFile(File apkFile) {
mApkFile = new ExtFile(apkFile); mApkFile = new ExtFile(apkFile);
mResTable = null; mResTable = null;
} }
public void setOutDir(File outDir) throws AndrolibException { public void setOutDir(File outDir) throws AndrolibException {
mOutDir = outDir; mOutDir = outDir;
} }
public void decode() throws AndrolibException { public void decode() throws AndrolibException {
File outDir = getOutDir(); File outDir = getOutDir();
if (! mForceDelete && outDir.exists()) { if (!mForceDelete && outDir.exists()) {
throw new OutDirExistsException(); throw new OutDirExistsException();
} }
if (! mApkFile.isFile() || ! mApkFile.canRead() ) { if (!mApkFile.isFile() || !mApkFile.canRead()) {
throw new InFileNotFoundException(); throw new InFileNotFoundException();
} }
try { try {
OS.rmdir(outDir); OS.rmdir(outDir);
} catch (BrutException ex) { } catch (BrutException ex) {
throw new AndrolibException(ex); throw new AndrolibException(ex);
} }
outDir.mkdirs(); outDir.mkdirs();
if (hasSources()) { if (hasSources()) {
switch (mDecodeSources) { switch (mDecodeSources) {
case DECODE_SOURCES_NONE: case DECODE_SOURCES_NONE:
mAndrolib.decodeSourcesRaw(mApkFile, outDir, mDebug); mAndrolib.decodeSourcesRaw(mApkFile, outDir, mDebug);
break; break;
case DECODE_SOURCES_SMALI: case DECODE_SOURCES_SMALI:
mAndrolib.decodeSourcesSmali(mApkFile, outDir, mDebug, mBakDeb); mAndrolib.decodeSourcesSmali(mApkFile, outDir, mDebug, mBakDeb);
break; break;
case DECODE_SOURCES_JAVA: case DECODE_SOURCES_JAVA:
mAndrolib.decodeSourcesJava(mApkFile, outDir, mDebug); mAndrolib.decodeSourcesJava(mApkFile, outDir, mDebug);
break; break;
} }
} }
if (hasResources()) { if (hasResources()) {
switch (mDecodeResources) { switch (mDecodeResources) {
case DECODE_RESOURCES_NONE: case DECODE_RESOURCES_NONE:
mAndrolib.decodeResourcesRaw(mApkFile, outDir); mAndrolib.decodeResourcesRaw(mApkFile, outDir);
break; break;
case DECODE_RESOURCES_FULL: case DECODE_RESOURCES_FULL:
mAndrolib.decodeResourcesFull(mApkFile, outDir, mAndrolib.decodeResourcesFull(mApkFile, outDir, getResTable());
getResTable()); break;
break; }
} } else {
} else { // if there's no resources.asrc, decode the manifest without looking
// if there's no resources.asrc, decode the manifest without looking up // up
// attribute references // attribute references
if (hasManifest()) { if (hasManifest()) {
switch (mDecodeResources) { switch (mDecodeResources) {
case DECODE_RESOURCES_NONE: case DECODE_RESOURCES_NONE:
mAndrolib.decodeManifestRaw(mApkFile, outDir); mAndrolib.decodeManifestRaw(mApkFile, outDir);
break; break;
case DECODE_RESOURCES_FULL: case DECODE_RESOURCES_FULL:
mAndrolib.decodeManifestFull(mApkFile, outDir, mAndrolib.decodeManifestFull(mApkFile, outDir,
getResTable()); getResTable());
break; break;
} }
} }
} }
mAndrolib.decodeRawFiles(mApkFile, outDir); mAndrolib.decodeRawFiles(mApkFile, outDir);
writeMetaFile(); writeMetaFile();
} }
public void setDecodeSources(short mode) throws AndrolibException { public void setDecodeSources(short mode) throws AndrolibException {
if (mode != DECODE_SOURCES_NONE && mode != DECODE_SOURCES_SMALI if (mode != DECODE_SOURCES_NONE && mode != DECODE_SOURCES_SMALI
&& mode != DECODE_SOURCES_JAVA) { && mode != DECODE_SOURCES_JAVA) {
throw new AndrolibException("Invalid decode sources mode: " + mode); throw new AndrolibException("Invalid decode sources mode: " + mode);
} }
mDecodeSources = mode; mDecodeSources = mode;
} }
public void setDecodeResources(short mode) throws AndrolibException { public void setDecodeResources(short mode) throws AndrolibException {
if (mode != DECODE_RESOURCES_NONE && mode != DECODE_RESOURCES_FULL) { if (mode != DECODE_RESOURCES_NONE && mode != DECODE_RESOURCES_FULL) {
throw new AndrolibException("Invalid decode resources mode"); throw new AndrolibException("Invalid decode resources mode");
} }
mDecodeResources = mode; mDecodeResources = mode;
} }
public void setDebugMode(boolean debug) { public void setDebugMode(boolean debug) {
mDebug = debug; mDebug = debug;
} }
public void setBaksmaliDebugMode(boolean bakdeb) { public void setBaksmaliDebugMode(boolean bakdeb) {
mBakDeb = bakdeb; mBakDeb = bakdeb;
} }
public void setForceDelete(boolean forceDelete) { public void setForceDelete(boolean forceDelete) {
mForceDelete = forceDelete; mForceDelete = forceDelete;
} }
public void setFrameworkTag(String tag) throws AndrolibException { public void setFrameworkTag(String tag) throws AndrolibException {
mFrameTag = tag; mFrameTag = tag;
if (mResTable != null) { if (mResTable != null) {
getResTable().setFrameTag(tag); getResTable().setFrameTag(tag);
} }
} }
public void setKeepBrokenResources(boolean keepBrokenResources) { public void setKeepBrokenResources(boolean keepBrokenResources) {
mKeepBrokenResources = keepBrokenResources; mKeepBrokenResources = keepBrokenResources;
} }
public void setFrameworkDir(String dir) { public void setFrameworkDir(String dir) {
mFrameworkDir = dir; mFrameworkDir = dir;
} }
public ResTable getResTable() throws AndrolibException { public ResTable getResTable() throws AndrolibException {
if (mResTable == null) { if (mResTable == null) {
boolean hasResources = hasResources(); boolean hasResources = hasResources();
boolean hasManifest = hasManifest(); boolean hasManifest = hasManifest();
if (! (hasManifest || hasResources)) { if (!(hasManifest || hasResources)) {
throw new AndrolibException( throw new AndrolibException(
"Apk doesn't contain either AndroidManifest.xml file or resources.arsc file"); "Apk doesn't contain either AndroidManifest.xml file or resources.arsc file");
} }
AndrolibResources.sKeepBroken = mKeepBrokenResources; AndrolibResources.sKeepBroken = mKeepBrokenResources;
AndrolibResources.sFrameworkFolder = mFrameworkDir; AndrolibResources.sFrameworkFolder = mFrameworkDir;
mResTable = mAndrolib.getResTable(mApkFile, hasResources); mResTable = mAndrolib.getResTable(mApkFile, hasResources);
mResTable.setFrameTag(mFrameTag); mResTable.setFrameTag(mFrameTag);
} }
return mResTable; return mResTable;
} }
public boolean hasSources() throws AndrolibException { public boolean hasSources() throws AndrolibException {
try { try {
return mApkFile.getDirectory().containsFile("classes.dex"); return mApkFile.getDirectory().containsFile("classes.dex");
} catch (DirectoryException ex) { } catch (DirectoryException ex) {
throw new AndrolibException(ex); throw new AndrolibException(ex);
} }
} }
public boolean hasManifest() throws AndrolibException { public boolean hasManifest() throws AndrolibException {
try { try {
return mApkFile.getDirectory().containsFile("AndroidManifest.xml"); return mApkFile.getDirectory().containsFile("AndroidManifest.xml");
} catch (DirectoryException ex) { } catch (DirectoryException ex) {
throw new AndrolibException(ex); throw new AndrolibException(ex);
} }
} }
public boolean hasResources() throws AndrolibException { public boolean hasResources() throws AndrolibException {
try { try {
return mApkFile.getDirectory().containsFile("resources.arsc"); return mApkFile.getDirectory().containsFile("resources.arsc");
} catch (DirectoryException ex) { } catch (DirectoryException ex) {
throw new AndrolibException(ex); throw new AndrolibException(ex);
} }
} }
public final static short DECODE_SOURCES_NONE = 0x0000; public final static short DECODE_SOURCES_NONE = 0x0000;
public final static short DECODE_SOURCES_SMALI = 0x0001; public final static short DECODE_SOURCES_SMALI = 0x0001;
public final static short DECODE_SOURCES_JAVA = 0x0002; public final static short DECODE_SOURCES_JAVA = 0x0002;
public final static short DECODE_RESOURCES_NONE = 0x0100; public final static short DECODE_RESOURCES_NONE = 0x0100;
public final static short DECODE_RESOURCES_FULL = 0x0101; public final static short DECODE_RESOURCES_FULL = 0x0101;
private File getOutDir() throws AndrolibException {
if (mOutDir == null) {
throw new AndrolibException("Out dir not set");
}
return mOutDir;
}
private File getOutDir() throws AndrolibException { private void writeMetaFile() throws AndrolibException {
if (mOutDir == null) { Map<String, Object> meta = new LinkedHashMap<String, Object>();
throw new AndrolibException("Out dir not set"); meta.put("version", Androlib.getVersion());
} meta.put("apkFileName", mApkFile.getName());
return mOutDir;
}
private void writeMetaFile() throws AndrolibException { if (mDecodeResources != DECODE_RESOURCES_NONE
Map<String, Object> meta = new LinkedHashMap<String, Object>(); && (hasManifest() || hasResources())) {
meta.put("version", Androlib.getVersion()); meta.put("isFrameworkApk",
meta.put("apkFileName", mApkFile.getName()); Boolean.valueOf(mAndrolib.isFrameworkApk(getResTable())));
putUsesFramework(meta);
putSdkInfo(meta);
putPackageInfo(meta);
}
if (mDecodeResources != DECODE_RESOURCES_NONE && (hasManifest() || hasResources())) { mAndrolib.writeMetaFile(mOutDir, meta);
meta.put("isFrameworkApk", }
Boolean.valueOf(mAndrolib.isFrameworkApk(getResTable())));
putUsesFramework(meta);
putSdkInfo(meta);
putPackageInfo(meta);
}
mAndrolib.writeMetaFile(mOutDir, meta); private void putUsesFramework(Map<String, Object> meta)
} throws AndrolibException {
Set<ResPackage> pkgs = getResTable().listFramePackages();
if (pkgs.isEmpty()) {
return;
}
private void putUsesFramework(Map<String, Object> meta) Integer[] ids = new Integer[pkgs.size()];
throws AndrolibException { int i = 0;
Set<ResPackage> pkgs = getResTable().listFramePackages(); for (ResPackage pkg : pkgs) {
if (pkgs.isEmpty()) { ids[i++] = pkg.getId();
return; }
} Arrays.sort(ids);
Integer[] ids = new Integer[pkgs.size()]; Map<String, Object> uses = new LinkedHashMap<String, Object>();
int i = 0; uses.put("ids", ids);
for (ResPackage pkg : pkgs) {
ids[i++] = pkg.getId();
}
Arrays.sort(ids);
Map<String, Object> uses = new LinkedHashMap<String, Object>(); if (mFrameTag != null) {
uses.put("ids", ids); uses.put("tag", mFrameTag);
}
if (mFrameTag != null) { meta.put("usesFramework", uses);
uses.put("tag", mFrameTag); }
}
meta.put("usesFramework", uses); private void putSdkInfo(Map<String, Object> meta) throws AndrolibException {
} Map<String, String> info = getResTable().getSdkInfo();
if (info.size() > 0) {
meta.put("sdkInfo", info);
}
}
private void putSdkInfo(Map<String, Object> meta) private void putPackageInfo(Map<String, Object> meta)
throws AndrolibException { throws AndrolibException {
Map<String, String> info = getResTable().getSdkInfo(); Map<String, String> info = getResTable().getPackageInfo();
if (info.size() > 0) { if (info.size() > 0) {
meta.put("sdkInfo", info); meta.put("packageInfo", info);
} }
} }
private void putPackageInfo(Map<String, Object> meta) private final Androlib mAndrolib;
throws AndrolibException {
Map<String, String> info = getResTable().getPackageInfo();
if (info.size() > 0) {
meta.put("packageInfo", info);
}
}
private final Androlib mAndrolib; private ExtFile mApkFile;
private File mOutDir;
private ExtFile mApkFile; private ResTable mResTable;
private File mOutDir; private short mDecodeSources = DECODE_SOURCES_SMALI;
private ResTable mResTable; private short mDecodeResources = DECODE_RESOURCES_FULL;
private short mDecodeSources = DECODE_SOURCES_SMALI; private boolean mDebug = false;
private short mDecodeResources = DECODE_RESOURCES_FULL; private boolean mForceDelete = false;
private boolean mDebug = false; private String mFrameTag;
private boolean mForceDelete = false; private boolean mKeepBrokenResources = false;
private String mFrameTag; private String mFrameworkDir = null;
private boolean mKeepBrokenResources = false; private boolean mBakDeb = true;
private String mFrameworkDir = null;
private boolean mBakDeb = true;
} }

View File

@ -143,4 +143,8 @@ public class ResTable {
public Map<String, String> getPackageInfo() { public Map<String, String> getPackageInfo() {
return mPackageInfo; return mPackageInfo;
} }
public boolean isPackageInfoValueSet(String key) {
return (mPackageInfo.containsKey(key));
}
} }

View File

@ -83,6 +83,10 @@ public class ARSCDecoder {
packages[i] = readPackage(); packages[i] = readPackage();
} }
// store package
if (this.mResTable.isPackageInfoValueSet("cur_package") != true) {
this.mResTable.addPackageInfo("cur_package", packages[0].getName());
}
return packages; return packages;
} }

View File

@ -53,30 +53,31 @@ public class XmlPullStreamDecoder implements ResStreamDecoder {
if ("manifest".equalsIgnoreCase(pp.getName())) { if ("manifest".equalsIgnoreCase(pp.getName())) {
try { try {
hidePackageInfo = parseManifest(pp); hidePackageInfo = parseManifest(pp);
if (hidePackageInfo) {
return;
}
} catch (AndrolibException e) {} } catch (AndrolibException e) {}
} }else if ("uses-sdk".equalsIgnoreCase(pp.getName())) {
if ("uses-sdk".equalsIgnoreCase(pp.getName())) {
try { try {
hideSdkInfo = parseAttr(pp); hideSdkInfo = parseAttr(pp);
if(hideSdkInfo) {
return;
}
} catch (AndrolibException e) {} } catch (AndrolibException e) {}
} }
} else if (hideSdkInfo && type == XmlPullParser.END_TAG && } else if (hideSdkInfo && type == XmlPullParser.END_TAG &&
"uses-sdk".equalsIgnoreCase(pp.getName())) { "uses-sdk".equalsIgnoreCase(pp.getName()) ||
hidePackageInfo && type == XmlPullParser.END_TAG &&
"manifest".equalsIgnoreCase(pp.getName())) {
return; return;
} }
super.event(pp); super.event(pp);
} }
private boolean parseManifest(XmlPullParser pp) throws AndrolibException { private boolean parseManifest(XmlPullParser pp) throws AndrolibException {
ResTable restable = resTable;
// @todo read <manifest> for package: // read <manifest> for package:
return false; for (int i = 0; i < pp.getAttributeCount(); i++) {
if (pp.getAttributeName(i).equalsIgnoreCase(("package"))) {
restable.addPackageInfo("orig_package", pp.getAttributeValue(i));
}
}
return true;
} }
private boolean parseAttr(XmlPullParser pp) throws AndrolibException { private boolean parseAttr(XmlPullParser pp) throws AndrolibException {