Skip to content

Commit

Permalink
Adds ability to skip asset files
Browse files Browse the repository at this point in the history
 - fixes #1605
 - prevents rebuild
  • Loading branch information
iBotPeaches committed Sep 19, 2017
1 parent dc33533 commit f0f87c8
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 4 deletions.
10 changes: 10 additions & 0 deletions brut.apktool/apktool-cli/src/main/java/brut/apktool/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ private static void cmdDecode(CommandLine cli) throws AndrolibException {
if (cli.hasOption("r") || cli.hasOption("no-res")) {
decoder.setDecodeResources(ApkDecoder.DECODE_RESOURCES_NONE);
}
if (cli.hasOption("no-assets")) {
decoder.setDecodeAssets(ApkDecoder.DECODE_ASSETS_NONE);
}
if (cli.hasOption("k") || cli.hasOption("keep-broken-res")) {
decoder.setKeepBrokenResources(true);
}
Expand Down Expand Up @@ -283,6 +286,11 @@ private static void _Options() {
.desc("Do not decode resources.")
.build();

Option noAssetOption = Option.builder()
.longOpt("no-assets")
.desc("Do not decode assets.")
.build();

Option debugDecOption = Option.builder("d")
.longOpt("debug")
.desc("REMOVED (DOES NOT WORK): Decode in debug mode.")
Expand Down Expand Up @@ -396,6 +404,7 @@ private static void _Options() {
DecodeOptions.addOption(keepResOption);
DecodeOptions.addOption(analysisOption);
DecodeOptions.addOption(apiLevelOption);
DecodeOptions.addOption(noAssetOption);

BuildOptions.addOption(debugBuiOption);
BuildOptions.addOption(aaptOption);
Expand Down Expand Up @@ -444,6 +453,7 @@ private static void _Options() {
allOptions.addOption(analysisOption);
allOptions.addOption(debugDecOption);
allOptions.addOption(noDbgOption);
allOptions.addOption(noAssetOption);
allOptions.addOption(keepResOption);
allOptions.addOption(debugBuiOption);
allOptions.addOption(aaptOption);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,16 @@ public void decodeManifestWithResources(ExtFile apkFile, File outDir, ResTable r
mAndRes.decodeManifestWithResources(resTable, apkFile, outDir);
}

public void decodeRawFiles(ExtFile apkFile, File outDir)
public void decodeRawFiles(ExtFile apkFile, File outDir, short decodeAssetMode)
throws AndrolibException {
LOGGER.info("Copying assets and libs...");
try {
Directory in = apkFile.getDirectory();
if (in.containsDir("assets")) {
in.copyToDir(outDir, "assets");

if (decodeAssetMode == ApkDecoder.DECODE_ASSETS_FULL) {
if (in.containsDir("assets")) {
in.copyToDir(outDir, "assets");
}
}
if (in.containsDir("lib")) {
in.copyToDir(outDir, "lib");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void decode() throws AndrolibException, IOException, DirectoryException {
}
}

mAndrolib.decodeRawFiles(mApkFile, outDir);
mAndrolib.decodeRawFiles(mApkFile, outDir, mDecodeAssets);
mAndrolib.decodeUnknownFiles(mApkFile, outDir, mResTable);
mUncompressedFiles = new ArrayList<String>();
mAndrolib.recordUncompressedFiles(mApkFile, mUncompressedFiles);
Expand Down Expand Up @@ -190,6 +190,13 @@ public void setDecodeResources(short mode) throws AndrolibException {
mDecodeResources = mode;
}

public void setDecodeAssets(short mode) throws AndrolibException {
if (mode != DECODE_ASSETS_NONE && mode != DECODE_ASSETS_FULL) {
throw new AndrolibException("Invalid decode asset mode");
}
mDecodeAssets = mode;
}

public void setAnalysisMode(boolean mode, boolean pass) throws AndrolibException{
mAnalysisMode = mode;

Expand Down Expand Up @@ -299,6 +306,9 @@ public void close() throws IOException {
public final static short DECODE_RESOURCES_NONE = 0x0100;
public final static short DECODE_RESOURCES_FULL = 0x0101;

public final static short DECODE_ASSETS_NONE = 0x0000;
public final static short DECODE_ASSETS_FULL = 0x0001;

private File getOutDir() throws AndrolibException {
if (mOutDir == null) {
throw new AndrolibException("Out dir not set");
Expand Down Expand Up @@ -407,6 +417,7 @@ private void putSharedLibraryInfo(MetaInfo meta) throws AndrolibException {
private ResTable mResTable;
private short mDecodeSources = DECODE_SOURCES_SMALI;
private short mDecodeResources = DECODE_RESOURCES_FULL;
private short mDecodeAssets = DECODE_ASSETS_FULL;
private boolean mForceDelete = false;
private boolean mKeepBrokenResources = false;
private boolean mBakDeb = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Copyright (C) 2017 Ryszard Wiśniewski <brut.alll@gmail.com>
* Copyright (C) 2017 Connor Tumbleson <connor.tumbleson@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package brut.androlib;

import brut.directory.ExtFile;
import brut.common.BrutException;
import brut.util.OS;
import java.io.File;
import java.io.IOException;

import org.junit.*;
import static org.junit.Assert.*;

public class SkipAssetTest {

@BeforeClass
public static void beforeClass() throws Exception {
TestUtils.cleanFrameworkFile();
sTmpDir = new ExtFile(OS.createTempDirectory());
TestUtils.copyResourceDir(SkipAssetTest.class, "brut/apktool/issue1605/", sTmpDir);
}

@AfterClass
public static void afterClass() throws BrutException {
OS.rmdir(sTmpDir);
}

@Test
public void checkIfEnablingSkipAssetWorks() throws BrutException, IOException {
String apk = "issue1605.apk";

// decode issue1605.apk
ApkDecoder apkDecoder = new ApkDecoder(new File(sTmpDir + File.separator + apk));
sTestOrigDir = new ExtFile(sTmpDir + File.separator + apk + ".out");

apkDecoder.setOutDir(sTestOrigDir);
apkDecoder.setDecodeAssets(ApkDecoder.DECODE_ASSETS_NONE);
apkDecoder.setForceDelete(true);
apkDecoder.decode();

checkFileDoesNotExist("assets" + File.separator + "kotlin.kotlin_builtins");
checkFileDoesNotExist("assets" + File.separator + "ranges" + File.separator + "ranges.kotlin_builtins");
}

@Test
public void checkControl() throws BrutException, IOException {
String apk = "issue1605.apk";

// decode issue1605.apk
ApkDecoder apkDecoder = new ApkDecoder(new File(sTmpDir + File.separator + apk));
sTestOrigDir = new ExtFile(sTmpDir + File.separator + apk + ".out");

apkDecoder.setOutDir(sTestOrigDir);
apkDecoder.setDecodeAssets(ApkDecoder.DECODE_ASSETS_FULL);
apkDecoder.setForceDelete(true);
apkDecoder.decode();

checkFileDoesExist("assets" + File.separator + "kotlin.kotlin_builtins");
checkFileDoesExist("assets" + File.separator + "ranges" + File.separator + "ranges.kotlin_builtins");
}

private void checkFileDoesNotExist(String path) throws BrutException {
File f = new File(sTestOrigDir, path);

assertFalse(f.isFile());
}

private void checkFileDoesExist(String path) throws BrutException {
File f = new File(sTestOrigDir, path);

assertTrue(f.isFile());
}

private static ExtFile sTmpDir;
private static ExtFile sTestOrigDir;
}
Binary file not shown.

0 comments on commit f0f87c8

Please sign in to comment.