| 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.err.InFileNotFoundException; |
| 20 |
import brut.androlib.err.OutDirExistsException; |
| 21 |
import brut.androlib.err.UndefinedResObjectException; |
| 22 |
import brut.androlib.meta.MetaInfo; |
| 23 |
import brut.androlib.meta.PackageInfo; |
| 24 |
import brut.androlib.meta.UsesFramework; |
| 25 |
import brut.androlib.meta.VersionInfo; |
| 26 |
import brut.androlib.res.AndrolibResources; |
| 27 |
import brut.androlib.res.data.ResPackage; |
| 28 |
import brut.androlib.res.data.ResTable; |
| 29 |
import brut.directory.ExtFile; |
| 30 |
import brut.androlib.res.xml.ResXmlPatcher; |
| 31 |
import brut.common.BrutException; |
| 32 |
import brut.directory.DirectoryException; |
| 33 |
import brut.util.OS; |
| 34 |
import com.google.common.base.Strings; |
| 35 |
|
| 36 |
import java.io.File; |
| 37 |
import java.io.IOException; |
| 38 |
import java.util.*; |
| 39 |
import java.util.logging.Logger; |
| 40 |
|
| 41 |
public class ApkDecoder { |
| 42 |
public ApkDecoder() { |
| 43 |
this(new Androlib()); |
| 44 |
} |
| 45 |
|
| 46 |
public ApkDecoder(Androlib androlib) { |
| 47 |
mAndrolib = androlib; |
| 48 |
} |
| 49 |
|
| 50 |
public ApkDecoder(File apkFile) { |
| 51 |
this(apkFile, new Androlib()); |
| 52 |
} |
| 53 |
|
| 54 |
public ApkDecoder(File apkFile, Androlib androlib) { |
| 55 |
mAndrolib = androlib; |
| 56 |
setApkFile(apkFile); |
| 57 |
} |
| 58 |
|
| 59 |
public void setApkFile(File apkFile) { |
| 60 |
if (mApkFile != null) { |
| 61 |
try { |
| 62 |
mApkFile.close(); |
| 63 |
} catch (IOException ignored) {} |
| 64 |
} |
| 65 |
|
| 66 |
mApkFile = new ExtFile(apkFile); |
| 67 |
mResTable = null; |
| 68 |
} |
| 69 |
|
| 70 |
public void setOutDir(File outDir) { |
| 71 |
mOutDir = outDir; |
| 72 |
} |
| 73 |
|
| 74 |
public void decode() throws AndrolibException, IOException, DirectoryException { |
| 75 |
try { |
| 76 |
File outDir = getOutDir(); |
| 77 |
AndrolibResources.sKeepBroken = mKeepBrokenResources; |
| 78 |
|
| 79 |
if (!mForceDelete && outDir.exists()) { |
| 80 |
throw new OutDirExistsException(); |
| 81 |
} |
| 82 |
|
| 83 |
if (!mApkFile.isFile() || !mApkFile.canRead()) { |
| 84 |
throw new InFileNotFoundException(); |
| 85 |
} |
| 86 |
|
| 87 |
try { |
| 88 |
OS.rmdir(outDir); |
| 89 |
} catch (BrutException ex) { |
| 90 |
throw new AndrolibException(ex); |
| 91 |
} |
| 92 |
outDir.mkdirs(); |
| 93 |
|
| 94 |
LOGGER.info("Using Apktool " + Androlib.getVersion() + " on " + mApkFile.getName()); |
| 95 |
|
| 96 |
if (hasResources()) { |
| 97 |
switch (mDecodeResources) { |
| 98 |
case DECODE_RESOURCES_NONE: |
| 99 |
mAndrolib.decodeResourcesRaw(mApkFile, outDir); |
| 100 |
if (mForceDecodeManifest == FORCE_DECODE_MANIFEST_FULL) { |
| 101 |
// done after raw decoding of resources because copyToDir overwrites dest files |
| 102 |
if (hasManifest()) { |
| 103 |
mAndrolib.decodeManifestWithResources(mApkFile, outDir, getResTable()); |
| 104 |
} |
| 105 |
} |
| 106 |
break; |
| 107 |
case DECODE_RESOURCES_FULL: |
| 108 |
if (hasManifest()) { |
| 109 |
mAndrolib.decodeManifestWithResources(mApkFile, outDir, getResTable()); |
| 110 |
} |
| 111 |
mAndrolib.decodeResourcesFull(mApkFile, outDir, getResTable()); |
| 112 |
break; |
| 113 |
} |
| 114 |
} else { |
| 115 |
// if there's no resources.arsc, decode the manifest without looking |
| 116 |
// up attribute references |
| 117 |
if (hasManifest()) { |
| 118 |
if (mDecodeResources == DECODE_RESOURCES_FULL |
| 119 |
|| mForceDecodeManifest == FORCE_DECODE_MANIFEST_FULL) { |
| 120 |
mAndrolib.decodeManifestFull(mApkFile, outDir, getResTable()); |
| 121 |
} |
| 122 |
else { |
| 123 |
mAndrolib.decodeManifestRaw(mApkFile, outDir); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if (hasSources()) { |
| 129 |
switch (mDecodeSources) { |
| 130 |
case DECODE_SOURCES_NONE: |
| 131 |
mAndrolib.decodeSourcesRaw(mApkFile, outDir, "classes.dex"); |
| 132 |
break; |
| 133 |
case DECODE_SOURCES_SMALI: |
| 134 |
case DECODE_SOURCES_SMALI_ONLY_MAIN_CLASSES: |
| 135 |
mAndrolib.decodeSourcesSmali(mApkFile, outDir, "classes.dex", mBakDeb, mApiLevel); |
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if (hasMultipleSources()) { |
| 141 |
// foreach unknown dex file in root, lets disassemble it |
| 142 |
Set<String> files = mApkFile.getDirectory().getFiles(true); |
| 143 |
for (String file : files) { |
| 144 |
if (file.endsWith(".dex")) { |
| 145 |
if (! file.equalsIgnoreCase("classes.dex")) { |
| 146 |
switch(mDecodeSources) { |
| 147 |
case DECODE_SOURCES_NONE: |
| 148 |
mAndrolib.decodeSourcesRaw(mApkFile, outDir, file); |
| 149 |
break; |
| 150 |
case DECODE_SOURCES_SMALI: |
| 151 |
mAndrolib.decodeSourcesSmali(mApkFile, outDir, file, mBakDeb, mApiLevel); |
| 152 |
break; |
| 153 |
case DECODE_SOURCES_SMALI_ONLY_MAIN_CLASSES: |
| 154 |
if (file.startsWith("classes") && file.endsWith(".dex")) { |
| 155 |
mAndrolib.decodeSourcesSmali(mApkFile, outDir, file, mBakDeb, mApiLevel); |
| 156 |
} else { |
| 157 |
mAndrolib.decodeSourcesRaw(mApkFile, outDir, file); |
| 158 |
} |
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
mAndrolib.decodeRawFiles(mApkFile, outDir, mDecodeAssets); |
| 167 |
mAndrolib.decodeUnknownFiles(mApkFile, outDir); |
| 168 |
mUncompressedFiles = new ArrayList<>(); |
| 169 |
mAndrolib.recordUncompressedFiles(mApkFile, mUncompressedFiles); |
| 170 |
mAndrolib.writeOriginalFiles(mApkFile, outDir); |
| 171 |
writeMetaFile(); |
| 172 |
} finally { |
| 173 |
try { |
| 174 |
mApkFile.close(); |
| 175 |
} catch (IOException ignored) {} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
public void setDecodeSources(short mode) throws AndrolibException { |
| 180 |
if (mode != DECODE_SOURCES_NONE && mode != DECODE_SOURCES_SMALI && mode != DECODE_SOURCES_SMALI_ONLY_MAIN_CLASSES) { |
| 181 |
throw new AndrolibException("Invalid decode sources mode: " + mode); |
| 182 |
} |
| 183 |
if (mDecodeSources == DECODE_SOURCES_NONE && mode == DECODE_SOURCES_SMALI_ONLY_MAIN_CLASSES) { |
| 184 |
LOGGER.info("--only-main-classes cannot be paired with -s/--no-src. Ignoring."); |
| 185 |
return; |
| 186 |
} |
| 187 |
mDecodeSources = mode; |
| 188 |
} |
| 189 |
|
| 190 |
public void setDecodeResources(short mode) throws AndrolibException { |
| 191 |
if (mode != DECODE_RESOURCES_NONE && mode != DECODE_RESOURCES_FULL) { |
| 192 |
throw new AndrolibException("Invalid decode resources mode"); |
| 193 |
} |
| 194 |
mDecodeResources = mode; |
| 195 |
} |
| 196 |
|
| 197 |
public void setForceDecodeManifest(short mode) throws AndrolibException { |
| 198 |
if (mode != FORCE_DECODE_MANIFEST_NONE && mode != FORCE_DECODE_MANIFEST_FULL) { |
| 199 |
throw new AndrolibException("Invalid force decode manifest mode"); |
| 200 |
} |
| 201 |
mForceDecodeManifest = mode; |
| 202 |
} |
| 203 |
|
| 204 |
public void setDecodeAssets(short mode) throws AndrolibException { |
| 205 |
if (mode != DECODE_ASSETS_NONE && mode != DECODE_ASSETS_FULL) { |
| 206 |
throw new AndrolibException("Invalid decode asset mode"); |
| 207 |
} |
| 208 |
mDecodeAssets = mode; |
| 209 |
} |
| 210 |
|
| 211 |
public void setAnalysisMode(boolean mode) { |
| 212 |
mAnalysisMode = mode; |
| 213 |
|
| 214 |
if (mResTable != null) { |
| 215 |
mResTable.setAnalysisMode(mode); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
public void setApiLevel(int apiLevel) { |
| 220 |
mApiLevel = apiLevel; |
| 221 |
} |
| 222 |
|
| 223 |
public void setBaksmaliDebugMode(boolean bakDeb) { |
| 224 |
mBakDeb = bakDeb; |
| 225 |
} |
| 226 |
|
| 227 |
public void setForceDelete(boolean forceDelete) { |
| 228 |
mForceDelete = forceDelete; |
| 229 |
} |
| 230 |
|
| 231 |
public void setFrameworkTag(String tag) { |
| 232 |
mAndrolib.buildOptions.frameworkTag = tag; |
| 233 |
} |
| 234 |
|
| 235 |
public void setKeepBrokenResources(boolean keepBrokenResources) { |
| 236 |
mKeepBrokenResources = keepBrokenResources; |
| 237 |
} |
| 238 |
|
| 239 |
public void setFrameworkDir(String dir) { |
| 240 |
mAndrolib.buildOptions.frameworkFolderLocation = dir; |
| 241 |
} |
| 242 |
|
| 243 |
public ResTable getResTable() throws AndrolibException { |
| 244 |
if (mResTable == null) { |
| 245 |
boolean hasResources = hasResources(); |
| 246 |
boolean hasManifest = hasManifest(); |
| 247 |
if (! (hasManifest || hasResources)) { |
| 248 |
throw new AndrolibException( |
| 249 |
"Apk doesn't contain either AndroidManifest.xml file or resources.arsc file"); |
| 250 |
} |
| 251 |
mResTable = mAndrolib.getResTable(mApkFile, hasResources); |
| 252 |
mResTable.setAnalysisMode(mAnalysisMode); |
| 253 |
} |
| 254 |
return mResTable; |
| 255 |
} |
| 256 |
|
| 257 |
public boolean hasSources() throws AndrolibException { |
| 258 |
try { |
| 259 |
return mApkFile.getDirectory().containsFile("classes.dex"); |
| 260 |
} catch (DirectoryException ex) { |
| 261 |
throw new AndrolibException(ex); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
public boolean hasMultipleSources() throws AndrolibException { |
| 266 |
try { |
| 267 |
Set<String> files = mApkFile.getDirectory().getFiles(false); |
| 268 |
for (String file : files) { |
| 269 |
if (file.endsWith(".dex")) { |
| 270 |
if (! file.equalsIgnoreCase("classes.dex")) { |
| 271 |
return true; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
return false; |
| 277 |
} catch (DirectoryException ex) { |
| 278 |
throw new AndrolibException(ex); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
public boolean hasManifest() throws AndrolibException { |
| 283 |
try { |
| 284 |
return mApkFile.getDirectory().containsFile("AndroidManifest.xml"); |
| 285 |
} catch (DirectoryException ex) { |
| 286 |
throw new AndrolibException(ex); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
public boolean hasResources() throws AndrolibException { |
| 291 |
try { |
| 292 |
return mApkFile.getDirectory().containsFile("resources.arsc"); |
| 293 |
} catch (DirectoryException ex) { |
| 294 |
throw new AndrolibException(ex); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
public void close() throws IOException { |
| 299 |
if (mAndrolib != null) { |
| 300 |
mAndrolib.close(); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
public final static short DECODE_SOURCES_NONE = 0x0000; |
| 305 |
public final static short DECODE_SOURCES_SMALI = 0x0001; |
| 306 |
public final static short DECODE_SOURCES_SMALI_ONLY_MAIN_CLASSES = 0x0010; |
| 307 |
|
| 308 |
public final static short DECODE_RESOURCES_NONE = 0x0100; |
| 309 |
public final static short DECODE_RESOURCES_FULL = 0x0101; |
| 310 |
|
| 311 |
public final static short FORCE_DECODE_MANIFEST_NONE = 0x0000; |
| 312 |
public final static short FORCE_DECODE_MANIFEST_FULL = 0x0001; |
| 313 |
|
| 314 |
public final static short DECODE_ASSETS_NONE = 0x0000; |
| 315 |
public final static short DECODE_ASSETS_FULL = 0x0001; |
| 316 |
|
| 317 |
private File getOutDir() throws AndrolibException { |
| 318 |
if (mOutDir == null) { |
| 319 |
throw new AndrolibException("Out dir not set"); |
| 320 |
} |
| 321 |
return mOutDir; |
| 322 |
} |
| 323 |
|
| 324 |
private void writeMetaFile() throws AndrolibException { |
| 325 |
MetaInfo meta = new MetaInfo(); |
| 326 |
meta.version = Androlib.getVersion(); |
| 327 |
meta.apkFileName = mApkFile.getName(); |
| 328 |
|
| 329 |
if (mResTable != null) { |
| 330 |
meta.isFrameworkApk = mAndrolib.isFrameworkApk(mResTable); |
| 331 |
putUsesFramework(meta); |
| 332 |
putSdkInfo(meta); |
| 333 |
putPackageInfo(meta); |
| 334 |
putVersionInfo(meta); |
| 335 |
putSharedLibraryInfo(meta); |
| 336 |
putSparseResourcesInfo(meta); |
| 337 |
} else { |
| 338 |
putMinSdkInfo(meta); |
| 339 |
} |
| 340 |
putUnknownInfo(meta); |
| 341 |
putFileCompressionInfo(meta); |
| 342 |
|
| 343 |
mAndrolib.writeMetaFile(mOutDir, meta); |
| 344 |
} |
| 345 |
|
| 346 |
private void putUsesFramework(MetaInfo meta) { |
| 347 |
Set<ResPackage> pkgs = mResTable.listFramePackages(); |
| 348 |
if (pkgs.isEmpty()) { |
| 349 |
return; |
| 350 |
} |
| 351 |
|
| 352 |
Integer[] ids = new Integer[pkgs.size()]; |
| 353 |
int i = 0; |
| 354 |
for (ResPackage pkg : pkgs) { |
| 355 |
ids[i++] = pkg.getId(); |
| 356 |
} |
| 357 |
Arrays.sort(ids); |
| 358 |
|
| 359 |
meta.usesFramework = new UsesFramework(); |
| 360 |
meta.usesFramework.ids = Arrays.asList(ids); |
| 361 |
|
| 362 |
if (mAndrolib.buildOptions.frameworkTag != null) { |
| 363 |
meta.usesFramework.tag = mAndrolib.buildOptions.frameworkTag; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
private void putSdkInfo(MetaInfo meta) { |
| 368 |
Map<String, String> info = mResTable.getSdkInfo(); |
| 369 |
if (info.size() > 0) { |
| 370 |
String refValue; |
| 371 |
if (info.get("minSdkVersion") != null) { |
| 372 |
refValue = ResXmlPatcher.pullValueFromIntegers(mOutDir, info.get("minSdkVersion")); |
| 373 |
if (refValue != null) { |
| 374 |
info.put("minSdkVersion", refValue); |
| 375 |
} |
| 376 |
} |
| 377 |
if (info.get("targetSdkVersion") != null) { |
| 378 |
refValue = ResXmlPatcher.pullValueFromIntegers(mOutDir, info.get("targetSdkVersion")); |
| 379 |
if (refValue != null) { |
| 380 |
info.put("targetSdkVersion", refValue); |
| 381 |
} |
| 382 |
} |
| 383 |
if (info.get("maxSdkVersion") != null) { |
| 384 |
refValue = ResXmlPatcher.pullValueFromIntegers(mOutDir, info.get("maxSdkVersion")); |
| 385 |
if (refValue != null) { |
| 386 |
info.put("maxSdkVersion", refValue); |
| 387 |
} |
| 388 |
} |
| 389 |
meta.sdkInfo = info; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
private void putMinSdkInfo(MetaInfo meta) { |
| 394 |
int minSdkVersion = mAndrolib.getMinSdkVersion(); |
| 395 |
if (minSdkVersion > 0) { |
| 396 |
Map<String, String> sdkInfo = new LinkedHashMap<>(); |
| 397 |
sdkInfo.put("minSdkVersion", Integer.toString(minSdkVersion)); |
| 398 |
meta.sdkInfo = sdkInfo; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
private void putPackageInfo(MetaInfo meta) throws AndrolibException { |
| 403 |
String renamed = mResTable.getPackageRenamed(); |
| 404 |
String original = mResTable.getPackageOriginal(); |
| 405 |
|
| 406 |
int id = mResTable.getPackageId(); |
| 407 |
try { |
| 408 |
id = mResTable.getPackage(renamed).getId(); |
| 409 |
} catch (UndefinedResObjectException ignored) {} |
| 410 |
|
| 411 |
if (Strings.isNullOrEmpty(original)) { |
| 412 |
return; |
| 413 |
} |
| 414 |
|
| 415 |
meta.packageInfo = new PackageInfo(); |
| 416 |
|
| 417 |
// only put rename-manifest-package into apktool.yml, if the change will be required |
| 418 |
if (!renamed.equalsIgnoreCase(original)) { |
| 419 |
meta.packageInfo.renameManifestPackage = renamed; |
| 420 |
} |
| 421 |
meta.packageInfo.forcedPackageId = String.valueOf(id); |
| 422 |
} |
| 423 |
|
| 424 |
private void putVersionInfo(MetaInfo meta) { |
| 425 |
VersionInfo info = mResTable.getVersionInfo(); |
| 426 |
String refValue = ResXmlPatcher.pullValueFromStrings(mOutDir, info.versionName); |
| 427 |
if (refValue != null) { |
| 428 |
info.versionName = refValue; |
| 429 |
} |
| 430 |
meta.versionInfo = info; |
| 431 |
} |
| 432 |
|
| 433 |
private void putSharedLibraryInfo(MetaInfo meta) { |
| 434 |
meta.sharedLibrary = mResTable.getSharedLibrary(); |
| 435 |
} |
| 436 |
|
| 437 |
private void putSparseResourcesInfo(MetaInfo meta) { |
| 438 |
meta.sparseResources = mResTable.getSparseResources(); |
| 439 |
} |
| 440 |
|
| 441 |
private void putUnknownInfo(MetaInfo meta) { |
| 442 |
meta.unknownFiles = mAndrolib.mResUnknownFiles.getUnknownFiles(); |
| 443 |
} |
| 444 |
|
| 445 |
private void putFileCompressionInfo(MetaInfo meta) { |
| 446 |
if (mUncompressedFiles != null && !mUncompressedFiles.isEmpty()) { |
| 447 |
meta.doNotCompress = mUncompressedFiles; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
private final Androlib mAndrolib; |
| 452 |
|
| 453 |
private final static Logger LOGGER = Logger.getLogger(Androlib.class.getName()); |
| 454 |
|
| 455 |
private ExtFile mApkFile; |
| 456 |
private File mOutDir; |
| 457 |
private ResTable mResTable; |
| 458 |
private short mDecodeSources = DECODE_SOURCES_SMALI; |
| 459 |
private short mDecodeResources = DECODE_RESOURCES_FULL; |
| 460 |
private short mForceDecodeManifest = FORCE_DECODE_MANIFEST_NONE; |
| 461 |
private short mDecodeAssets = DECODE_ASSETS_FULL; |
| 462 |
private boolean mForceDelete = false; |
| 463 |
private boolean mKeepBrokenResources = false; |
| 464 |
private boolean mBakDeb = true; |
| 465 |
private Collection<String> mUncompressedFiles; |
| 466 |
private boolean mAnalysisMode = false; |
| 467 |
private int mApiLevel = 0; |
| 468 |
} |