19
19
import brut .androlib .exceptions .AndrolibException ;
20
20
import brut .androlib .exceptions .InFileNotFoundException ;
21
21
import brut .androlib .exceptions .OutDirExistsException ;
22
- import brut .androlib .exceptions .UndefinedResObjectException ;
23
22
import brut .androlib .meta .MetaInfo ;
24
- import brut .androlib .meta .PackageInfo ;
25
- import brut .androlib .meta .UsesFramework ;
26
- import brut .androlib .meta .VersionInfo ;
27
23
import brut .androlib .res .AndrolibResources ;
28
- import brut .androlib .res .data .ResPackage ;
29
24
import brut .androlib .res .data .ResTable ;
30
25
import brut .androlib .res .data .ResUnknownFiles ;
31
26
import brut .androlib .src .SmaliDecoder ;
32
27
import brut .directory .Directory ;
33
28
import brut .directory .ExtFile ;
34
- import brut .androlib .res .xml .ResXmlPatcher ;
35
29
import brut .common .BrutException ;
36
30
import brut .directory .DirectoryException ;
37
31
import brut .util .OS ;
38
32
import com .android .tools .smali .dexlib2 .iface .DexFile ;
39
- import com .google .common .base .Strings ;
40
33
import org .apache .commons .io .FilenameUtils ;
41
34
42
35
import java .io .File ;
@@ -111,7 +104,7 @@ public void decode(File outDir) throws AndrolibException, IOException, Directory
111
104
if (hasResources ()) {
112
105
switch (config .decodeResources ) {
113
106
case Config .DECODE_RESOURCES_NONE :
114
- decodeResourcesRaw (mApkFile , outDir );
107
+ copyResourcesRaw (mApkFile , outDir );
115
108
if (config .forceDecodeManifest == Config .FORCE_DECODE_MANIFEST_FULL ) {
116
109
// done after raw decoding of resources because copyToDir overwrites dest files
117
110
if (hasManifest ()) {
@@ -135,15 +128,15 @@ public void decode(File outDir) throws AndrolibException, IOException, Directory
135
128
decodeManifestFull (mApkFile , outDir , getResTable ());
136
129
}
137
130
else {
138
- decodeManifestRaw (mApkFile , outDir );
131
+ copyManifestRaw (mApkFile , outDir );
139
132
}
140
133
}
141
134
}
142
135
143
136
if (hasSources ()) {
144
137
switch (config .decodeSources ) {
145
138
case Config .DECODE_SOURCES_NONE :
146
- decodeSourcesRaw (mApkFile , outDir , "classes.dex" );
139
+ copySourcesRaw (mApkFile , outDir , "classes.dex" );
147
140
break ;
148
141
case Config .DECODE_SOURCES_SMALI :
149
142
case Config .DECODE_SOURCES_SMALI_ONLY_MAIN_CLASSES :
@@ -160,7 +153,7 @@ public void decode(File outDir) throws AndrolibException, IOException, Directory
160
153
if (! file .equalsIgnoreCase ("classes.dex" )) {
161
154
switch (config .decodeSources ) {
162
155
case Config .DECODE_SOURCES_NONE :
163
- decodeSourcesRaw (mApkFile , outDir , file );
156
+ copySourcesRaw (mApkFile , outDir , file );
164
157
break ;
165
158
case Config .DECODE_SOURCES_SMALI :
166
159
decodeSourcesSmali (mApkFile , outDir , file );
@@ -169,7 +162,7 @@ public void decode(File outDir) throws AndrolibException, IOException, Directory
169
162
if (file .startsWith ("classes" ) && file .endsWith (".dex" )) {
170
163
decodeSourcesSmali (mApkFile , outDir , file );
171
164
} else {
172
- decodeSourcesRaw (mApkFile , outDir , file );
165
+ copySourcesRaw (mApkFile , outDir , file );
173
166
}
174
167
break ;
175
168
}
@@ -178,11 +171,11 @@ public void decode(File outDir) throws AndrolibException, IOException, Directory
178
171
}
179
172
}
180
173
181
- decodeRawFiles (mApkFile , outDir );
182
- decodeUnknownFiles (mApkFile , outDir );
174
+ copyRawFiles (mApkFile , outDir );
175
+ copyUnknownFiles (mApkFile , outDir );
183
176
mUncompressedFiles = new ArrayList <>();
184
177
recordUncompressedFiles (mApkFile , mUncompressedFiles );
185
- writeOriginalFiles (mApkFile , outDir );
178
+ copyOriginalFiles (mApkFile , outDir );
186
179
writeMetaFile (outDir );
187
180
} finally {
188
181
try {
@@ -250,25 +243,25 @@ public void close() throws IOException {
250
243
mAndRes .close ();
251
244
}
252
245
253
- public void writeMetaFile (File outDir )
254
- throws AndrolibException {
246
+ private void writeMetaFile (File outDir ) throws AndrolibException {
255
247
MetaInfo meta = new MetaInfo ();
256
248
meta .version = ApktoolProperties .getVersion ();
257
249
meta .apkFileName = mApkFile .getName ();
258
250
259
251
if (mResTable != null ) {
260
- meta .isFrameworkApk = mResTable .isFrameworkApk ();
261
- putUsesFramework (meta );
262
- putSdkInfo (outDir , meta );
263
- putPackageInfo (meta );
264
- putVersionInfo (outDir , meta );
265
- putSharedLibraryInfo (meta );
266
- putSparseResourcesInfo (meta );
252
+ mResTable .initMetaInfo (meta , outDir );
253
+ if (config .frameworkTag != null ) {
254
+ meta .usesFramework .tag = config .frameworkTag ;
255
+ }
267
256
} else {
268
- putMinSdkInfo (meta );
257
+ if (mMinSdkVersion > 0 ) {
258
+ meta .sdkInfo = getMinSdkInfo ();
259
+ }
260
+ }
261
+ meta .unknownFiles = mResUnknownFiles .getUnknownFiles ();
262
+ if (mUncompressedFiles != null && !mUncompressedFiles .isEmpty ()) {
263
+ meta .doNotCompress = mUncompressedFiles ;
269
264
}
270
- putUnknownInfo (meta );
271
- putFileCompressionInfo (meta );
272
265
273
266
try {
274
267
meta .save (new File (outDir , "apktool.yml" ));
@@ -277,111 +270,13 @@ public void writeMetaFile(File outDir)
277
270
}
278
271
}
279
272
280
- private void putUsesFramework (MetaInfo meta ) {
281
- Set <ResPackage > pkgs = mResTable .listFramePackages ();
282
- if (pkgs .isEmpty ()) {
283
- return ;
284
- }
285
-
286
- Integer [] ids = new Integer [pkgs .size ()];
287
- int i = 0 ;
288
- for (ResPackage pkg : pkgs ) {
289
- ids [i ++] = pkg .getId ();
290
- }
291
- Arrays .sort (ids );
292
-
293
- meta .usesFramework = new UsesFramework ();
294
- meta .usesFramework .ids = Arrays .asList (ids );
295
-
296
- if (config .frameworkTag != null ) {
297
- meta .usesFramework .tag = config .frameworkTag ;
298
- }
299
- }
300
-
301
- private void putSdkInfo (File outDir , MetaInfo meta ) {
302
- Map <String , String > info = mResTable .getSdkInfo ();
303
- if (info .size () > 0 ) {
304
- String refValue ;
305
- if (info .get ("minSdkVersion" ) != null ) {
306
- refValue = ResXmlPatcher .pullValueFromIntegers (outDir , info .get ("minSdkVersion" ));
307
- if (refValue != null ) {
308
- info .put ("minSdkVersion" , refValue );
309
- }
310
- }
311
- if (info .get ("targetSdkVersion" ) != null ) {
312
- refValue = ResXmlPatcher .pullValueFromIntegers (outDir , info .get ("targetSdkVersion" ));
313
- if (refValue != null ) {
314
- info .put ("targetSdkVersion" , refValue );
315
- }
316
- }
317
- if (info .get ("maxSdkVersion" ) != null ) {
318
- refValue = ResXmlPatcher .pullValueFromIntegers (outDir , info .get ("maxSdkVersion" ));
319
- if (refValue != null ) {
320
- info .put ("maxSdkVersion" , refValue );
321
- }
322
- }
323
- meta .sdkInfo = info ;
324
- }
325
- }
326
-
327
- private void putMinSdkInfo (MetaInfo meta ) {
328
- if (mMinSdkVersion > 0 ) {
329
- Map <String , String > sdkInfo = new LinkedHashMap <>();
330
- sdkInfo .put ("minSdkVersion" , Integer .toString (mMinSdkVersion ));
331
- meta .sdkInfo = sdkInfo ;
332
- }
333
- }
334
-
335
- private void putPackageInfo (MetaInfo meta ) throws AndrolibException {
336
- String renamed = mResTable .getPackageRenamed ();
337
- String original = mResTable .getPackageOriginal ();
338
-
339
- int id = mResTable .getPackageId ();
340
- try {
341
- id = mResTable .getPackage (renamed ).getId ();
342
- } catch (UndefinedResObjectException ignored ) {}
343
-
344
- if (Strings .isNullOrEmpty (original )) {
345
- return ;
346
- }
347
-
348
- meta .packageInfo = new PackageInfo ();
349
-
350
- // only put rename-manifest-package into apktool.yml, if the change will be required
351
- if (!renamed .equalsIgnoreCase (original )) {
352
- meta .packageInfo .renameManifestPackage = renamed ;
353
- }
354
- meta .packageInfo .forcedPackageId = String .valueOf (id );
355
- }
356
-
357
- private void putVersionInfo (File outDir , MetaInfo meta ) {
358
- VersionInfo info = mResTable .getVersionInfo ();
359
- String refValue = ResXmlPatcher .pullValueFromStrings (outDir , info .versionName );
360
- if (refValue != null ) {
361
- info .versionName = refValue ;
362
- }
363
- meta .versionInfo = info ;
364
- }
365
-
366
- private void putSharedLibraryInfo (MetaInfo meta ) {
367
- meta .sharedLibrary = mResTable .getSharedLibrary ();
368
- }
369
-
370
- private void putSparseResourcesInfo (MetaInfo meta ) {
371
- meta .sparseResources = mResTable .getSparseResources ();
372
- }
373
-
374
- private void putUnknownInfo (MetaInfo meta ) {
375
- meta .unknownFiles = mResUnknownFiles .getUnknownFiles ();
376
- }
377
-
378
- private void putFileCompressionInfo (MetaInfo meta ) {
379
- if (mUncompressedFiles != null && !mUncompressedFiles .isEmpty ()) {
380
- meta .doNotCompress = mUncompressedFiles ;
381
- }
273
+ private Map <String , String > getMinSdkInfo () {
274
+ Map <String , String > sdkInfo = new LinkedHashMap <>();
275
+ sdkInfo .put ("minSdkVersion" , Integer .toString (mMinSdkVersion ));
276
+ return sdkInfo ;
382
277
}
383
278
384
- public void decodeSourcesRaw (ExtFile apkFile , File outDir , String filename )
279
+ private void copySourcesRaw (ExtFile apkFile , File outDir , String filename )
385
280
throws AndrolibException {
386
281
try {
387
282
LOGGER .info ("Copying raw " + filename + " file..." );
@@ -391,7 +286,7 @@ public void decodeSourcesRaw(ExtFile apkFile, File outDir, String filename)
391
286
}
392
287
}
393
288
394
- public void decodeSourcesSmali (File apkFile , File outDir , String filename )
289
+ private void decodeSourcesSmali (File apkFile , File outDir , String filename )
395
290
throws AndrolibException {
396
291
try {
397
292
File smaliDir ;
@@ -415,7 +310,7 @@ public void decodeSourcesSmali(File apkFile, File outDir, String filename)
415
310
}
416
311
}
417
312
418
- public void decodeManifestRaw (ExtFile apkFile , File outDir )
313
+ private void copyManifestRaw (ExtFile apkFile , File outDir )
419
314
throws AndrolibException {
420
315
try {
421
316
LOGGER .info ("Copying raw manifest..." );
@@ -425,12 +320,12 @@ public void decodeManifestRaw(ExtFile apkFile, File outDir)
425
320
}
426
321
}
427
322
428
- public void decodeManifestFull (ExtFile apkFile , File outDir , ResTable resTable )
323
+ private void decodeManifestFull (ExtFile apkFile , File outDir , ResTable resTable )
429
324
throws AndrolibException {
430
325
mAndRes .decodeManifest (resTable , apkFile , outDir );
431
326
}
432
327
433
- public void decodeResourcesRaw (ExtFile apkFile , File outDir )
328
+ private void copyResourcesRaw (ExtFile apkFile , File outDir )
434
329
throws AndrolibException {
435
330
try {
436
331
LOGGER .info ("Copying raw resources..." );
@@ -440,17 +335,17 @@ public void decodeResourcesRaw(ExtFile apkFile, File outDir)
440
335
}
441
336
}
442
337
443
- public void decodeResourcesFull (ExtFile apkFile , File outDir , ResTable resTable )
338
+ private void decodeResourcesFull (ExtFile apkFile , File outDir , ResTable resTable )
444
339
throws AndrolibException {
445
340
mAndRes .decode (resTable , apkFile , outDir );
446
341
}
447
342
448
- public void decodeManifestWithResources (ExtFile apkFile , File outDir , ResTable resTable )
343
+ private void decodeManifestWithResources (ExtFile apkFile , File outDir , ResTable resTable )
449
344
throws AndrolibException {
450
345
mAndRes .decodeManifestWithResources (resTable , apkFile , outDir );
451
346
}
452
347
453
- public void decodeRawFiles (ExtFile apkFile , File outDir )
348
+ private void copyRawFiles (ExtFile apkFile , File outDir )
454
349
throws AndrolibException {
455
350
LOGGER .info ("Copying assets and libs..." );
456
351
try {
@@ -484,7 +379,7 @@ private boolean isAPKFileNames(String file) {
484
379
return false ;
485
380
}
486
381
487
- public void decodeUnknownFiles (ExtFile apkFile , File outDir )
382
+ private void copyUnknownFiles (ExtFile apkFile , File outDir )
488
383
throws AndrolibException {
489
384
LOGGER .info ("Copying unknown files..." );
490
385
File unknownOut = new File (outDir , UNK_DIRNAME );
@@ -508,7 +403,7 @@ public void decodeUnknownFiles(ExtFile apkFile, File outDir)
508
403
}
509
404
}
510
405
511
- public void writeOriginalFiles (ExtFile apkFile , File outDir )
406
+ private void copyOriginalFiles (ExtFile apkFile , File outDir )
512
407
throws AndrolibException {
513
408
LOGGER .info ("Copying original files..." );
514
409
File originalDir = new File (outDir , "original" );
@@ -531,7 +426,7 @@ public void writeOriginalFiles(ExtFile apkFile, File outDir)
531
426
if (in .containsDir ("META-INF/services" )) {
532
427
// If the original APK contains the folder META-INF/services folder
533
428
// that is used for service locators (like coroutines on android),
534
- // copy it to the destination folder so it does not get dropped.
429
+ // copy it to the destination folder, so it does not get dropped.
535
430
LOGGER .info ("Copying META-INF/services directory" );
536
431
in .copyToDir (outDir , "META-INF/services" );
537
432
}
@@ -541,7 +436,7 @@ public void writeOriginalFiles(ExtFile apkFile, File outDir)
541
436
}
542
437
}
543
438
544
- public void recordUncompressedFiles (ExtFile apkFile , Collection <String > uncompressedFilesOrExts ) throws AndrolibException {
439
+ private void recordUncompressedFiles (ExtFile apkFile , Collection <String > uncompressedFilesOrExts ) throws AndrolibException {
545
440
try {
546
441
Directory unk = apkFile .getDirectory ();
547
442
Set <String > files = unk .getFiles (true );
0 commit comments