Skip to content

Commit 58e5148

Browse files
authored
feat: staged aliases support (#4085)
1 parent ccf0c9d commit 58e5148

111 files changed

Lines changed: 1988 additions & 1565 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

brut.apktool/apktool-cli/src/main/java/brut/apktool/Main.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -797,58 +797,64 @@ private static void printVersion() {
797797
System.out.println(props.getVersion());
798798
}
799799

800-
private static void setupLogging(final Verbosity verbosity) {
801-
Logger logger = Logger.getLogger("");
802-
for (Handler handler : logger.getHandlers()) {
803-
logger.removeHandler(handler);
804-
}
800+
private static void setupLogging(Verbosity verbosity) {
805801
LogManager.getLogManager().reset();
802+
Logger logger = Logger.getLogger("");
806803

807804
if (verbosity == Verbosity.QUIET) {
805+
logger.setLevel(Level.OFF);
808806
return;
809807
}
810808

811809
Handler handler = new Handler() {
812810
@Override
813811
public void publish(LogRecord record) {
814-
if (getFormatter() == null) {
815-
setFormatter(new Formatter() {
816-
@Override
817-
public String format(LogRecord record) {
818-
return record.getLevel().toString().charAt(0) + ": "
819-
+ record.getMessage() + System.lineSeparator();
820-
}
821-
});
812+
if (!isLoggable(record)) {
813+
return;
822814
}
823-
824815
try {
825816
String message = getFormatter().format(record);
826817
int level = record.getLevel().intValue();
827818
if (level >= Level.WARNING.intValue()) {
828-
System.err.write(message.getBytes());
829-
} else if (level >= Level.INFO.intValue() || verbosity == Verbosity.VERBOSE) {
830-
System.out.write(message.getBytes());
819+
System.err.println(message);
820+
} else {
821+
System.out.println(message);
831822
}
832823
} catch (Exception ex) {
833824
reportError(null, ex, ErrorManager.FORMAT_FAILURE);
834825
}
835826
}
836827

837828
@Override
838-
public void close() throws SecurityException {
829+
public void flush() {
830+
System.out.flush();
831+
System.err.flush();
839832
}
840833

841834
@Override
842-
public void flush() {
835+
public void close() throws SecurityException {
836+
flush();
843837
}
844838
};
845-
839+
handler.setFormatter(new Formatter() {
840+
@Override
841+
public String format(LogRecord record) {
842+
String prefix;
843+
int level = record.getLevel().intValue();
844+
if (level >= Level.SEVERE.intValue()) {
845+
prefix = "E";
846+
} else if (level >= Level.WARNING.intValue()) {
847+
prefix = "W";
848+
} else if (level >= Level.INFO.intValue()) {
849+
prefix = "I";
850+
} else {
851+
prefix = "D";
852+
}
853+
return prefix + ": " + record.getMessage();
854+
}
855+
});
846856
logger.addHandler(handler);
847-
848-
if (verbosity == Verbosity.VERBOSE) {
849-
handler.setLevel(Level.ALL);
850-
logger.setLevel(Level.ALL);
851-
}
857+
logger.setLevel(verbosity == Verbosity.VERBOSE ? Level.ALL : Level.INFO);
852858
}
853859

854860
private static class Props extends Properties {

brut.apktool/apktool-lib/src/main/java/brut/androlib/ApkBuilder.java

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import brut.androlib.res.xml.ResXmlUtils;
2727
import brut.androlib.smali.SmaliBuilder;
2828
import brut.common.BrutException;
29+
import brut.common.Log;
2930
import brut.directory.Directory;
3031
import brut.directory.DirectoryException;
3132
import brut.directory.ExtFile;
@@ -41,11 +42,10 @@
4142
import java.nio.file.Files;
4243
import java.util.*;
4344
import java.util.concurrent.atomic.AtomicReference;
44-
import java.util.logging.Logger;
4545
import java.util.zip.ZipOutputStream;
4646

4747
public class ApkBuilder {
48-
private static final Logger LOGGER = Logger.getLogger(ApkBuilder.class.getName());
48+
private static final String TAG = ApkBuilder.class.getName();
4949

5050
private final ExtFile mApkDir;
5151
private final Config mConfig;
@@ -69,8 +69,7 @@ public void build(File outApk) throws AndrolibException {
6969
try {
7070
mApkInfo = ApkInfo.load(mApkDir);
7171
String minSdkVersion = mApkInfo.getSdkInfo().getMinSdkVersion();
72-
mSmaliBuilder = new SmaliBuilder(minSdkVersion != null
73-
? SdkInfo.parseSdkInt(minSdkVersion) : 0);
72+
mSmaliBuilder = new SmaliBuilder(minSdkVersion != null ? SdkInfo.parseSdkInt(minSdkVersion) : 0);
7473
mAaptInvoker = new AaptInvoker(mApkInfo, mConfig);
7574

7675
String apkName = mApkInfo.getApkFileName();
@@ -86,8 +85,8 @@ public void build(File outApk) throws AndrolibException {
8685
File outDir = new File(mApkDir, "build/apk");
8786
OS.mkdir(outDir);
8887

89-
LOGGER.info("Using Apktool " + mConfig.getVersion() + " on " + apkName
90-
+ (mWorker != null ? " with " + mConfig.getJobs() + " threads" : ""));
88+
Log.i(TAG, "Using Apktool " + mConfig.getVersion() + " on " + apkName
89+
+ (mWorker != null ? " with " + mConfig.getJobs() + " threads" : ""));
9190

9291
buildSources(outDir);
9392
buildResources(outDir);
@@ -129,8 +128,7 @@ private void buildSources(File outDir) throws AndrolibException {
129128
if (dirName.equals("smali")) {
130129
fileName = "classes.dex";
131130
} else if (dirName.startsWith("smali_")) {
132-
fileName = dirName.substring(dirName.indexOf('_') + 1)
133-
.replace('@', File.separatorChar) + ".dex";
131+
fileName = dirName.substring(dirName.indexOf('_') + 1).replace('@', File.separatorChar) + ".dex";
134132
} else {
135133
continue;
136134
}
@@ -149,11 +147,11 @@ private void copySourcesRaw(File outDir, String fileName) throws AndrolibExcepti
149147
File outFile = new File(outDir, fileName);
150148

151149
if (!mConfig.isForced() && !isFileNewer(inFile, outFile)) {
152-
LOGGER.info(fileName + " has not changed.");
150+
Log.i(TAG, fileName + " has not changed.");
153151
return;
154152
}
155153

156-
LOGGER.info("Copying raw " + fileName + "...");
154+
Log.i(TAG, "Copying raw " + fileName + "...");
157155
try {
158156
BrutIO.copyAndClose(Files.newInputStream(inFile.toPath()), Files.newOutputStream(outFile.toPath()));
159157
} catch (IOException ex) {
@@ -182,11 +180,11 @@ private void buildSourcesSmaliJob(File outDir, String dirName, String fileName)
182180
File dexFile = new File(outDir, fileName);
183181

184182
if (!mConfig.isForced() && !isFileNewer(smaliDir, dexFile)) {
185-
LOGGER.info(dirName + " has not changed.");
183+
Log.i(TAG, dirName + " has not changed.");
186184
return;
187185
}
188186

189-
LOGGER.info("Smaling " + dirName + " folder into " + fileName + "...");
187+
Log.i(TAG, "Smaling " + dirName + " folder into " + fileName + "...");
190188
mSmaliBuilder.build(smaliDir, dexFile);
191189
}
192190

@@ -233,13 +231,12 @@ private void buildResources(File outDir) throws AndrolibException {
233231
}
234232

235233
private void copyManifestRaw(File outDir, File manifest) throws AndrolibException {
236-
if (!mConfig.isForced()
237-
&& !isFileNewer(manifest, new File(outDir, "AndroidManifest.xml"))) {
238-
LOGGER.info("AndroidManifest.xml has not changed.");
234+
if (!mConfig.isForced() && !isFileNewer(manifest, new File(outDir, "AndroidManifest.xml"))) {
235+
Log.i(TAG, "AndroidManifest.xml has not changed.");
239236
return;
240237
}
241238

242-
LOGGER.info("Copying raw AndroidManifest.xml...");
239+
Log.i(TAG, "Copying raw AndroidManifest.xml...");
243240
try {
244241
Directory in = mApkDir.getDirectory();
245242

@@ -250,13 +247,12 @@ private void copyManifestRaw(File outDir, File manifest) throws AndrolibExceptio
250247
}
251248

252249
private void copyResourcesRaw(File outDir, File arscFile) throws AndrolibException {
253-
if (!mConfig.isForced()
254-
&& !isFileNewer(arscFile, new File(outDir, "resources.arsc"))) {
255-
LOGGER.info("resources.arsc has not changed.");
250+
if (!mConfig.isForced() && !isFileNewer(arscFile, new File(outDir, "resources.arsc"))) {
251+
Log.i(TAG, "resources.arsc has not changed.");
256252
return;
257253
}
258254

259-
LOGGER.info("Copying raw resources.arsc...");
255+
Log.i(TAG, "Copying raw resources.arsc...");
260256
try {
261257
Directory in = mApkDir.getDirectory();
262258

@@ -267,9 +263,8 @@ private void copyResourcesRaw(File outDir, File arscFile) throws AndrolibExcepti
267263
}
268264

269265
private void buildManifestOnly(File outDir, File manifest) throws AndrolibException {
270-
if (!mConfig.isForced()
271-
&& !isFileNewer(manifest, new File(outDir, "AndroidManifest.xml"))) {
272-
LOGGER.info("AndroidManifest.xml has not changed.");
266+
if (!mConfig.isForced() && !isFileNewer(manifest, new File(outDir, "AndroidManifest.xml"))) {
267+
Log.i(TAG, "AndroidManifest.xml has not changed.");
273268
return;
274269
}
275270

@@ -284,7 +279,7 @@ private void buildManifestOnly(File outDir, File manifest) throws AndrolibExcept
284279
ResXmlUtils.fixingPublicAttrsInProviderAttributes(manifest);
285280

286281
if (mConfig.isDebuggable()) {
287-
LOGGER.info("Setting 'debuggable' attribute to 'true' in AndroidManifest.xml...");
282+
Log.i(TAG, "Setting 'debuggable' attribute to 'true' in AndroidManifest.xml...");
288283
ResXmlUtils.setApplicationDebugTagTrue(manifest);
289284
}
290285

@@ -296,7 +291,7 @@ private void buildManifestOnly(File outDir, File manifest) throws AndrolibExcept
296291
throw new AndrolibException(ex);
297292
}
298293

299-
LOGGER.info("Building AndroidManifest.xml with " + AaptManager.getBinaryName() + "...");
294+
Log.i(TAG, "Building AndroidManifest.xml with " + AaptManager.getBinaryName() + "...");
300295
mAaptInvoker.invoke(tmpFile, manifest, null);
301296

302297
try (ZipRODirectory tmpDir = new ZipRODirectory(tmpFile)) {
@@ -316,10 +311,9 @@ private void buildManifestOnly(File outDir, File manifest) throws AndrolibExcept
316311
}
317312

318313
private void buildResourcesFully(File outDir, File manifest, File resDir) throws AndrolibException {
319-
if (!mConfig.isForced()
320-
&& !isFileNewer(manifest, new File(outDir, "AndroidManifest.xml"))
314+
if (!mConfig.isForced() && !isFileNewer(manifest, new File(outDir, "AndroidManifest.xml"))
321315
&& !isFileNewer(resDir, new File(outDir, "res"))) {
322-
LOGGER.info("AndroidManifest.xml and resources have not changed.");
316+
Log.i(TAG, "AndroidManifest.xml and resources have not changed.");
323317
return;
324318
}
325319

@@ -334,20 +328,20 @@ private void buildResourcesFully(File outDir, File manifest, File resDir) throws
334328
ResXmlUtils.fixingPublicAttrsInProviderAttributes(manifest);
335329

336330
if (mConfig.isDebuggable()) {
337-
LOGGER.info("Setting 'debuggable' attribute to 'true' in AndroidManifest.xml...");
331+
Log.i(TAG, "Setting 'debuggable' attribute to 'true' in AndroidManifest.xml...");
338332
ResXmlUtils.setApplicationDebugTagTrue(manifest);
339333
}
340334

341335
if (mConfig.isNetSecConf()) {
342-
LOGGER.info("Adding permissive network security config in manifest...");
336+
Log.i(TAG, "Adding permissive network security config in manifest...");
343337
File netSecConfOrig = new File(mApkDir, "res/xml/network_security_config.xml");
344338
OS.mkdir(netSecConfOrig.getParentFile());
345339
ResXmlUtils.modNetworkSecurityConfig(netSecConfOrig);
346340
ResXmlUtils.setNetworkSecurityConfig(manifest);
347341

348342
String targetSdkVersion = mApkInfo.getSdkInfo().getTargetSdkVersion();
349343
if (targetSdkVersion != null && SdkInfo.parseSdkInt(targetSdkVersion) < ResConfig.SDK_NOUGAT) {
350-
LOGGER.warning("Target SDK version is lower than 24, Network Security Configuration might be ignored!");
344+
Log.w(TAG, "Target SDK version is lower than 24, Network Security Configuration might be ignored!");
351345
}
352346
}
353347

@@ -359,7 +353,7 @@ private void buildResourcesFully(File outDir, File manifest, File resDir) throws
359353
throw new AndrolibException(ex);
360354
}
361355

362-
LOGGER.info("Building resources with " + AaptManager.getBinaryName() + "...");
356+
Log.i(TAG, "Building resources with " + AaptManager.getBinaryName() + "...");
363357
mAaptInvoker.invoke(tmpFile, manifest, resDir);
364358

365359
try (ZipRODirectory tmpDir = new ZipRODirectory(tmpFile)) {
@@ -388,7 +382,7 @@ private void copyOriginalFiles(File outDir) throws AndrolibException {
388382
return;
389383
}
390384

391-
LOGGER.info("Copying original files...");
385+
Log.i(TAG, "Copying original files...");
392386
try {
393387
FileDirectory in = new FileDirectory(originalDir);
394388

@@ -415,7 +409,7 @@ private void buildApkFile(File outDir, File outApk) throws AndrolibException {
415409
// Convert to set for fast lookup.
416410
Set<String> doNotCompress = new HashSet<>(mApkInfo.getDoNotCompress());
417411

418-
LOGGER.info("Building apk file...");
412+
Log.i(TAG, "Building apk file...");
419413
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(outApk.toPath()))) {
420414
// Zip aapt output files.
421415
ZipUtils.zipDir(outDir, out, doNotCompress);
@@ -424,21 +418,21 @@ private void buildApkFile(File outDir, File outApk) throws AndrolibException {
424418
for (String dirName : ApkInfo.RAW_DIRS) {
425419
File rawDir = new File(mApkDir, dirName);
426420
if (rawDir.isDirectory()) {
427-
LOGGER.info("Importing " + dirName + "...");
421+
Log.i(TAG, "Importing " + dirName + "...");
428422
ZipUtils.zipDir(mApkDir, dirName, out, doNotCompress);
429423
}
430424
}
431425

432426
// Zip unknown files.
433427
File unknownDir = new File(mApkDir, "unknown");
434428
if (unknownDir.isDirectory()) {
435-
LOGGER.info("Importing unknown files...");
429+
Log.i(TAG, "Importing unknown files...");
436430
ZipUtils.zipDir(unknownDir, out, doNotCompress);
437431
}
438432
} catch (IOException ex) {
439433
throw new AndrolibException(ex);
440434
}
441-
LOGGER.info("Built apk into: " + outApk.getPath());
435+
Log.i(TAG, "Built apk into: " + outApk.getPath());
442436
}
443437

444438
private boolean isFileNewer(File file, File reference) {

0 commit comments

Comments
 (0)