Skip to content

Commit 9d7d580

Browse files
authored
ApkDecoder use apkFile and unknownFiles from ApkInfo (#3242)
1 parent e52c3b0 commit 9d7d580

File tree

6 files changed

+33
-71
lines changed

6 files changed

+33
-71
lines changed

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

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import brut.androlib.exceptions.OutDirExistsException;
2222
import brut.androlib.apk.ApkInfo;
2323
import brut.androlib.res.ResourcesDecoder;
24-
import brut.androlib.res.data.*;
25-
import brut.androlib.res.xml.ResXmlPatcher;
2624
import brut.androlib.src.SmaliDecoder;
2725
import brut.directory.Directory;
2826
import brut.directory.ExtFile;
@@ -41,9 +39,7 @@ public class ApkDecoder {
4139
private final static Logger LOGGER = Logger.getLogger(ApkDecoder.class.getName());
4240

4341
private final Config mConfig;
44-
private final ExtFile mApkFile;
45-
protected final ResUnknownFiles mResUnknownFiles;
46-
private ApkInfo mApkInfo;
42+
private final ApkInfo mApkInfo;
4743
private int mMinSdkVersion = 0;
4844

4945
private final static String SMALI_DIRNAME = "smali";
@@ -73,17 +69,17 @@ public ApkDecoder(Config config, File apkFile) {
7369

7470
public ApkDecoder(Config config, ExtFile apkFile) {
7571
mConfig = config;
76-
mApkFile = apkFile;
77-
mResUnknownFiles = new ResUnknownFiles();
72+
mApkInfo = new ApkInfo(apkFile);
7873
}
7974

8075
public ApkInfo decode(File outDir) throws AndrolibException, IOException, DirectoryException {
76+
ExtFile apkFile = mApkInfo.getApkFile();
8177
try {
8278
if (!mConfig.forceDelete && outDir.exists()) {
8379
throw new OutDirExistsException();
8480
}
8581

86-
if (!mApkFile.isFile() || !mApkFile.canRead()) {
82+
if (!apkFile.isFile() || !apkFile.canRead()) {
8783
throw new InFileNotFoundException();
8884
}
8985

@@ -94,11 +90,9 @@ public ApkInfo decode(File outDir) throws AndrolibException, IOException, Direct
9490
}
9591
outDir.mkdirs();
9692

97-
LOGGER.info("Using Apktool " + ApktoolProperties.getVersion() + " on " + mApkFile.getName());
93+
LOGGER.info("Using Apktool " + ApktoolProperties.getVersion() + " on " + mApkInfo.apkFileName);
9894

99-
mApkInfo = new ApkInfo(mApkFile);
100-
101-
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(mConfig, mApkFile, mApkInfo);
95+
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(mConfig, mApkInfo);
10296

10397
if (mApkInfo.hasResources()) {
10498
switch (mConfig.decodeResources) {
@@ -136,7 +130,7 @@ public ApkInfo decode(File outDir) throws AndrolibException, IOException, Direct
136130

137131
if (mApkInfo.hasMultipleSources()) {
138132
// foreach unknown dex file in root, lets disassemble it
139-
Set<String> files = mApkFile.getDirectory().getFiles(true);
133+
Set<String> files = apkFile.getDirectory().getFiles(true);
140134
for (String file : files) {
141135
if (file.endsWith(".dex")) {
142136
if (!file.equalsIgnoreCase("classes.dex")) {
@@ -174,7 +168,7 @@ public ApkInfo decode(File outDir) throws AndrolibException, IOException, Direct
174168
return mApkInfo;
175169
} finally {
176170
try {
177-
mApkFile.close();
171+
apkFile.close();
178172
} catch (IOException ignored) {}
179173
}
180174
}
@@ -186,7 +180,7 @@ private void writeApkInfo(File outDir) throws AndrolibException {
186180
private void copyManifestRaw(File outDir) throws AndrolibException {
187181
try {
188182
LOGGER.info("Copying raw manifest...");
189-
mApkFile.getDirectory().copyToDir(outDir, APK_MANIFEST_FILENAMES);
183+
mApkInfo.getApkFile().getDirectory().copyToDir(outDir, APK_MANIFEST_FILENAMES);
190184
} catch (DirectoryException ex) {
191185
throw new AndrolibException(ex);
192186
}
@@ -195,7 +189,7 @@ private void copyManifestRaw(File outDir) throws AndrolibException {
195189
private void copyResourcesRaw(File outDir) throws AndrolibException {
196190
try {
197191
LOGGER.info("Copying raw resources...");
198-
mApkFile.getDirectory().copyToDir(outDir, APK_RESOURCES_FILENAMES);
192+
mApkInfo.getApkFile().getDirectory().copyToDir(outDir, APK_RESOURCES_FILENAMES);
199193
} catch (DirectoryException ex) {
200194
throw new AndrolibException(ex);
201195
}
@@ -204,7 +198,7 @@ private void copyResourcesRaw(File outDir) throws AndrolibException {
204198
private void copySourcesRaw(File outDir, String filename) throws AndrolibException {
205199
try {
206200
LOGGER.info("Copying raw " + filename + " file...");
207-
mApkFile.getDirectory().copyToDir(outDir, filename);
201+
mApkInfo.getApkFile().getDirectory().copyToDir(outDir, filename);
208202
} catch (DirectoryException ex) {
209203
throw new AndrolibException(ex);
210204
}
@@ -222,7 +216,7 @@ private void decodeSourcesSmali(File outDir, String filename) throws AndrolibExc
222216
//noinspection ResultOfMethodCallIgnored
223217
smaliDir.mkdirs();
224218
LOGGER.info("Baksmaling " + filename + "...");
225-
DexFile dexFile = SmaliDecoder.decode(mApkFile, smaliDir, filename,
219+
DexFile dexFile = SmaliDecoder.decode(mApkInfo.getApkFile(), smaliDir, filename,
226220
mConfig.baksmaliDebugMode, mConfig.apiLevel);
227221
int minSdkVersion = dexFile.getOpcodes().api;
228222
if (mMinSdkVersion == 0 || mMinSdkVersion > minSdkVersion) {
@@ -236,7 +230,7 @@ private void decodeSourcesSmali(File outDir, String filename) throws AndrolibExc
236230
private void copyRawFiles(File outDir) throws AndrolibException {
237231
LOGGER.info("Copying assets and libs...");
238232
try {
239-
Directory in = mApkFile.getDirectory();
233+
Directory in = mApkInfo.getApkFile().getDirectory();
240234

241235
if (mConfig.decodeAssets == Config.DECODE_ASSETS_FULL) {
242236
if (in.containsDir("assets")) {
@@ -270,7 +264,7 @@ private void copyUnknownFiles(File outDir) throws AndrolibException {
270264
LOGGER.info("Copying unknown files...");
271265
File unknownOut = new File(outDir, UNK_DIRNAME);
272266
try {
273-
Directory unk = mApkFile.getDirectory();
267+
Directory unk = mApkInfo.getApkFile().getDirectory();
274268

275269
// loop all items in container recursively, ignoring any that are pre-defined by aapt
276270
Set<String> files = unk.getFiles(true);
@@ -281,11 +275,9 @@ private void copyUnknownFiles(File outDir) throws AndrolibException {
281275
unk.copyToDir(unknownOut, file);
282276
// let's record the name of the file, and its compression type
283277
// so that we may re-include it the same way
284-
mResUnknownFiles.addUnknownFileInfo(file, String.valueOf(unk.getCompressionLevel(file)));
278+
mApkInfo.addUnknownFileInfo(file, String.valueOf(unk.getCompressionLevel(file)));
285279
}
286280
}
287-
// update apk info
288-
mApkInfo.unknownFiles = mResUnknownFiles.getUnknownFiles();
289281
} catch (DirectoryException ex) {
290282
throw new AndrolibException(ex);
291283
}
@@ -300,7 +292,7 @@ private void copyOriginalFiles(File outDir) throws AndrolibException {
300292
}
301293

302294
try {
303-
Directory in = mApkFile.getDirectory();
295+
Directory in = mApkInfo.getApkFile().getDirectory();
304296
if (in.containsFile("AndroidManifest.xml")) {
305297
in.copyToDir(originalDir, "AndroidManifest.xml");
306298
}
@@ -326,7 +318,7 @@ private void copyOriginalFiles(File outDir) throws AndrolibException {
326318
private void recordUncompressedFiles(Map<String, String> resFileMapping) throws AndrolibException {
327319
try {
328320
List<String> uncompressedFilesOrExts = new ArrayList<>();
329-
Directory unk = mApkFile.getDirectory();
321+
Directory unk = mApkInfo.getApkFile().getDirectory();
330322
Set<String> files = unk.getFiles(true);
331323

332324
for (String file : files) {

brut.apktool/apktool-lib/src/main/java/brut/androlib/apk/ApkInfo.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class ApkInfo implements YamlSerializable {
3939
public boolean resourcesAreCompressed;
4040
public boolean sharedLibrary;
4141
public boolean sparseResources;
42-
public Map<String, String> unknownFiles;
42+
public Map<String, String> unknownFiles = new LinkedHashMap<>();
4343
public List<String> doNotCompress;
4444

4545
/** @deprecated use {@link #resourcesAreCompressed} */
@@ -120,6 +120,10 @@ public boolean hasMultipleSources() throws AndrolibException {
120120
}
121121
}
122122

123+
public void addUnknownFileInfo(String file, String value) {
124+
unknownFiles.put(file, value);
125+
}
126+
123127
public String checkTargetSdkVersionBounds() {
124128
int target = mapSdkShorthandToVersion(getTargetSdkVersion());
125129

@@ -295,7 +299,9 @@ public void write(YamlWriter writer) {
295299
writer.writeBool("resourcesAreCompressed", resourcesAreCompressed);
296300
writer.writeBool("sharedLibrary", sharedLibrary);
297301
writer.writeBool("sparseResources", sparseResources);
298-
writer.writeStringMap("unknownFiles", unknownFiles);
302+
if (unknownFiles.size() > 0) {
303+
writer.writeStringMap("unknownFiles", unknownFiles);
304+
}
299305
writer.writeList("doNotCompress", doNotCompress);
300306
}
301307
}

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/ResourcesDecoder.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import brut.androlib.res.xml.ResXmlPatcher;
2828
import brut.directory.Directory;
2929
import brut.directory.DirectoryException;
30-
import brut.directory.ExtFile;
3130
import brut.directory.FileDirectory;
3231
import org.xmlpull.v1.XmlSerializer;
3332

@@ -39,7 +38,6 @@ public class ResourcesDecoder {
3938
private final static Logger LOGGER = Logger.getLogger(ResourcesDecoder.class.getName());
4039

4140
private final Config mConfig;
42-
private final ExtFile mApkFile;
4341
private final ApkInfo mApkInfo;
4442
private final ResTable mResTable;
4543
private final Map<String, String> mResFileMapping = new HashMap<>();
@@ -48,9 +46,8 @@ public class ResourcesDecoder {
4846
"android", "com.htc", "com.lge", "com.lge.internal", "yi", "flyme", "air.com.adobe.appentry",
4947
"FFFFFFFFFFFFFFFFFFFFFF" };
5048

51-
public ResourcesDecoder(Config config, ExtFile apkFile, ApkInfo apkInfo) {
49+
public ResourcesDecoder(Config config, ApkInfo apkInfo) {
5250
mConfig = config;
53-
mApkFile = apkFile;
5451
mApkInfo = apkInfo;
5552
mResTable = new ResTable(mConfig, mApkInfo);
5653
}
@@ -68,7 +65,7 @@ public Map<String, String> getResFileMapping() {
6865
}
6966

7067
public void loadMainPkg() throws AndrolibException {
71-
mResTable.loadMainPkg(mApkFile);
68+
mResTable.loadMainPkg(mApkInfo.getApkFile());
7269
}
7370

7471
public void decodeManifest(File outDir) throws AndrolibException {
@@ -81,7 +78,7 @@ public void decodeManifest(File outDir) throws AndrolibException {
8178

8279
Directory inApk, out;
8380
try {
84-
inApk = mApkFile.getDirectory();
81+
inApk = mApkInfo.getApkFile().getDirectory();
8582
out = new FileDirectory(outDir);
8683

8784
if (mApkInfo.hasResources()) {
@@ -147,7 +144,7 @@ public void decodeResources(File outDir) throws AndrolibException {
147144
return;
148145
}
149146

150-
mResTable.loadMainPkg(mApkFile);
147+
mResTable.loadMainPkg(mApkInfo.getApkFile());
151148

152149
ResStreamDecoderContainer decoders = new ResStreamDecoderContainer();
153150
decoders.setDecoder("raw", new ResRawStreamDecoder());
@@ -161,7 +158,7 @@ public void decodeResources(File outDir) throws AndrolibException {
161158

162159
try {
163160
out = new FileDirectory(outDir);
164-
in = mApkFile.getDirectory();
161+
in = mApkInfo.getApkFile().getDirectory();
165162
out = out.createDir("res");
166163
} catch (DirectoryException ex) {
167164
throw new AndrolibException(ex);

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/data/ResUnknownFiles.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/NonStandardPkgIdTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static void beforeClass() throws Exception {
5454

5555
LOGGER.info("Decoding pkgid8.apk...");
5656
ApkInfo testInfo = new ApkInfo(testApk);
57-
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(Config.getDefaultConfig(), testApk, testInfo);
57+
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(Config.getDefaultConfig(), testInfo);
5858

5959
sTestNewDir.mkdirs();
6060
resourcesDecoder.decodeResources(sTestNewDir);

brut.apktool/apktool-lib/src/test/java/brut/androlib/decode/DecodeArrayTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void decodeStringArray() throws BrutException {
5555
ExtFile apkFile = new ExtFile(sTmpDir, "issue1994.apk");
5656
ApkInfo apkInfo = new ApkInfo(apkFile);
5757
//ApkDecoder apkDecoder = new ApkDecoder(apkFile);
58-
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(Config.getDefaultConfig(), apkFile, apkInfo);
58+
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(Config.getDefaultConfig(), apkInfo);
5959

6060
resourcesDecoder.loadMainPkg();
6161
ResTable resTable = resourcesDecoder.getResTable();
@@ -68,7 +68,7 @@ public void decodeStringArray() throws BrutException {
6868
public void decodeArray() throws BrutException {
6969
ExtFile apkFile = new ExtFile(sTmpDir, "issue1994.apk");
7070
ApkInfo apkInfo = new ApkInfo(apkFile);
71-
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(Config.getDefaultConfig(), apkFile, apkInfo);
71+
ResourcesDecoder resourcesDecoder = new ResourcesDecoder(Config.getDefaultConfig(), apkInfo);
7272

7373
resourcesDecoder.loadMainPkg();
7474
ResTable resTable = resourcesDecoder.getResTable();

0 commit comments

Comments
 (0)