| 1 |
/* |
| 2 |
* Copyright (C) 2010 Ryszard Wiśniewski <[email protected]> |
| 3 |
* Copyright (C) 2010 Connor Tumbleson <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* https://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package brut.androlib; |
| 18 |
|
| 19 |
import brut.androlib.meta.MetaInfo; |
| 20 |
import brut.androlib.meta.UsesFramework; |
| 21 |
import brut.androlib.options.BuildOptions; |
| 22 |
import brut.androlib.res.AndrolibResources; |
| 23 |
import brut.androlib.res.data.ResConfigFlags; |
| 24 |
import brut.androlib.res.data.ResPackage; |
| 25 |
import brut.androlib.res.data.ResTable; |
| 26 |
import brut.androlib.res.data.ResUnknownFiles; |
| 27 |
import brut.common.InvalidUnknownFileException; |
| 28 |
import brut.common.RootUnknownFileException; |
| 29 |
import brut.common.TraversalUnknownFileException; |
| 30 |
import brut.androlib.res.xml.ResXmlPatcher; |
| 31 |
import brut.androlib.src.SmaliBuilder; |
| 32 |
import brut.androlib.src.SmaliDecoder; |
| 33 |
import brut.common.BrutException; |
| 34 |
import brut.directory.*; |
| 35 |
import brut.util.*; |
| 36 |
import org.apache.commons.io.FileUtils; |
| 37 |
import org.apache.commons.io.FilenameUtils; |
| 38 |
import org.jf.dexlib2.iface.DexFile; |
| 39 |
import org.xml.sax.SAXException; |
| 40 |
|
| 41 |
import javax.xml.parsers.ParserConfigurationException; |
| 42 |
import javax.xml.transform.TransformerException; |
| 43 |
import java.io.*; |
| 44 |
import java.util.*; |
| 45 |
import java.util.logging.Logger; |
| 46 |
import java.util.regex.Pattern; |
| 47 |
import java.util.zip.CRC32; |
| 48 |
import java.util.zip.ZipEntry; |
| 49 |
import java.util.zip.ZipFile; |
| 50 |
import java.util.zip.ZipOutputStream; |
| 51 |
|
| 52 |
public class Androlib { |
| 53 |
private final AndrolibResources mAndRes = new AndrolibResources(); |
| 54 |
protected final ResUnknownFiles mResUnknownFiles = new ResUnknownFiles(); |
| 55 |
public final BuildOptions buildOptions; |
| 56 |
private int mMinSdkVersion = 0; |
| 57 |
|
| 58 |
public Androlib() { |
| 59 |
this(new BuildOptions()); |
| 60 |
} |
| 61 |
|
| 62 |
public Androlib(BuildOptions buildOptions) { |
| 63 |
this.buildOptions = buildOptions; |
| 64 |
mAndRes.buildOptions = buildOptions; |
| 65 |
} |
| 66 |
|
| 67 |
public ResTable getResTable(ExtFile apkFile) |
| 68 |
throws AndrolibException { |
| 69 |
return mAndRes.getResTable(apkFile, true); |
| 70 |
} |
| 71 |
|
| 72 |
public ResTable getResTable(ExtFile apkFile, boolean loadMainPkg) |
| 73 |
throws AndrolibException { |
| 74 |
return mAndRes.getResTable(apkFile, loadMainPkg); |
| 75 |
} |
| 76 |
|
| 77 |
public int getMinSdkVersion() { |
| 78 |
return mMinSdkVersion; |
| 79 |
} |
| 80 |
|
| 81 |
public void decodeSourcesRaw(ExtFile apkFile, File outDir, String filename) |
| 82 |
throws AndrolibException { |
| 83 |
try { |
| 84 |
LOGGER.info("Copying raw " + filename + " file..."); |
| 85 |
apkFile.getDirectory().copyToDir(outDir, filename); |
| 86 |
} catch (DirectoryException ex) { |
| 87 |
throw new AndrolibException(ex); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public void decodeSourcesSmali(File apkFile, File outDir, String filename, boolean bakDeb, int apiLevel) |
| 92 |
throws AndrolibException { |
| 93 |
try { |
| 94 |
File smaliDir; |
| 95 |
if (filename.equalsIgnoreCase("classes.dex")) { |
| 96 |
smaliDir = new File(outDir, SMALI_DIRNAME); |
| 97 |
} else { |
| 98 |
smaliDir = new File(outDir, SMALI_DIRNAME + "_" + filename.substring(0, filename.indexOf("."))); |
| 99 |
} |
| 100 |
OS.rmdir(smaliDir); |
| 101 |
smaliDir.mkdirs(); |
| 102 |
LOGGER.info("Baksmaling " + filename + "..."); |
| 103 |
DexFile dexFile = SmaliDecoder.decode(apkFile, smaliDir, filename, bakDeb, apiLevel); |
| 104 |
int minSdkVersion = dexFile.getOpcodes().api; |
| 105 |
if (mMinSdkVersion == 0 || mMinSdkVersion > minSdkVersion) { |
| 106 |
mMinSdkVersion = minSdkVersion; |
| 107 |
} |
| 108 |
} catch (BrutException ex) { |
| 109 |
throw new AndrolibException(ex); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public void decodeManifestRaw(ExtFile apkFile, File outDir) |
| 114 |
throws AndrolibException { |
| 115 |
try { |
| 116 |
LOGGER.info("Copying raw manifest..."); |
| 117 |
apkFile.getDirectory().copyToDir(outDir, APK_MANIFEST_FILENAMES); |
| 118 |
} catch (DirectoryException ex) { |
| 119 |
throw new AndrolibException(ex); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public void decodeManifestFull(ExtFile apkFile, File outDir, ResTable resTable) |
| 124 |
throws AndrolibException { |
| 125 |
mAndRes.decodeManifest(resTable, apkFile, outDir); |
| 126 |
} |
| 127 |
|
| 128 |
public void decodeResourcesRaw(ExtFile apkFile, File outDir) |
| 129 |
throws AndrolibException { |
| 130 |
try { |
| 131 |
LOGGER.info("Copying raw resources..."); |
| 132 |
apkFile.getDirectory().copyToDir(outDir, APK_RESOURCES_FILENAMES); |
| 133 |
} catch (DirectoryException ex) { |
| 134 |
throw new AndrolibException(ex); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public void decodeResourcesFull(ExtFile apkFile, File outDir, ResTable resTable) |
| 139 |
throws AndrolibException { |
| 140 |
mAndRes.decode(resTable, apkFile, outDir); |
| 141 |
} |
| 142 |
|
| 143 |
public void decodeManifestWithResources(ExtFile apkFile, File outDir, ResTable resTable) |
| 144 |
throws AndrolibException { |
| 145 |
mAndRes.decodeManifestWithResources(resTable, apkFile, outDir); |
| 146 |
} |
| 147 |
|
| 148 |
public void decodeRawFiles(ExtFile apkFile, File outDir, short decodeAssetMode) |
| 149 |
throws AndrolibException { |
| 150 |
LOGGER.info("Copying assets and libs..."); |
| 151 |
try { |
| 152 |
Directory in = apkFile.getDirectory(); |
| 153 |
|
| 154 |
if (decodeAssetMode == ApkDecoder.DECODE_ASSETS_FULL) { |
| 155 |
if (in.containsDir("assets")) { |
| 156 |
in.copyToDir(outDir, "assets"); |
| 157 |
} |
| 158 |
} |
| 159 |
if (in.containsDir("lib")) { |
| 160 |
in.copyToDir(outDir, "lib"); |
| 161 |
} |
| 162 |
if (in.containsDir("libs")) { |
| 163 |
in.copyToDir(outDir, "libs"); |
| 164 |
} |
| 165 |
if (in.containsDir("kotlin")) { |
| 166 |
in.copyToDir(outDir, "kotlin"); |
| 167 |
} |
| 168 |
} catch (DirectoryException ex) { |
| 169 |
throw new AndrolibException(ex); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
public void recordUncompressedFiles(ExtFile apkFile, Collection<String> uncompressedFilesOrExts) throws AndrolibException { |
| 174 |
try { |
| 175 |
Directory unk = apkFile.getDirectory(); |
| 176 |
Set<String> files = unk.getFiles(true); |
| 177 |
|
| 178 |
for (String file : files) { |
| 179 |
if (isAPKFileNames(file) && unk.getCompressionLevel(file) == 0) { |
| 180 |
String extOrFile = ""; |
| 181 |
if (unk.getSize(file) != 0) { |
| 182 |
extOrFile = FilenameUtils.getExtension(file); |
| 183 |
} |
| 184 |
|
| 185 |
if (extOrFile.isEmpty() || !NO_COMPRESS_PATTERN.matcher(extOrFile).find()) { |
| 186 |
extOrFile = file; |
| 187 |
if (mAndRes.mResFileMapping.containsKey(extOrFile)) { |
| 188 |
extOrFile = mAndRes.mResFileMapping.get(extOrFile); |
| 189 |
} |
| 190 |
} |
| 191 |
if (!uncompressedFilesOrExts.contains(extOrFile)) { |
| 192 |
uncompressedFilesOrExts.add(extOrFile); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} catch (DirectoryException ex) { |
| 197 |
throw new AndrolibException(ex); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
private boolean isAPKFileNames(String file) { |
| 202 |
for (String apkFile : APK_STANDARD_ALL_FILENAMES) { |
| 203 |
if (apkFile.equals(file) || file.startsWith(apkFile + "/")) { |
| 204 |
return true; |
| 205 |
} |
| 206 |
} |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
public void decodeUnknownFiles(ExtFile apkFile, File outDir) |
| 211 |
throws AndrolibException { |
| 212 |
LOGGER.info("Copying unknown files..."); |
| 213 |
File unknownOut = new File(outDir, UNK_DIRNAME); |
| 214 |
try { |
| 215 |
Directory unk = apkFile.getDirectory(); |
| 216 |
|
| 217 |
// loop all items in container recursively, ignoring any that are pre-defined by aapt |
| 218 |
Set<String> files = unk.getFiles(true); |
| 219 |
for (String file : files) { |
| 220 |
if (!isAPKFileNames(file) && !file.endsWith(".dex")) { |
| 221 |
|
| 222 |
// copy file out of archive into special "unknown" folder |
| 223 |
unk.copyToDir(unknownOut, file); |
| 224 |
// lets record the name of the file, and its compression type |
| 225 |
// so that we may re-include it the same way |
| 226 |
mResUnknownFiles.addUnknownFileInfo(file, String.valueOf(unk.getCompressionLevel(file))); |
| 227 |
} |
| 228 |
} |
| 229 |
} catch (DirectoryException ex) { |
| 230 |
throw new AndrolibException(ex); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
public void writeOriginalFiles(ExtFile apkFile, File outDir) |
| 235 |
throws AndrolibException { |
| 236 |
LOGGER.info("Copying original files..."); |
| 237 |
File originalDir = new File(outDir, "original"); |
| 238 |
if (!originalDir.exists()) { |
| 239 |
originalDir.mkdirs(); |
| 240 |
} |
| 241 |
|
| 242 |
try { |
| 243 |
Directory in = apkFile.getDirectory(); |
| 244 |
if (in.containsFile("AndroidManifest.xml")) { |
| 245 |
in.copyToDir(originalDir, "AndroidManifest.xml"); |
| 246 |
} |
| 247 |
if (in.containsFile("stamp-cert-sha256")) { |
| 248 |
in.copyToDir(originalDir, "stamp-cert-sha256"); |
| 249 |
} |
| 250 |
if (in.containsDir("META-INF")) { |
| 251 |
in.copyToDir(originalDir, "META-INF"); |
| 252 |
|
| 253 |
if (in.containsDir("META-INF/services")) { |
| 254 |
// If the original APK contains the folder META-INF/services folder |
| 255 |
// that is used for service locators (like coroutines on android), |
| 256 |
// copy it to the destination folder so it does not get dropped. |
| 257 |
LOGGER.info("Copying META-INF/services directory"); |
| 258 |
in.copyToDir(outDir, "META-INF/services"); |
| 259 |
} |
| 260 |
} |
| 261 |
} catch (DirectoryException ex) { |
| 262 |
throw new AndrolibException(ex); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
public void writeMetaFile(File mOutDir, MetaInfo meta) |
| 267 |
throws AndrolibException { |
| 268 |
try { |
| 269 |
meta.save(new File(mOutDir, "apktool.yml")); |
| 270 |
} catch (IOException ex) { |
| 271 |
throw new AndrolibException(ex); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
public MetaInfo readMetaFile(ExtFile appDir) |
| 276 |
throws AndrolibException { |
| 277 |
try( |
| 278 |
InputStream in = appDir.getDirectory().getFileInput("apktool.yml") |
| 279 |
) { |
| 280 |
return MetaInfo.load(in); |
| 281 |
} catch (DirectoryException | IOException ex) { |
| 282 |
throw new AndrolibException(ex); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
public void build(File appDir, File outFile) throws BrutException { |
| 287 |
build(new ExtFile(appDir), outFile); |
| 288 |
} |
| 289 |
|
| 290 |
public void build(ExtFile appDir, File outFile) |
| 291 |
throws BrutException { |
| 292 |
LOGGER.info("Using Apktool " + Androlib.getVersion()); |
| 293 |
|
| 294 |
MetaInfo meta = readMetaFile(appDir); |
| 295 |
buildOptions.isFramework = meta.isFrameworkApk; |
| 296 |
buildOptions.resourcesAreCompressed = meta.compressionType; |
| 297 |
buildOptions.doNotCompress = meta.doNotCompress; |
| 298 |
|
| 299 |
mAndRes.setSdkInfo(meta.sdkInfo); |
| 300 |
mAndRes.setPackageId(meta.packageInfo); |
| 301 |
mAndRes.setPackageRenamed(meta.packageInfo); |
| 302 |
mAndRes.setVersionInfo(meta.versionInfo); |
| 303 |
mAndRes.setSharedLibrary(meta.sharedLibrary); |
| 304 |
mAndRes.setSparseResources(meta.sparseResources); |
| 305 |
|
| 306 |
if (meta.sdkInfo != null && meta.sdkInfo.get("minSdkVersion") != null) { |
| 307 |
String minSdkVersion = meta.sdkInfo.get("minSdkVersion"); |
| 308 |
mMinSdkVersion = mAndRes.getMinSdkVersionFromAndroidCodename(meta, minSdkVersion); |
| 309 |
} |
| 310 |
|
| 311 |
if (outFile == null) { |
| 312 |
String outFileName = meta.apkFileName; |
| 313 |
outFile = new File(appDir, "dist" + File.separator + (outFileName == null ? "out.apk" : outFileName)); |
| 314 |
} |
| 315 |
|
| 316 |
new File(appDir, APK_DIRNAME).mkdirs(); |
| 317 |
File manifest = new File(appDir, "AndroidManifest.xml"); |
| 318 |
File manifestOriginal = new File(appDir, "AndroidManifest.xml.orig"); |
| 319 |
|
| 320 |
buildSources(appDir); |
| 321 |
buildNonDefaultSources(appDir); |
| 322 |
buildManifestFile(appDir, manifest, manifestOriginal); |
| 323 |
buildResources(appDir, meta.usesFramework); |
| 324 |
buildLibs(appDir); |
| 325 |
buildCopyOriginalFiles(appDir); |
| 326 |
buildApk(appDir, outFile); |
| 327 |
|
| 328 |
// we must go after the Apk is built, and copy the files in via Zip |
| 329 |
// this is because Aapt won't add files it doesn't know (ex unknown files) |
| 330 |
buildUnknownFiles(appDir, outFile, meta); |
| 331 |
|
| 332 |
// we copied the AndroidManifest.xml to AndroidManifest.xml.orig so we can edit it |
| 333 |
// lets restore the unedited one, to not change the original |
| 334 |
if (manifest.isFile() && manifest.exists() && manifestOriginal.isFile()) { |
| 335 |
try { |
| 336 |
if (new File(appDir, "AndroidManifest.xml").delete()) { |
| 337 |
FileUtils.moveFile(manifestOriginal, manifest); |
| 338 |
} |
| 339 |
} catch (IOException ex) { |
| 340 |
throw new AndrolibException(ex.getMessage()); |
| 341 |
} |
| 342 |
} |
| 343 |
LOGGER.info("Built apk into: " + outFile.getPath()); |
| 344 |
} |
| 345 |
|
| 346 |
private void buildManifestFile(File appDir, File manifest, File manifestOriginal) |
| 347 |
throws AndrolibException { |
| 348 |
|
| 349 |
// If we decoded in "raw", we cannot patch AndroidManifest |
| 350 |
if (new File(appDir, "resources.arsc").exists()) { |
| 351 |
return; |
| 352 |
} |
| 353 |
if (manifest.isFile() && manifest.exists()) { |
| 354 |
try { |
| 355 |
if (manifestOriginal.exists()) { |
| 356 |
manifestOriginal.delete(); |
| 357 |
} |
| 358 |
FileUtils.copyFile(manifest, manifestOriginal); |
| 359 |
ResXmlPatcher.fixingPublicAttrsInProviderAttributes(manifest); |
| 360 |
} catch (IOException ex) { |
| 361 |
throw new AndrolibException(ex.getMessage()); |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
public void buildSources(File appDir) |
| 367 |
throws AndrolibException { |
| 368 |
if (!buildSourcesRaw(appDir, "classes.dex") && !buildSourcesSmali(appDir, "smali", "classes.dex")) { |
| 369 |
LOGGER.warning("Could not find sources"); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
public void buildNonDefaultSources(ExtFile appDir) |
| 374 |
throws AndrolibException { |
| 375 |
try { |
| 376 |
// loop through any smali_ directories for multi-dex apks |
| 377 |
Map<String, Directory> dirs = appDir.getDirectory().getDirs(); |
| 378 |
for (Map.Entry<String, Directory> directory : dirs.entrySet()) { |
| 379 |
String name = directory.getKey(); |
| 380 |
if (name.startsWith("smali_")) { |
| 381 |
String filename = name.substring(name.indexOf("_") + 1) + ".dex"; |
| 382 |
|
| 383 |
if (!buildSourcesRaw(appDir, filename) && !buildSourcesSmali(appDir, name, filename)) { |
| 384 |
LOGGER.warning("Could not find sources"); |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
// loop through any classes#.dex files for multi-dex apks |
| 390 |
File[] dexFiles = appDir.listFiles(); |
| 391 |
if (dexFiles != null) { |
| 392 |
for (File dex : dexFiles) { |
| 393 |
|
| 394 |
// skip classes.dex because we have handled it in buildSources() |
| 395 |
if (dex.getName().endsWith(".dex") && ! dex.getName().equalsIgnoreCase("classes.dex")) { |
| 396 |
buildSourcesRaw(appDir, dex.getName()); |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
} catch(DirectoryException ex) { |
| 401 |
throw new AndrolibException(ex); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
public boolean buildSourcesRaw(File appDir, String filename) |
| 406 |
throws AndrolibException { |
| 407 |
File working = new File(appDir, filename); |
| 408 |
if (!working.exists()) { |
| 409 |
return false; |
| 410 |
} |
| 411 |
File stored = new File(appDir, APK_DIRNAME + "/" + filename); |
| 412 |
if (buildOptions.forceBuildAll || isModified(working, stored)) { |
| 413 |
LOGGER.info("Copying " + appDir.toString() + " " + filename + " file..."); |
| 414 |
try { |
| 415 |
BrutIO.copyAndClose(new FileInputStream(working), new FileOutputStream(stored)); |
| 416 |
return true; |
| 417 |
} catch (IOException ex) { |
| 418 |
throw new AndrolibException(ex); |
| 419 |
} |
| 420 |
} |
| 421 |
return true; |
| 422 |
} |
| 423 |
|
| 424 |
public boolean buildSourcesSmali(File appDir, String folder, String filename) |
| 425 |
throws AndrolibException { |
| 426 |
ExtFile smaliDir = new ExtFile(appDir, folder); |
| 427 |
if (!smaliDir.exists()) { |
| 428 |
return false; |
| 429 |
} |
| 430 |
File dex = new File(appDir, APK_DIRNAME + "/" + filename); |
| 431 |
if (! buildOptions.forceBuildAll) { |
| 432 |
LOGGER.info("Checking whether sources has changed..."); |
| 433 |
} |
| 434 |
if (buildOptions.forceBuildAll || isModified(smaliDir, dex)) { |
| 435 |
LOGGER.info("Smaling " + folder + " folder into " + filename + "..."); |
| 436 |
dex.delete(); |
| 437 |
SmaliBuilder.build(smaliDir, dex, buildOptions.forceApi > 0 ? buildOptions.forceApi : mMinSdkVersion); |
| 438 |
} |
| 439 |
return true; |
| 440 |
} |
| 441 |
|
| 442 |
public void buildResources(ExtFile appDir, UsesFramework usesFramework) |
| 443 |
throws BrutException { |
| 444 |
if (!buildResourcesRaw(appDir) && !buildResourcesFull(appDir, usesFramework) |
| 445 |
&& !buildManifest(appDir, usesFramework)) { |
| 446 |
LOGGER.warning("Could not find resources"); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
public boolean buildResourcesRaw(ExtFile appDir) |
| 451 |
throws AndrolibException { |
| 452 |
try { |
| 453 |
if (!new File(appDir, "resources.arsc").exists()) { |
| 454 |
return false; |
| 455 |
} |
| 456 |
File apkDir = new File(appDir, APK_DIRNAME); |
| 457 |
if (! buildOptions.forceBuildAll) { |
| 458 |
LOGGER.info("Checking whether resources has changed..."); |
| 459 |
} |
| 460 |
if (buildOptions.forceBuildAll || isModified(newFiles(APK_RESOURCES_FILENAMES, appDir), |
| 461 |
newFiles(APK_RESOURCES_FILENAMES, apkDir))) { |
| 462 |
LOGGER.info("Copying raw resources..."); |
| 463 |
appDir.getDirectory().copyToDir(apkDir, APK_RESOURCES_FILENAMES); |
| 464 |
} |
| 465 |
return true; |
| 466 |
} catch (DirectoryException ex) { |
| 467 |
throw new AndrolibException(ex); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
public boolean buildResourcesFull(File appDir, UsesFramework usesFramework) |
| 472 |
throws AndrolibException { |
| 473 |
try { |
| 474 |
if (!new File(appDir, "res").exists()) { |
| 475 |
return false; |
| 476 |
} |
| 477 |
if (! buildOptions.forceBuildAll) { |
| 478 |
LOGGER.info("Checking whether resources has changed..."); |
| 479 |
} |
| 480 |
File apkDir = new File(appDir, APK_DIRNAME); |
| 481 |
File resourceFile = new File(apkDir.getParent(), "resources.zip"); |
| 482 |
|
| 483 |
if (buildOptions.forceBuildAll || isModified(newFiles(APP_RESOURCES_FILENAMES, appDir), |
| 484 |
newFiles(APK_RESOURCES_FILENAMES, apkDir)) || (buildOptions.isAapt2() && !isFile(resourceFile))) { |
| 485 |
LOGGER.info("Building resources..."); |
| 486 |
|
| 487 |
if (buildOptions.debugMode) { |
| 488 |
if (buildOptions.isAapt2()) { |
| 489 |
LOGGER.info("Using aapt2 - setting 'debuggable' attribute to 'true' in AndroidManifest.xml"); |
| 490 |
ResXmlPatcher.setApplicationDebugTagTrue(new File(appDir, "AndroidManifest.xml")); |
| 491 |
} else { |
| 492 |
ResXmlPatcher.removeApplicationDebugTag(new File(appDir, "AndroidManifest.xml")); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
if (buildOptions.netSecConf) { |
| 497 |
MetaInfo meta = readMetaFile(new ExtFile(appDir)); |
| 498 |
if (meta.sdkInfo != null && meta.sdkInfo.get("targetSdkVersion") != null) { |
| 499 |
if (Integer.parseInt(meta.sdkInfo.get("targetSdkVersion")) < ResConfigFlags.SDK_NOUGAT) { |
| 500 |
LOGGER.warning("Target SDK version is lower than 24! Network Security Configuration might be ignored!"); |
| 501 |
} |
| 502 |
} |
| 503 |
File netSecConfOrig = new File(appDir, "res/xml/network_security_config.xml"); |
| 504 |
if (netSecConfOrig.exists()) { |
| 505 |
LOGGER.info("Replacing existing network_security_config.xml!"); |
| 506 |
netSecConfOrig.delete(); |
| 507 |
} |
| 508 |
ResXmlPatcher.modNetworkSecurityConfig(netSecConfOrig); |
| 509 |
ResXmlPatcher.setNetworkSecurityConfig(new File(appDir, "AndroidManifest.xml")); |
| 510 |
LOGGER.info("Added permissive network security config in manifest"); |
| 511 |
} |
| 512 |
|
| 513 |
File apkFile = File.createTempFile("APKTOOL", null); |
| 514 |
apkFile.delete(); |
| 515 |
resourceFile.delete(); |
| 516 |
|
| 517 |
File ninePatch = new File(appDir, "9patch"); |
| 518 |
if (!ninePatch.exists()) { |
| 519 |
ninePatch = null; |
| 520 |
} |
| 521 |
mAndRes.aaptPackage(apkFile, new File(appDir, |
| 522 |
"AndroidManifest.xml"), new File(appDir, "res"), |
| 523 |
ninePatch, null, parseUsesFramework(usesFramework)); |
| 524 |
|
| 525 |
ExtFile tmpExtFile = new ExtFile(apkFile); |
| 526 |
Directory tmpDir = tmpExtFile.getDirectory(); |
| 527 |
|
| 528 |
// Sometimes an application is built with a resources.arsc file with no resources, |
| 529 |
// Apktool assumes it will have a rebuilt arsc file, when it doesn't. So if we |
| 530 |
// encounter a copy error, move to a warning and continue on. (#1730) |
| 531 |
try { |
| 532 |
tmpDir.copyToDir(apkDir, |
| 533 |
tmpDir.containsDir("res") ? APK_RESOURCES_FILENAMES |
| 534 |
: APK_RESOURCES_WITHOUT_RES_FILENAMES); |
| 535 |
} catch (DirectoryException ex) { |
| 536 |
LOGGER.warning(ex.getMessage()); |
| 537 |
} finally { |
| 538 |
tmpExtFile.close(); |
| 539 |
} |
| 540 |
|
| 541 |
// delete tmpDir |
| 542 |
apkFile.delete(); |
| 543 |
} |
| 544 |
return true; |
| 545 |
} catch (IOException | BrutException | ParserConfigurationException | TransformerException | SAXException ex) { |
| 546 |
throw new AndrolibException(ex); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
public boolean buildManifestRaw(ExtFile appDir) |
| 551 |
throws AndrolibException { |
| 552 |
try { |
| 553 |
File apkDir = new File(appDir, APK_DIRNAME); |
| 554 |
LOGGER.info("Copying raw AndroidManifest.xml..."); |
| 555 |
appDir.getDirectory().copyToDir(apkDir, APK_MANIFEST_FILENAMES); |
| 556 |
return true; |
| 557 |
} catch (DirectoryException ex) { |
| 558 |
throw new AndrolibException(ex); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
public boolean buildManifest(ExtFile appDir, UsesFramework usesFramework) |
| 563 |
throws BrutException { |
| 564 |
try { |
| 565 |
if (!new File(appDir, "AndroidManifest.xml").exists()) { |
| 566 |
return false; |
| 567 |
} |
| 568 |
if (! buildOptions.forceBuildAll) { |
| 569 |
LOGGER.info("Checking whether resources has changed..."); |
| 570 |
} |
| 571 |
|
| 572 |
File apkDir = new File(appDir, APK_DIRNAME); |
| 573 |
|
| 574 |
if (buildOptions.forceBuildAll || isModified(newFiles(APK_MANIFEST_FILENAMES, appDir), |
| 575 |
newFiles(APK_MANIFEST_FILENAMES, apkDir))) { |
| 576 |
LOGGER.info("Building AndroidManifest.xml..."); |
| 577 |
|
| 578 |
File apkFile = File.createTempFile("APKTOOL", null); |
| 579 |
apkFile.delete(); |
| 580 |
|
| 581 |
File ninePatch = new File(appDir, "9patch"); |
| 582 |
if (!ninePatch.exists()) { |
| 583 |
ninePatch = null; |
| 584 |
} |
| 585 |
|
| 586 |
mAndRes.aaptPackage(apkFile, new File(appDir, |
| 587 |
"AndroidManifest.xml"), null, ninePatch, null, |
| 588 |
parseUsesFramework(usesFramework)); |
| 589 |
|
| 590 |
Directory tmpDir = new ExtFile(apkFile).getDirectory(); |
| 591 |
tmpDir.copyToDir(apkDir, APK_MANIFEST_FILENAMES); |
| 592 |
|
| 593 |
apkFile.delete(); |
| 594 |
} |
| 595 |
return true; |
| 596 |
} catch (IOException | DirectoryException ex) { |
| 597 |
throw new AndrolibException(ex); |
| 598 |
} catch (AndrolibException ex) { |
| 599 |
LOGGER.warning("Parse AndroidManifest.xml failed, treat it as raw file."); |
| 600 |
return buildManifestRaw(appDir); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
public void buildLibs(File appDir) throws AndrolibException { |
| 605 |
buildLibrary(appDir, "lib"); |
| 606 |
buildLibrary(appDir, "libs"); |
| 607 |
buildLibrary(appDir, "kotlin"); |
| 608 |
buildLibrary(appDir, "META-INF/services"); |
| 609 |
} |
| 610 |
|
| 611 |
public void buildLibrary(File appDir, String folder) throws AndrolibException { |
| 612 |
File working = new File(appDir, folder); |
| 613 |
|
| 614 |
if (! working.exists()) { |
| 615 |
return; |
| 616 |
} |
| 617 |
|
| 618 |
File stored = new File(appDir, APK_DIRNAME + "/" + folder); |
| 619 |
if (buildOptions.forceBuildAll || isModified(working, stored)) { |
| 620 |
LOGGER.info("Copying libs... (/" + folder + ")"); |
| 621 |
try { |
| 622 |
OS.rmdir(stored); |
| 623 |
OS.cpdir(working, stored); |
| 624 |
} catch (BrutException ex) { |
| 625 |
throw new AndrolibException(ex); |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
public void buildCopyOriginalFiles(File appDir) |
| 631 |
throws AndrolibException { |
| 632 |
if (buildOptions.copyOriginalFiles) { |
| 633 |
File originalDir = new File(appDir, "original"); |
| 634 |
if (originalDir.exists()) { |
| 635 |
try { |
| 636 |
LOGGER.info("Copy original files..."); |
| 637 |
Directory in = (new ExtFile(originalDir)).getDirectory(); |
| 638 |
if (in.containsFile("AndroidManifest.xml")) { |
| 639 |
LOGGER.info("Copy AndroidManifest.xml..."); |
| 640 |
in.copyToDir(new File(appDir, APK_DIRNAME), "AndroidManifest.xml"); |
| 641 |
} |
| 642 |
if (in.containsFile("stamp-cert-sha256")) { |
| 643 |
LOGGER.info("Copy stamp-cert-sha256..."); |
| 644 |
in.copyToDir(new File(appDir, APK_DIRNAME), "stamp-cert-sha256"); |
| 645 |
} |
| 646 |
if (in.containsDir("META-INF")) { |
| 647 |
LOGGER.info("Copy META-INF..."); |
| 648 |
in.copyToDir(new File(appDir, APK_DIRNAME), "META-INF"); |
| 649 |
} |
| 650 |
} catch (DirectoryException ex) { |
| 651 |
throw new AndrolibException(ex); |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
public void buildUnknownFiles(File appDir, File outFile, MetaInfo meta) |
| 658 |
throws AndrolibException { |
| 659 |
if (meta.unknownFiles != null) { |
| 660 |
LOGGER.info("Copying unknown files/dir..."); |
| 661 |
|
| 662 |
Map<String, String> files = meta.unknownFiles; |
| 663 |
File tempFile = new File(outFile.getParent(), outFile.getName() + ".apktool_temp"); |
| 664 |
boolean renamed = outFile.renameTo(tempFile); |
| 665 |
if (!renamed) { |
| 666 |
throw new AndrolibException("Unable to rename temporary file"); |
| 667 |
} |
| 668 |
|
| 669 |
try ( |
| 670 |
ZipFile inputFile = new ZipFile(tempFile); |
| 671 |
ZipOutputStream actualOutput = new ZipOutputStream(new FileOutputStream(outFile)) |
| 672 |
) { |
| 673 |
copyExistingFiles(inputFile, actualOutput); |
| 674 |
copyUnknownFiles(appDir, actualOutput, files); |
| 675 |
} catch (IOException | BrutException ex) { |
| 676 |
throw new AndrolibException(ex); |
| 677 |
} |
| 678 |
|
| 679 |
// Remove our temporary file. |
| 680 |
tempFile.delete(); |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
private void copyExistingFiles(ZipFile inputFile, ZipOutputStream outputFile) throws IOException { |
| 685 |
// First, copy the contents from the existing outFile: |
| 686 |
Enumeration<? extends ZipEntry> entries = inputFile.entries(); |
| 687 |
while (entries.hasMoreElements()) { |
| 688 |
ZipEntry entry = new ZipEntry(entries.nextElement()); |
| 689 |
|
| 690 |
// We can't reuse the compressed size because it depends on compression sizes. |
| 691 |
entry.setCompressedSize(-1); |
| 692 |
outputFile.putNextEntry(entry); |
| 693 |
|
| 694 |
// No need to create directory entries in the final apk |
| 695 |
if (! entry.isDirectory()) { |
| 696 |
BrutIO.copy(inputFile, outputFile, entry); |
| 697 |
} |
| 698 |
|
| 699 |
outputFile.closeEntry(); |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
private void copyUnknownFiles(File appDir, ZipOutputStream outputFile, Map<String, String> files) |
| 704 |
throws BrutException, IOException { |
| 705 |
File unknownFileDir = new File(appDir, UNK_DIRNAME); |
| 706 |
|
| 707 |
// loop through unknown files |
| 708 |
for (Map.Entry<String,String> unknownFileInfo : files.entrySet()) { |
| 709 |
File inputFile; |
| 710 |
|
| 711 |
try { |
| 712 |
inputFile = new File(unknownFileDir, BrutIO.sanitizeUnknownFile(unknownFileDir, unknownFileInfo.getKey())); |
| 713 |
} catch (RootUnknownFileException | InvalidUnknownFileException | TraversalUnknownFileException exception) { |
| 714 |
LOGGER.warning(String.format("Skipping file %s (%s)", unknownFileInfo.getKey(), exception.getMessage())); |
| 715 |
continue; |
| 716 |
} |
| 717 |
|
| 718 |
if (inputFile.isDirectory()) { |
| 719 |
continue; |
| 720 |
} |
| 721 |
|
| 722 |
ZipEntry newEntry = new ZipEntry(unknownFileInfo.getKey()); |
| 723 |
int method = Integer.parseInt(unknownFileInfo.getValue()); |
| 724 |
LOGGER.fine(String.format("Copying unknown file %s with method %d", unknownFileInfo.getKey(), method)); |
| 725 |
if (method == ZipEntry.STORED) { |
| 726 |
newEntry.setMethod(ZipEntry.STORED); |
| 727 |
newEntry.setSize(inputFile.length()); |
| 728 |
newEntry.setCompressedSize(-1); |
| 729 |
BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(inputFile)); |
| 730 |
CRC32 crc = BrutIO.calculateCrc(unknownFile); |
| 731 |
newEntry.setCrc(crc.getValue()); |
| 732 |
unknownFile.close(); |
| 733 |
} else { |
| 734 |
newEntry.setMethod(ZipEntry.DEFLATED); |
| 735 |
} |
| 736 |
outputFile.putNextEntry(newEntry); |
| 737 |
|
| 738 |
BrutIO.copy(inputFile, outputFile); |
| 739 |
outputFile.closeEntry(); |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
public void buildApk(File appDir, File outApk) throws AndrolibException { |
| 744 |
LOGGER.info("Building apk file..."); |
| 745 |
if (outApk.exists()) { |
| 746 |
outApk.delete(); |
| 747 |
} else { |
| 748 |
File outDir = outApk.getParentFile(); |
| 749 |
if (outDir != null && !outDir.exists()) { |
| 750 |
outDir.mkdirs(); |
| 751 |
} |
| 752 |
} |
| 753 |
File assetDir = new File(appDir, "assets"); |
| 754 |
if (!assetDir.exists()) { |
| 755 |
assetDir = null; |
| 756 |
} |
| 757 |
mAndRes.zipPackage(outApk, new File(appDir, APK_DIRNAME), assetDir); |
| 758 |
} |
| 759 |
|
| 760 |
public void publicizeResources(File arscFile) throws AndrolibException { |
| 761 |
mAndRes.publicizeResources(arscFile); |
| 762 |
} |
| 763 |
|
| 764 |
public void installFramework(File frameFile) |
| 765 |
throws AndrolibException { |
| 766 |
mAndRes.installFramework(frameFile); |
| 767 |
} |
| 768 |
|
| 769 |
public void listFrameworks() throws AndrolibException { |
| 770 |
mAndRes.listFrameworkDirectory(); |
| 771 |
} |
| 772 |
|
| 773 |
public void emptyFrameworkDirectory() throws AndrolibException { |
| 774 |
mAndRes.emptyFrameworkDirectory(); |
| 775 |
} |
| 776 |
|
| 777 |
public boolean isFrameworkApk(ResTable resTable) { |
| 778 |
for (ResPackage pkg : resTable.listMainPackages()) { |
| 779 |
if (pkg.getId() < 64) { |
| 780 |
return true; |
| 781 |
} |
| 782 |
} |
| 783 |
return false; |
| 784 |
} |
| 785 |
|
| 786 |
public static String getVersion() { |
| 787 |
return ApktoolProperties.get("application.version"); |
| 788 |
} |
| 789 |
|
| 790 |
private File[] parseUsesFramework(UsesFramework usesFramework) |
| 791 |
throws AndrolibException { |
| 792 |
if (usesFramework == null) { |
| 793 |
return null; |
| 794 |
} |
| 795 |
|
| 796 |
List<Integer> ids = usesFramework.ids; |
| 797 |
if (ids == null || ids.isEmpty()) { |
| 798 |
return null; |
| 799 |
} |
| 800 |
|
| 801 |
String tag = usesFramework.tag; |
| 802 |
File[] files = new File[ids.size()]; |
| 803 |
int i = 0; |
| 804 |
for (int id : ids) { |
| 805 |
files[i++] = mAndRes.getFrameworkApk(id, tag); |
| 806 |
} |
| 807 |
return files; |
| 808 |
} |
| 809 |
|
| 810 |
private boolean isModified(File working, File stored) { |
| 811 |
return ! stored.exists() || BrutIO.recursiveModifiedTime(working) > BrutIO .recursiveModifiedTime(stored); |
| 812 |
} |
| 813 |
|
| 814 |
private boolean isFile(File working) { |
| 815 |
return working.exists(); |
| 816 |
} |
| 817 |
|
| 818 |
private boolean isModified(File[] working, File[] stored) { |
| 819 |
for (File file : stored) { |
| 820 |
if (!file.exists()) { |
| 821 |
return true; |
| 822 |
} |
| 823 |
} |
| 824 |
return BrutIO.recursiveModifiedTime(working) > BrutIO.recursiveModifiedTime(stored); |
| 825 |
} |
| 826 |
|
| 827 |
private File[] newFiles(String[] names, File dir) { |
| 828 |
File[] files = new File[names.length]; |
| 829 |
for (int i = 0; i < names.length; i++) { |
| 830 |
files[i] = new File(dir, names[i]); |
| 831 |
} |
| 832 |
return files; |
| 833 |
} |
| 834 |
|
| 835 |
public void close() throws IOException { |
| 836 |
mAndRes.close(); |
| 837 |
} |
| 838 |
|
| 839 |
private final static Logger LOGGER = Logger.getLogger(Androlib.class.getName()); |
| 840 |
|
| 841 |
private final static String SMALI_DIRNAME = "smali"; |
| 842 |
private final static String APK_DIRNAME = "build/apk"; |
| 843 |
private final static String UNK_DIRNAME = "unknown"; |
| 844 |
private final static String[] APK_RESOURCES_FILENAMES = new String[] { |
| 845 |
"resources.arsc", "AndroidManifest.xml", "res", "r", "R" }; |
| 846 |
private final static String[] APK_RESOURCES_WITHOUT_RES_FILENAMES = new String[] { |
| 847 |
"resources.arsc", "AndroidManifest.xml" }; |
| 848 |
private final static String[] APP_RESOURCES_FILENAMES = new String[] { |
| 849 |
"AndroidManifest.xml", "res" }; |
| 850 |
private final static String[] APK_MANIFEST_FILENAMES = new String[] { |
| 851 |
"AndroidManifest.xml" }; |
| 852 |
private final static String[] APK_STANDARD_ALL_FILENAMES = new String[] { |
| 853 |
"classes.dex", "AndroidManifest.xml", "resources.arsc", "res", "r", "R", |
| 854 |
"lib", "libs", "assets", "META-INF", "kotlin" }; |
| 855 |
private final static Pattern NO_COMPRESS_PATTERN = Pattern.compile("(" + |
| 856 |
"jpg|jpeg|png|gif|wav|mp2|mp3|ogg|aac|mpg|mpeg|mid|midi|smf|jet|rtttl|imy|xmf|mp4|" + |
| 857 |
"m4a|m4v|3gp|3gpp|3g2|3gpp2|amr|awb|wma|wmv|webm|webp|mkv)$"); |
| 858 |
} |