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
*.setting
bin/
*.iml

View File

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

View File

@ -53,30 +53,31 @@ public class XmlPullStreamDecoder implements ResStreamDecoder {
if ("manifest".equalsIgnoreCase(pp.getName())) {
try {
hidePackageInfo = parseManifest(pp);
if (hidePackageInfo) {
return;
}
} catch (AndrolibException e) {}
}
if ("uses-sdk".equalsIgnoreCase(pp.getName())) {
}else if ("uses-sdk".equalsIgnoreCase(pp.getName())) {
try {
hideSdkInfo = parseAttr(pp);
if(hideSdkInfo) {
return;
}
} catch (AndrolibException e) {}
}
} 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;
}
super.event(pp);
}
private boolean parseManifest(XmlPullParser pp) throws AndrolibException {
ResTable restable = resTable;
// @todo read <manifest> for package:
return false;
// read <manifest> for package:
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 {