Skip to content

Commit f6d568b

Browse files
authored
refactor: Config/Tests/misc rework (#3746)
* refactor: Config/Tests/misc rework Config is now propagated to ResValue instead of being a globally accessible instance that gets set each time a new Config is instantiated. The old way violted every principle of an instance and even a singleton. This caused working with Tests extremely error-prone, because the Config had to be carefully reset to default for each Test to get the expected behavior, and ResValue was only aware of the global instance. Now, only Main and BaseTest are calling new Config(). BaseTest provides a fresh Config for every Test which turns the boilerplate into something shorter, predictable and more robust. Config class is now encapsulated to give control over the values passed to it. Having both publically-modifyable fields and setters with the verfications was more of a dirty hack than proper coding principles. General-purpose XML utilities extracted from ResXmlUtils into XmlUtils in the brut.xml library. It contains Document creation, parsing and saving. In addition, it has a generic evaluateXPath method that's now used more widely wherever manual looping over nodes was used, like in some tests. ResXmlUtils reworked and cleaned, now takes advantage of XmlUtils. ResXmlUtils only retains methods relevant specifically to resource XMLs and Android Manifest. Minor tweaks and code cleanup in various classed like ApktoolProperties and the main work classes (Framework, ApkDecoder, ApkBuilder, etc.), as well as utility classes like AaptInvoker and OS. Fixed locks being held by certain ExtFiles to zips with loaded Directory, which prevented proper temp file cleanup during Test stage. ExternalEntityTest resources ("doctype") moved to aapt1 as it's an aapt1-only test. Overall, mainly a code refactor with no end-user changes, to make development less prone to errors. * fix framework parent check * make sure frameworkDirectory is never null * make sure frameworkDirectory is never null * style * retrieve Config only in ResBagValue and misc tweaks We actually don't need to pass Config explicitly to all ResValue subtypes. It's only needed by 3 subtypes of ResBagValue, so we get the Config via ResBagValue's mParent.getPackage().getConfig(). Encapsulate ResConfigFlags. Encapsulate ResID and make it a numeric and comparable type. Sort resource specs by resource ID in generatePublicXml. Duo is redundant, replace with Apache Common's Pair. ResIdValue is redundant, it's never actually instantiated. Tweak equals and hashCode overrides to standard formats. Misc style and variable name tweaks. * redundant explicit toString calls * Objects.hash is safer
1 parent 70a99d2 commit f6d568b

File tree

115 files changed

+2664
-3233
lines changed

Some content is hidden

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

115 files changed

+2664
-3233
lines changed

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

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
import brut.androlib.exceptions.OutDirExistsException;
2424
import brut.androlib.res.Framework;
2525
import brut.common.BrutException;
26-
import brut.directory.DirectoryException;
2726
import brut.directory.ExtFile;
28-
import brut.util.AaptManager;
2927
import brut.util.OSDetection;
3028
import org.apache.commons.cli.*;
3129

@@ -73,8 +71,8 @@ public static void main(String[] args) throws BrutException {
7371
try {
7472
commandLine = parser.parse(allOptions, args, false);
7573

76-
if (! OSDetection.is64Bit()) {
77-
System.err.println("32 bit support is deprecated. Apktool will not support 32bit on v3.0.0.");
74+
if (!OSDetection.is64Bit()) {
75+
System.err.println("32-bit support is deprecated and will be removed in 3.0.0.");
7876
}
7977
} catch (ParseException ex) {
8078
System.err.println(ex.getMessage());
@@ -96,7 +94,7 @@ public static void main(String[] args) throws BrutException {
9694
setAdvanceMode();
9795
}
9896

99-
Config config = Config.getDefaultConfig();
97+
Config config = new Config();
10098
initConfig(commandLine, config);
10199

102100
boolean cmdFound = false;
@@ -145,16 +143,16 @@ public static void main(String[] args) throws BrutException {
145143

146144
private static void initConfig(CommandLine cli, Config config) {
147145
if (cli.hasOption("p") || cli.hasOption("frame-path")) {
148-
config.frameworkDirectory = cli.getOptionValue("p");
146+
config.setFrameworkDirectory(cli.getOptionValue("p"));
149147
}
150148
if (cli.hasOption("t") || cli.hasOption("tag")) {
151-
config.frameworkTag = cli.getOptionValue("t");
149+
config.setFrameworkTag(cli.getOptionValue("t"));
152150
}
153151
if (cli.hasOption("api") || cli.hasOption("api-level")) {
154-
config.apiLevel = Integer.parseInt(cli.getOptionValue("api"));
152+
config.setApiLevel(Integer.parseInt(cli.getOptionValue("api")));
155153
}
156154
if (cli.hasOption("j") || cli.hasOption("jobs")) {
157-
config.jobs = Integer.parseInt(cli.getOptionValue("j"));
155+
config.setJobs(Integer.parseInt(cli.getOptionValue("j")));
158156
}
159157
}
160158

@@ -166,17 +164,21 @@ private static void cmdDecode(CommandLine cli, Config config) throws AndrolibExc
166164
config.setDecodeSources(Config.DECODE_SOURCES_NONE);
167165
}
168166
if (cli.hasOption("only-main-classes")) {
169-
config.setDecodeSources(Config.DECODE_SOURCES_SMALI_ONLY_MAIN_CLASSES);
167+
if (cli.hasOption("s") || cli.hasOption("no-src")) {
168+
System.err.println("--only-main-classes cannot be paired with -s/--no-src. Ignoring.");
169+
} else {
170+
config.setDecodeSources(Config.DECODE_SOURCES_SMALI_ONLY_MAIN_CLASSES);
171+
}
170172
}
171173
if (cli.hasOption("d") || cli.hasOption("debug")) {
172174
System.err.println("SmaliDebugging has been removed in 2.1.0 onward. Please see: https://github.com/iBotPeaches/Apktool/issues/1061");
173175
System.exit(1);
174176
}
175177
if (cli.hasOption("b") || cli.hasOption("no-debug-info")) {
176-
config.baksmaliDebugMode = false;
178+
config.setBaksmaliDebugMode(false);
177179
}
178180
if (cli.hasOption("f") || cli.hasOption("force")) {
179-
config.forceDelete = true;
181+
config.setForceDelete(true);
180182
}
181183
if (cli.hasOption("r") || cli.hasOption("no-res")) {
182184
config.setDecodeResources(Config.DECODE_RESOURCES_NONE);
@@ -188,10 +190,10 @@ private static void cmdDecode(CommandLine cli, Config config) throws AndrolibExc
188190
config.setDecodeAssets(Config.DECODE_ASSETS_NONE);
189191
}
190192
if (cli.hasOption("k") || cli.hasOption("keep-broken-res")) {
191-
config.keepBrokenResources = true;
193+
config.setKeepBrokenResources(true);
192194
}
193195
if (cli.hasOption("m") || cli.hasOption("match-original")) {
194-
config.analysisMode = true;
196+
config.setAnalysisMode(true);
195197
}
196198
if (cli.hasOption("resm") || cli.hasOption("res-mode") || cli.hasOption("resolve-resources-mode")) {
197199
String mode = cli.getOptionValue("resm");
@@ -266,16 +268,16 @@ private static void cmdBuild(CommandLine cli, Config config) throws AndrolibExce
266268

267269
// check for build options
268270
if (cli.hasOption("f") || cli.hasOption("force-all")) {
269-
config.forceBuildAll = true;
271+
config.setForceBuildAll(true);
270272
}
271273
if (cli.hasOption("d") || cli.hasOption("debug")) {
272-
config.debugMode = true;
274+
config.setDebugMode(true);
273275
}
274276
if (cli.hasOption("n") || cli.hasOption("net-sec-conf")) {
275-
config.netSecConf = true;
277+
config.setNetSecConf(true);
276278
}
277279
if (cli.hasOption("v") || cli.hasOption("verbose")) {
278-
config.verbose = true;
280+
config.setVerbose(true);
279281
}
280282
if (cli.hasOption("a") || cli.hasOption("aapt")) {
281283
if (cli.hasOption("use-aapt1") || cli.hasOption("use-aapt2")) {
@@ -284,8 +286,7 @@ private static void cmdBuild(CommandLine cli, Config config) throws AndrolibExce
284286
}
285287

286288
try {
287-
config.aaptBinary = new File(cli.getOptionValue("a"));
288-
config.aaptVersion = AaptManager.getAaptVersion(config.aaptBinary);
289+
config.setAaptBinary(new File(cli.getOptionValue("a")));
289290
} catch (BrutException ex) {
290291
System.err.println(ex.getMessage());
291292
System.exit(1);
@@ -296,42 +297,38 @@ private static void cmdBuild(CommandLine cli, Config config) throws AndrolibExce
296297
System.exit(1);
297298
}
298299

299-
config.aaptVersion = 1;
300+
config.setAaptVersion(1);
300301
}
301302
if (cli.hasOption("c") || cli.hasOption("copy-original")) {
302-
config.copyOriginalFiles = true;
303+
config.setCopyOriginalFiles(true);
303304
}
304305
if (cli.hasOption("nc") || cli.hasOption("no-crunch")) {
305-
config.noCrunch = true;
306+
config.setNoCrunch(true);
306307
}
307308
if (cli.hasOption("na") || cli.hasOption("no-apk")) {
308-
config.noApk = true;
309+
config.setNoApk(true);
309310
}
310311

311-
File outFile;
312-
if (cli.hasOption("o") || cli.hasOption("output")) {
313-
outFile = new File(cli.getOptionValue("o"));
314-
} else {
315-
outFile = null;
316-
}
317-
318-
if (config.netSecConf && config.aaptVersion == 1) {
312+
if (config.isNetSecConf() && config.getAaptVersion() == 1) {
319313
System.err.println("-n / --net-sec-conf is not supported with legacy aapt.");
320314
System.exit(1);
321315
}
322316

317+
File outFile = cli.hasOption("o") || cli.hasOption("output")
318+
? new File(cli.getOptionValue("o")) : null;
319+
323320
ExtFile apkDir = new ExtFile(apkDirName);
324321
ApkBuilder builder = new ApkBuilder(apkDir, config);
325322
builder.build(outFile);
326323
}
327324

328325
private static void cmdInstallFramework(CommandLine cli, Config config) throws AndrolibException {
329326
String apkName = getLastArg(cli);
330-
new Framework(config).installFramework(new File(apkName));
327+
new Framework(config).install(new File(apkName));
331328
}
332329

333330
private static void cmdListFrameworks(CommandLine cli, Config config) throws AndrolibException {
334-
new Framework(config).listFrameworkDirectory();
331+
new Framework(config).listDirectory();
335332
}
336333

337334
private static void cmdPublicizeResources(CommandLine cli, Config config) throws AndrolibException {
@@ -341,9 +338,9 @@ private static void cmdPublicizeResources(CommandLine cli, Config config) throws
341338

342339
private static void cmdEmptyFrameworkDirectory(CommandLine cli, Config config) throws AndrolibException {
343340
if (cli.hasOption("f") || cli.hasOption("force")) {
344-
config.forceDeleteFramework = true;
341+
config.setForceDeleteFramework(true);
345342
}
346-
new Framework(config).emptyFrameworkDirectory();
343+
new Framework(config).emptyDirectory();
347344
}
348345

349346
private static String getLastArg(CommandLine cli) {
@@ -638,11 +635,11 @@ private static void usage() {
638635

639636
// print out license info prior to formatter.
640637
System.out.println(
641-
"Apktool " + ApktoolProperties.getVersion() + " - a tool for reengineering Android apk files\n" +
642-
"with smali v" + ApktoolProperties.get("smaliVersion") +
643-
" and baksmali v" + ApktoolProperties.get("baksmaliVersion") + "\n" +
644-
"Copyright 2010 Ryszard Wiśniewski <brut.alll@gmail.com>\n" +
645-
"Copyright 2010 Connor Tumbleson <connor.tumbleson@gmail.com>" );
638+
"Apktool " + ApktoolProperties.getVersion() + " - a tool for reengineering Android apk files\n" +
639+
"with smali " + ApktoolProperties.getSmaliVersion() +
640+
" and baksmali " + ApktoolProperties.getBaksmaliVersion() + "\n" +
641+
"Copyright 2010 Ryszard Wiśniewski <brut.alll@gmail.com>\n" +
642+
"Copyright 2010 Connor Tumbleson <connor.tumbleson@gmail.com>");
646643
if (isAdvanceMode()) {
647644
System.out.println("Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0)\n");
648645
}else {

brut.apktool/apktool-lib/src/main/java/android/util/TypedValue.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@ public class TypedValue {
217217

218218
private static final float MANTISSA_MULT = 1.0f / (1 << TypedValue.COMPLEX_MANTISSA_SHIFT);
219219
private static final float[] RADIX_MULTS = {
220-
MANTISSA_MULT, 1.0f / (1 << 7) * MANTISSA_MULT,
221-
1.0f / (1 << 15) * MANTISSA_MULT, 1.0f / (1 << 23) * MANTISSA_MULT };
220+
MANTISSA_MULT, 1.0f / (1 << 7) * MANTISSA_MULT,
221+
1.0f / (1 << 15) * MANTISSA_MULT, 1.0f / (1 << 23) * MANTISSA_MULT
222+
};
222223

223224
/**
224225
* Retrieve the base value from a complex data integer. This uses the

0 commit comments

Comments
 (0)