2012-09-20 03:27:35 +02:00
|
|
|
/**
|
|
|
|
* Copyright 2011 Ryszard Wiśniewski <brut.alll@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package brut.androlib;
|
|
|
|
|
|
|
|
import brut.androlib.java.AndrolibJava;
|
|
|
|
import brut.androlib.res.AndrolibResources;
|
|
|
|
import brut.androlib.res.data.ResPackage;
|
|
|
|
import brut.androlib.res.data.ResTable;
|
|
|
|
import brut.androlib.res.util.ExtFile;
|
|
|
|
import brut.androlib.src.SmaliBuilder;
|
|
|
|
import brut.androlib.src.SmaliDecoder;
|
|
|
|
import brut.common.BrutException;
|
|
|
|
import brut.directory.*;
|
|
|
|
import brut.util.BrutIO;
|
|
|
|
import brut.util.OS;
|
|
|
|
import java.io.*;
|
2013-05-04 02:16:34 +02:00
|
|
|
import java.net.URI;
|
2013-07-26 23:36:26 +02:00
|
|
|
import java.net.URISyntaxException;
|
2013-05-04 02:16:34 +02:00
|
|
|
import java.nio.file.*;
|
|
|
|
import java.nio.file.Path;
|
2013-03-31 16:04:12 +02:00
|
|
|
import java.util.*;
|
2012-09-20 03:27:35 +02:00
|
|
|
import java.util.logging.Logger;
|
2013-03-31 16:04:12 +02:00
|
|
|
import java.util.zip.ZipEntry;
|
2013-05-04 02:16:34 +02:00
|
|
|
import java.nio.file.Files;
|
2012-09-20 03:27:35 +02:00
|
|
|
import org.yaml.snakeyaml.DumperOptions;
|
|
|
|
import org.yaml.snakeyaml.Yaml;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
|
|
|
|
*/
|
|
|
|
public class Androlib {
|
2013-02-13 04:12:17 +01:00
|
|
|
private final AndrolibResources mAndRes = new AndrolibResources();
|
|
|
|
|
|
|
|
public ResTable getResTable(ExtFile apkFile) throws AndrolibException {
|
|
|
|
return mAndRes.getResTable(apkFile, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ResTable getResTable(ExtFile apkFile, boolean loadMainPkg)
|
|
|
|
throws AndrolibException {
|
|
|
|
return mAndRes.getResTable(apkFile, loadMainPkg);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decodeSourcesRaw(ExtFile apkFile, File outDir, boolean debug)
|
|
|
|
throws AndrolibException {
|
|
|
|
try {
|
|
|
|
Directory apk = apkFile.getDirectory();
|
|
|
|
LOGGER.info("Copying raw classes.dex file...");
|
|
|
|
apkFile.getDirectory().copyToDir(outDir, "classes.dex");
|
|
|
|
} catch (DirectoryException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-04 22:04:26 +02:00
|
|
|
public void decodeSourcesSmali(File apkFile, File outDir, boolean debug, String debugLinePrefix,
|
2013-06-21 15:19:43 +02:00
|
|
|
boolean bakdeb, int api) throws AndrolibException {
|
2013-02-13 04:12:17 +01:00
|
|
|
try {
|
|
|
|
File smaliDir = new File(outDir, SMALI_DIRNAME);
|
|
|
|
OS.rmdir(smaliDir);
|
|
|
|
smaliDir.mkdirs();
|
|
|
|
LOGGER.info("Baksmaling...");
|
2013-06-21 15:19:43 +02:00
|
|
|
SmaliDecoder.decode(apkFile, smaliDir, debug, debugLinePrefix, bakdeb, api);
|
2013-02-13 04:12:17 +01:00
|
|
|
} catch (BrutException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decodeSourcesJava(ExtFile apkFile, File outDir, boolean debug)
|
|
|
|
throws AndrolibException {
|
|
|
|
LOGGER.info("Decoding Java sources...");
|
|
|
|
new AndrolibJava().decode(apkFile, outDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decodeManifestRaw(ExtFile apkFile, File outDir)
|
|
|
|
throws AndrolibException {
|
|
|
|
try {
|
|
|
|
Directory apk = apkFile.getDirectory();
|
|
|
|
LOGGER.info("Copying raw manifest...");
|
|
|
|
apkFile.getDirectory().copyToDir(outDir, APK_MANIFEST_FILENAMES);
|
|
|
|
} catch (DirectoryException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decodeManifestFull(ExtFile apkFile, File outDir,
|
|
|
|
ResTable resTable) throws AndrolibException {
|
|
|
|
mAndRes.decodeManifest(resTable, apkFile, outDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decodeResourcesRaw(ExtFile apkFile, File outDir)
|
|
|
|
throws AndrolibException {
|
|
|
|
try {
|
|
|
|
// Directory apk = apkFile.getDirectory();
|
|
|
|
LOGGER.info("Copying raw resources...");
|
|
|
|
apkFile.getDirectory().copyToDir(outDir, APK_RESOURCES_FILENAMES);
|
|
|
|
} catch (DirectoryException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decodeResourcesFull(ExtFile apkFile, File outDir,
|
|
|
|
ResTable resTable) throws AndrolibException {
|
|
|
|
mAndRes.decode(resTable, apkFile, outDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decodeRawFiles(ExtFile apkFile, File outDir)
|
|
|
|
throws AndrolibException {
|
|
|
|
LOGGER.info("Copying assets and libs...");
|
|
|
|
try {
|
|
|
|
Directory in = apkFile.getDirectory();
|
|
|
|
if (in.containsDir("assets")) {
|
|
|
|
in.copyToDir(outDir, "assets");
|
|
|
|
}
|
|
|
|
if (in.containsDir("lib")) {
|
|
|
|
in.copyToDir(outDir, "lib");
|
|
|
|
}
|
2013-05-10 15:24:24 +02:00
|
|
|
if (in.containsDir("libs")) {
|
|
|
|
in.copyToDir(outDir, "libs");
|
|
|
|
}
|
2013-02-13 04:12:17 +01:00
|
|
|
} catch (DirectoryException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-31 16:04:12 +02:00
|
|
|
private boolean isAPKFileNames(String file) {
|
|
|
|
for (String apkFile : APK_STANDARD_ALL_FILENAMES) {
|
|
|
|
if (apkFile.equals(file) || file.startsWith(apkFile)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-01 00:13:10 +02:00
|
|
|
public void decodeUnknownFiles(ExtFile apkFile, File outDir, ResTable resTable)
|
2013-03-31 16:04:12 +02:00
|
|
|
throws AndrolibException {
|
|
|
|
LOGGER.info("Copying unknown files/dir...");
|
|
|
|
File unknownOut = new File(outDir, UNK_DIRNAME);
|
2013-04-01 00:13:10 +02:00
|
|
|
ZipEntry invZipFile;
|
2013-03-31 16:04:12 +02:00
|
|
|
|
|
|
|
// have to use container of ZipFile to help identify compression type
|
|
|
|
// with regular looping of apkFile for easy copy
|
|
|
|
try {
|
|
|
|
Directory unk = apkFile.getDirectory();
|
2013-04-06 19:50:42 +02:00
|
|
|
ZipExtFile apkZipFile = new ZipExtFile(apkFile.getAbsolutePath());
|
2013-03-31 16:04:12 +02:00
|
|
|
|
2013-04-01 00:13:10 +02:00
|
|
|
// loop all items in container recursively, ignoring any that are pre-defined by aapt
|
|
|
|
Set<String> files = unk.getFiles(true);
|
2013-03-31 16:04:12 +02:00
|
|
|
for (String file : files) {
|
|
|
|
if (!isAPKFileNames(file)) {
|
2013-04-01 00:13:10 +02:00
|
|
|
|
|
|
|
// copy file out of archive into special "unknown" folder
|
|
|
|
// to be re-included on build
|
2013-03-31 16:04:12 +02:00
|
|
|
unk.copyToDir(unknownOut,file);
|
2013-04-01 00:13:10 +02:00
|
|
|
try {
|
2013-04-06 19:50:42 +02:00
|
|
|
// ignore encryption
|
|
|
|
apkZipFile.getEntry(file.toString()).getGeneralPurposeBit().useEncryption(false);
|
2013-04-01 00:13:10 +02:00
|
|
|
invZipFile = apkZipFile.getEntry(file.toString());
|
|
|
|
|
|
|
|
// lets record the name of the file, and its compression type
|
|
|
|
// so that we may re-include it the same way
|
|
|
|
if (invZipFile != null) {
|
|
|
|
resTable.addUnknownFileInfo(invZipFile.getName(), String.valueOf(invZipFile.getMethod()));
|
|
|
|
}
|
|
|
|
} catch (NullPointerException ignored) {
|
|
|
|
|
|
|
|
}
|
2013-03-31 16:04:12 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-12 14:20:07 +02:00
|
|
|
apkZipFile.close();
|
2013-03-31 16:04:12 +02:00
|
|
|
}
|
|
|
|
catch (DirectoryException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
catch (IOException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-17 01:50:00 +01:00
|
|
|
public void writeOriginalFiles(ExtFile apkFile, File outDir)
|
|
|
|
throws AndrolibException {
|
|
|
|
LOGGER.info("Copying original files...");
|
|
|
|
File originalDir = new File(outDir, "original");
|
|
|
|
if (!originalDir.exists()) {
|
|
|
|
originalDir.mkdirs();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Directory in = apkFile.getDirectory();
|
|
|
|
if(in.containsFile("AndroidManifest.xml")) {
|
|
|
|
in.copyToDir(originalDir, "AndroidManifest.xml");
|
|
|
|
}
|
|
|
|
if (in.containsDir("META-INF")) {
|
|
|
|
in.copyToDir(originalDir, "META-INF");
|
|
|
|
}
|
|
|
|
} catch (DirectoryException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 04:12:17 +01:00
|
|
|
public void writeMetaFile(File mOutDir, Map<String, Object> meta)
|
|
|
|
throws AndrolibException {
|
|
|
|
DumperOptions options = new DumperOptions();
|
|
|
|
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
|
|
// options.setIndent(4);
|
|
|
|
Yaml yaml = new Yaml(options);
|
|
|
|
|
|
|
|
FileWriter writer = null;
|
|
|
|
try {
|
|
|
|
writer = new FileWriter(new File(mOutDir, "apktool.yml"));
|
|
|
|
yaml.dump(meta, writer);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
} finally {
|
|
|
|
if (writer != null) {
|
|
|
|
try {
|
|
|
|
writer.close();
|
|
|
|
} catch (IOException ex) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Object> readMetaFile(ExtFile appDir)
|
|
|
|
throws AndrolibException {
|
|
|
|
InputStream in = null;
|
|
|
|
try {
|
|
|
|
in = appDir.getDirectory().getFileInput("apktool.yml");
|
|
|
|
Yaml yaml = new Yaml();
|
|
|
|
return (Map<String, Object>) yaml.load(in);
|
|
|
|
} catch (DirectoryException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
} finally {
|
|
|
|
if (in != null) {
|
|
|
|
try {
|
|
|
|
in.close();
|
|
|
|
} catch (IOException ex) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void build(File appDir, File outFile,
|
2013-03-21 03:36:20 +01:00
|
|
|
HashMap<String, Boolean> flags, String aaptPath)
|
2013-02-13 04:12:17 +01:00
|
|
|
throws BrutException {
|
2013-03-21 03:36:20 +01:00
|
|
|
build(new ExtFile(appDir), outFile, flags, aaptPath);
|
2013-02-13 04:12:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void build(ExtFile appDir, File outFile,
|
2013-03-21 03:36:20 +01:00
|
|
|
HashMap<String, Boolean> flags, String aaptPath)
|
2013-02-13 04:12:17 +01:00
|
|
|
throws BrutException {
|
|
|
|
|
2013-09-18 04:52:09 +02:00
|
|
|
LOGGER.info("Using Apktool " + Androlib.getVersion() + " on " + appDir.getName());
|
|
|
|
|
|
|
|
mAaptPath = aaptPath;
|
2013-02-13 04:12:17 +01:00
|
|
|
Map<String, Object> meta = readMetaFile(appDir);
|
|
|
|
Object t1 = meta.get("isFrameworkApk");
|
|
|
|
flags.put("framework", t1 == null ? false : (Boolean) t1);
|
|
|
|
flags.put("compression", meta.get("compressionType") == null ? false
|
2013-07-08 19:27:55 +02:00
|
|
|
: Boolean.valueOf(meta.get("compressionType").toString()));
|
2013-02-13 04:12:17 +01:00
|
|
|
mAndRes.setSdkInfo((Map<String, String>) meta.get("sdkInfo"));
|
2013-05-12 17:15:36 +02:00
|
|
|
mAndRes.setPackageId((Map<String, String>) meta.get("packageInfo"));
|
2013-03-21 14:58:14 +01:00
|
|
|
mAndRes.setVersionInfo((Map<String, String>) meta.get("versionInfo"));
|
2013-02-13 04:12:17 +01:00
|
|
|
|
|
|
|
if (outFile == null) {
|
|
|
|
String outFileName = (String) meta.get("apkFileName");
|
|
|
|
outFile = new File(appDir, "dist" + File.separator
|
|
|
|
+ (outFileName == null ? "out.apk" : outFileName));
|
|
|
|
}
|
|
|
|
|
|
|
|
new File(appDir, APK_DIRNAME).mkdirs();
|
|
|
|
buildSources(appDir, flags);
|
|
|
|
buildResources(appDir, flags,
|
|
|
|
(Map<String, Object>) meta.get("usesFramework"));
|
|
|
|
buildLib(appDir, flags);
|
2013-03-17 01:50:00 +01:00
|
|
|
buildCopyOriginalFiles(appDir, flags);
|
2013-02-13 04:12:17 +01:00
|
|
|
buildApk(appDir, outFile, flags);
|
2013-04-01 00:13:10 +02:00
|
|
|
|
|
|
|
// we must go after the Apk is built, and copy the files in via Zip
|
|
|
|
// this is because Aapt won't add files it doesn't know (ex unknown files)
|
|
|
|
buildUnknownFiles(appDir,outFile,meta);
|
2013-02-13 04:12:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void buildSources(File appDir, HashMap<String, Boolean> flags)
|
|
|
|
throws AndrolibException {
|
|
|
|
if (!buildSourcesRaw(appDir, flags)
|
|
|
|
&& !buildSourcesSmali(appDir, flags)
|
|
|
|
&& !buildSourcesJava(appDir, flags)) {
|
|
|
|
LOGGER.warning("Could not find sources");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean buildSourcesRaw(File appDir, HashMap<String, Boolean> flags)
|
|
|
|
throws AndrolibException {
|
|
|
|
try {
|
|
|
|
File working = new File(appDir, "classes.dex");
|
|
|
|
if (!working.exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
File stored = new File(appDir, APK_DIRNAME + "/classes.dex");
|
|
|
|
if (flags.get("forceBuildAll") || isModified(working, stored)) {
|
|
|
|
LOGGER.info("Copying classes.dex file...");
|
|
|
|
BrutIO.copyAndClose(new FileInputStream(working),
|
|
|
|
new FileOutputStream(stored));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (IOException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean buildSourcesSmali(File appDir, HashMap<String, Boolean> flags)
|
|
|
|
throws AndrolibException {
|
|
|
|
ExtFile smaliDir = new ExtFile(appDir, "smali");
|
|
|
|
if (!smaliDir.exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
File dex = new File(appDir, APK_DIRNAME + "/classes.dex");
|
|
|
|
if (!flags.get("forceBuildAll")) {
|
|
|
|
LOGGER.info("Checking whether sources has changed...");
|
|
|
|
}
|
|
|
|
if (flags.get("forceBuildAll") || isModified(smaliDir, dex)) {
|
|
|
|
LOGGER.info("Smaling...");
|
|
|
|
dex.delete();
|
|
|
|
SmaliBuilder.build(smaliDir, dex, flags);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean buildSourcesJava(File appDir, HashMap<String, Boolean> flags)
|
|
|
|
throws AndrolibException {
|
|
|
|
File javaDir = new File(appDir, "src");
|
|
|
|
if (!javaDir.exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
File dex = new File(appDir, APK_DIRNAME + "/classes.dex");
|
|
|
|
if (!flags.get("forceBuildAll")) {
|
|
|
|
LOGGER.info("Checking whether sources has changed...");
|
|
|
|
}
|
|
|
|
if (flags.get("forceBuildAll") || isModified(javaDir, dex)) {
|
|
|
|
LOGGER.info("Building java sources...");
|
|
|
|
dex.delete();
|
|
|
|
new AndrolibJava().build(javaDir, dex);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void buildResources(ExtFile appDir, HashMap<String, Boolean> flags,
|
|
|
|
Map<String, Object> usesFramework) throws BrutException {
|
|
|
|
if (!buildResourcesRaw(appDir, flags)
|
|
|
|
&& !buildResourcesFull(appDir, flags, usesFramework)
|
|
|
|
&& !buildManifest(appDir, flags, usesFramework)) {
|
|
|
|
LOGGER.warning("Could not find resources");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean buildResourcesRaw(ExtFile appDir,
|
|
|
|
HashMap<String, Boolean> flags) throws AndrolibException {
|
|
|
|
try {
|
|
|
|
if (!new File(appDir, "resources.arsc").exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
File apkDir = new File(appDir, APK_DIRNAME);
|
|
|
|
if (!flags.get("forceBuildAll")) {
|
|
|
|
LOGGER.info("Checking whether resources has changed...");
|
|
|
|
}
|
|
|
|
if (flags.get("forceBuildAll")
|
|
|
|
|| isModified(newFiles(APK_RESOURCES_FILENAMES, appDir),
|
|
|
|
newFiles(APK_RESOURCES_FILENAMES, apkDir))) {
|
|
|
|
LOGGER.info("Copying raw resources...");
|
|
|
|
appDir.getDirectory()
|
|
|
|
.copyToDir(apkDir, APK_RESOURCES_FILENAMES);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (DirectoryException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean buildResourcesFull(File appDir,
|
|
|
|
HashMap<String, Boolean> flags, Map<String, Object> usesFramework)
|
|
|
|
throws AndrolibException {
|
|
|
|
try {
|
|
|
|
if (!new File(appDir, "res").exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!flags.get("forceBuildAll")) {
|
|
|
|
LOGGER.info("Checking whether resources has changed...");
|
|
|
|
}
|
|
|
|
File apkDir = new File(appDir, APK_DIRNAME);
|
|
|
|
if (flags.get("forceBuildAll")
|
|
|
|
|| isModified(newFiles(APP_RESOURCES_FILENAMES, appDir),
|
|
|
|
newFiles(APK_RESOURCES_FILENAMES, apkDir))) {
|
|
|
|
LOGGER.info("Building resources...");
|
|
|
|
|
|
|
|
File apkFile = File.createTempFile("APKTOOL", null);
|
|
|
|
apkFile.delete();
|
|
|
|
|
|
|
|
File ninePatch = new File(appDir, "9patch");
|
|
|
|
if (!ninePatch.exists()) {
|
|
|
|
ninePatch = null;
|
|
|
|
}
|
|
|
|
mAndRes.aaptPackage(apkFile, new File(appDir,
|
|
|
|
"AndroidManifest.xml"), new File(appDir, "res"),
|
|
|
|
ninePatch, null, parseUsesFramework(usesFramework),
|
|
|
|
flags, mAaptPath);
|
|
|
|
|
|
|
|
Directory tmpDir = new ExtFile(apkFile).getDirectory();
|
|
|
|
tmpDir.copyToDir(apkDir,
|
|
|
|
tmpDir.containsDir("res") ? APK_RESOURCES_FILENAMES
|
|
|
|
: APK_RESOURCES_WITHOUT_RES_FILENAMES);
|
|
|
|
|
|
|
|
// delete tmpDir
|
|
|
|
apkFile.delete();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (IOException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
} catch (DirectoryException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
} catch (BrutException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean buildManifestRaw(ExtFile appDir,
|
|
|
|
HashMap<String, Boolean> flags) throws AndrolibException {
|
|
|
|
try {
|
|
|
|
File apkDir = new File(appDir, APK_DIRNAME);
|
|
|
|
LOGGER.info("Copying raw AndroidManifest.xml...");
|
|
|
|
appDir.getDirectory().copyToDir(apkDir, APK_MANIFEST_FILENAMES);
|
|
|
|
return true;
|
|
|
|
} catch (DirectoryException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean buildManifest(ExtFile appDir,
|
|
|
|
HashMap<String, Boolean> flags, Map<String, Object> usesFramework)
|
|
|
|
throws BrutException {
|
|
|
|
try {
|
|
|
|
if (!new File(appDir, "AndroidManifest.xml").exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!flags.get("forceBuildAll")) {
|
|
|
|
LOGGER.info("Checking whether resources has changed...");
|
|
|
|
}
|
2013-02-16 14:32:39 +01:00
|
|
|
|
2013-02-13 04:12:17 +01:00
|
|
|
File apkDir = new File(appDir, APK_DIRNAME);
|
2013-02-16 14:32:39 +01:00
|
|
|
|
|
|
|
if (flags.get("debug")) {
|
|
|
|
mAndRes.remove_application_debug(new File(apkDir,"AndroidManifest.xml").getAbsolutePath());
|
|
|
|
}
|
|
|
|
|
2013-02-13 04:12:17 +01:00
|
|
|
if (flags.get("forceBuildAll")
|
|
|
|
|| isModified(newFiles(APK_MANIFEST_FILENAMES, appDir),
|
|
|
|
newFiles(APK_MANIFEST_FILENAMES, apkDir))) {
|
|
|
|
LOGGER.info("Building AndroidManifest.xml...");
|
|
|
|
|
|
|
|
File apkFile = File.createTempFile("APKTOOL", null);
|
|
|
|
apkFile.delete();
|
|
|
|
|
|
|
|
File ninePatch = new File(appDir, "9patch");
|
|
|
|
if (!ninePatch.exists()) {
|
|
|
|
ninePatch = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAndRes.aaptPackage(apkFile, new File(appDir,
|
|
|
|
"AndroidManifest.xml"), null, ninePatch, null,
|
|
|
|
parseUsesFramework(usesFramework), flags, mAaptPath);
|
|
|
|
|
|
|
|
Directory tmpDir = new ExtFile(apkFile).getDirectory();
|
|
|
|
tmpDir.copyToDir(apkDir, APK_MANIFEST_FILENAMES);
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (IOException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
} catch (DirectoryException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
} catch (AndrolibException ex) {
|
|
|
|
LOGGER.warning("Parse AndroidManifest.xml failed, treat it as raw file.");
|
|
|
|
return buildManifestRaw(appDir, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void buildLib(File appDir, HashMap<String, Boolean> flags)
|
|
|
|
throws AndrolibException {
|
|
|
|
File working = new File(appDir, "lib");
|
|
|
|
if (!working.exists()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
File stored = new File(appDir, APK_DIRNAME + "/lib");
|
|
|
|
if (flags.get("forceBuildAll") || isModified(working, stored)) {
|
|
|
|
LOGGER.info("Copying libs...");
|
|
|
|
try {
|
|
|
|
OS.rmdir(stored);
|
|
|
|
OS.cpdir(working, stored);
|
|
|
|
} catch (BrutException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-17 01:50:00 +01:00
|
|
|
public void buildCopyOriginalFiles(File appDir,
|
|
|
|
HashMap<String, Boolean> flags) throws AndrolibException {
|
|
|
|
if (flags.get("copyOriginal")) {
|
|
|
|
File originalDir = new File(appDir, "original");
|
|
|
|
if(originalDir.exists()) {
|
|
|
|
try {
|
|
|
|
LOGGER.info("Copy original files...");
|
|
|
|
Directory in = (new ExtFile(originalDir)).getDirectory();
|
|
|
|
if(in.containsFile("AndroidManifest.xml")) {
|
|
|
|
LOGGER.info("Copy AndroidManifest.xml...");
|
|
|
|
in.copyToDir(new File(appDir, APK_DIRNAME), "AndroidManifest.xml");
|
|
|
|
}
|
|
|
|
if (in.containsDir("META-INF")) {
|
|
|
|
LOGGER.info("Copy META-INF...");
|
|
|
|
in.copyToDir(new File(appDir, APK_DIRNAME), "META-INF");
|
|
|
|
}
|
|
|
|
} catch (DirectoryException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-01 00:13:10 +02:00
|
|
|
public void buildUnknownFiles(File appDir, File outFile, Map<String, Object> meta)
|
2013-09-07 15:53:07 +02:00
|
|
|
throws AndrolibException {
|
2013-05-04 02:16:34 +02:00
|
|
|
File file;
|
|
|
|
mPath = Paths.get(appDir.getPath() + File.separatorChar + UNK_DIRNAME);
|
2013-03-21 14:58:14 +01:00
|
|
|
|
2013-04-01 00:13:10 +02:00
|
|
|
if (meta.containsKey("unknownFiles")) {
|
|
|
|
LOGGER.info("Copying unknown files/dir...");
|
2013-03-21 14:58:14 +01:00
|
|
|
|
2013-04-01 00:13:10 +02:00
|
|
|
Map<String, String> files = (Map<String, String>)meta.get("unknownFiles");
|
2013-03-21 14:58:14 +01:00
|
|
|
|
2013-04-01 00:13:10 +02:00
|
|
|
try {
|
2013-05-04 02:16:34 +02:00
|
|
|
// set our filesystem options
|
|
|
|
Map<String, String> zip_properties = new HashMap<>();
|
|
|
|
zip_properties.put("create", "false");
|
|
|
|
zip_properties.put("encoding", "UTF-8");
|
|
|
|
|
|
|
|
// create filesystem
|
2013-05-24 14:55:33 +02:00
|
|
|
Path path = Paths.get(outFile.getAbsolutePath());
|
2013-07-26 23:36:26 +02:00
|
|
|
URI apkFileSystem = new URI("jar", path.toUri().toString(), null);
|
2013-05-04 02:16:34 +02:00
|
|
|
|
2013-09-07 15:53:07 +02:00
|
|
|
// loop through files inside
|
|
|
|
for (Map.Entry<String,String> entry : files.entrySet()) {
|
2013-05-04 02:16:34 +02:00
|
|
|
|
2013-09-07 15:53:07 +02:00
|
|
|
file = new File(mPath.toFile(), entry.getKey());
|
|
|
|
if (file.isFile() && file.exists()) {
|
|
|
|
insertFolder(apkFileSystem, zip_properties, file.getParentFile(), entry.getValue(), mPath.toAbsolutePath());
|
|
|
|
insertFile(apkFileSystem, zip_properties, file, entry.getValue(), mPath.toAbsolutePath());
|
2013-04-01 00:13:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
2013-07-26 23:36:26 +02:00
|
|
|
} catch (URISyntaxException ex) {
|
|
|
|
throw new AndrolibException(ex);
|
2013-04-01 00:13:10 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-04 02:16:34 +02:00
|
|
|
}
|
|
|
|
|
2013-09-07 15:53:07 +02:00
|
|
|
private void insertFile(URI apkFileSystem, Map<String,String> zip_properties, File insert, String method, Path location)
|
2013-05-04 02:16:34 +02:00
|
|
|
throws AndrolibException, IOException {
|
|
|
|
|
2013-09-07 15:53:07 +02:00
|
|
|
// ZipFileSystem only writes at .close()
|
|
|
|
// http://mail.openjdk.java.net/pipermail/nio-dev/2012-July/001764.html
|
|
|
|
try(FileSystem fs = FileSystems.newFileSystem(apkFileSystem, zip_properties)) {
|
|
|
|
|
|
|
|
Path root = fs.getPath("/");
|
|
|
|
|
|
|
|
// in order to get the path relative to the zip, we strip off the absolute path, minus what we
|
|
|
|
// already have in the zip. thus /var/files/apktool/apk/unknown/folder/file => /folder/file
|
|
|
|
Path dest = fs.getPath(root.toString() + insert.getAbsolutePath().replace(location.toString(),""));
|
|
|
|
Path newFile = Paths.get(insert.getAbsolutePath());
|
|
|
|
Files.copy(newFile,dest, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
fs.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void insertFolder(URI apkFileSystem, Map<String,String> zip_properties, File insert, String method, Path location)
|
|
|
|
throws AndrolibException, IOException {
|
|
|
|
|
|
|
|
try(FileSystem fs = FileSystems.newFileSystem(apkFileSystem, zip_properties)) {
|
|
|
|
|
|
|
|
Path root = fs.getPath("/");
|
|
|
|
Path dest = fs.getPath(root.toString() + insert.getAbsolutePath().replace(location.toString(),""));
|
|
|
|
Path parent = dest.normalize();
|
|
|
|
|
|
|
|
// check for folder existing in apkFileSystem
|
|
|
|
if (parent != null && Files.notExists(parent)) {
|
|
|
|
if (!Files.isDirectory(parent, LinkOption.NOFOLLOW_LINKS)) {
|
|
|
|
Files.createDirectories(parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fs.close();
|
2013-05-04 02:16:34 +02:00
|
|
|
}
|
2013-03-21 14:58:14 +01:00
|
|
|
}
|
|
|
|
|
2013-02-13 04:12:17 +01:00
|
|
|
public void buildApk(File appDir, File outApk,
|
|
|
|
HashMap<String, Boolean> flags) throws AndrolibException {
|
|
|
|
LOGGER.info("Building apk file...");
|
|
|
|
if (outApk.exists()) {
|
|
|
|
outApk.delete();
|
|
|
|
} else {
|
|
|
|
File outDir = outApk.getParentFile();
|
|
|
|
if (outDir != null && !outDir.exists()) {
|
|
|
|
outDir.mkdirs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
File assetDir = new File(appDir, "assets");
|
|
|
|
if (!assetDir.exists()) {
|
|
|
|
assetDir = null;
|
|
|
|
}
|
|
|
|
mAndRes.aaptPackage(outApk, null, null, new File(appDir, APK_DIRNAME),
|
|
|
|
assetDir, null, flags, mAaptPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void publicizeResources(File arscFile) throws AndrolibException {
|
|
|
|
mAndRes.publicizeResources(arscFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void installFramework(File frameFile, String tag, String frame_path)
|
|
|
|
throws AndrolibException {
|
|
|
|
mAndRes.setFrameworkFolder(frame_path);
|
|
|
|
mAndRes.installFramework(frameFile, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isFrameworkApk(ResTable resTable) {
|
|
|
|
for (ResPackage pkg : resTable.listMainPackages()) {
|
|
|
|
if (pkg.getId() < 64) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getVersion() {
|
|
|
|
String version = ApktoolProperties.get("application.version");
|
|
|
|
return version.endsWith("-SNAPSHOT") ? version.substring(0,
|
|
|
|
version.length() - 9)
|
|
|
|
+ '.' + ApktoolProperties.get("git.commit.id.abbrev") : version;
|
|
|
|
}
|
|
|
|
|
|
|
|
private File[] parseUsesFramework(Map<String, Object> usesFramework)
|
|
|
|
throws AndrolibException {
|
|
|
|
if (usesFramework == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Integer> ids = (List<Integer>) usesFramework.get("ids");
|
|
|
|
if (ids == null || ids.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
String tag = (String) usesFramework.get("tag");
|
|
|
|
File[] files = new File[ids.size()];
|
|
|
|
int i = 0;
|
|
|
|
for (int id : ids) {
|
|
|
|
files[i++] = mAndRes.getFrameworkApk(id, tag);
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isModified(File working, File stored) {
|
|
|
|
if (!stored.exists()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return BrutIO.recursiveModifiedTime(working) > BrutIO
|
|
|
|
.recursiveModifiedTime(stored);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isModified(File[] working, File[] stored) {
|
|
|
|
for (int i = 0; i < stored.length; i++) {
|
|
|
|
if (!stored[i].exists()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BrutIO.recursiveModifiedTime(working) > BrutIO
|
|
|
|
.recursiveModifiedTime(stored);
|
|
|
|
}
|
|
|
|
|
|
|
|
private File[] newFiles(String[] names, File dir) {
|
|
|
|
File[] files = new File[names.length];
|
|
|
|
for (int i = 0; i < names.length; i++) {
|
|
|
|
files[i] = new File(dir, names[i]);
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
2013-03-14 22:50:27 +01:00
|
|
|
|
|
|
|
public void setFrameworkFolder(String path) {
|
|
|
|
mAndRes.setFrameworkFolder(path);
|
|
|
|
}
|
2013-02-13 04:12:17 +01:00
|
|
|
|
|
|
|
private String mAaptPath = null;
|
2013-05-04 02:16:34 +02:00
|
|
|
private Path mPath = null;
|
2013-02-13 04:12:17 +01:00
|
|
|
|
|
|
|
private final static Logger LOGGER = Logger.getLogger(Androlib.class
|
|
|
|
.getName());
|
|
|
|
|
|
|
|
private final static String SMALI_DIRNAME = "smali";
|
|
|
|
private final static String APK_DIRNAME = "build/apk";
|
2013-03-31 16:04:12 +02:00
|
|
|
private final static String UNK_DIRNAME = "unknown";
|
2013-02-13 04:12:17 +01:00
|
|
|
private final static String[] APK_RESOURCES_FILENAMES = new String[] {
|
|
|
|
"resources.arsc", "AndroidManifest.xml", "res" };
|
|
|
|
private final static String[] APK_RESOURCES_WITHOUT_RES_FILENAMES = new String[] {
|
|
|
|
"resources.arsc", "AndroidManifest.xml" };
|
|
|
|
private final static String[] APP_RESOURCES_FILENAMES = new String[] {
|
|
|
|
"AndroidManifest.xml", "res" };
|
2013-03-31 16:04:12 +02:00
|
|
|
private final static String[] APK_MANIFEST_FILENAMES = new String[] {
|
|
|
|
"AndroidManifest.xml" };
|
|
|
|
private final static String[] APK_STANDARD_ALL_FILENAMES = new String[] {
|
2013-05-10 15:24:24 +02:00
|
|
|
"classes.dex", "AndroidManifest.xml", "resources.arsc","res","lib", "libs","assets","META-INF" };
|
2012-09-20 03:27:35 +02:00
|
|
|
}
|