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

@ -82,7 +82,11 @@ public class ARSCDecoder {
for (int i = 0; i < packageCount; i++) { for (int i = 0; i < packageCount; i++) {
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 {