Remove previous android:debuggable value to allow changing

- adds unit test
 - normalizeNewlines moved to TestUtils
This commit is contained in:
Connor Tumbleson 2016-04-28 08:31:36 -04:00
parent 032a3e5e25
commit 87315af36e
8 changed files with 149 additions and 7 deletions

View File

@ -409,6 +409,10 @@ public class Androlib {
newFiles(APK_RESOURCES_FILENAMES, apkDir))) {
LOGGER.info("Building resources...");
if (apkOptions.debugMode) {
ResXmlPatcher.removeApplicationDebugTag(new File(appDir, "AndroidManifest.xml"));
}
File apkFile = File.createTempFile("APKTOOL", null);
apkFile.delete();
@ -476,7 +480,6 @@ public class Androlib {
Directory tmpDir = new ExtFile(apkFile).getDirectory();
tmpDir.copyToDir(apkDir, APK_MANIFEST_FILENAMES);
}
return true;
} catch (IOException | DirectoryException ex) {

View File

@ -40,6 +40,35 @@ import java.io.IOException;
* @author Connor Tumbleson <connor.tumbleson@gmail.com>
*/
public final class ResXmlPatcher {
/**
* Removes "debug" tag from file
*
* @param file AndroidManifest file
* @throws AndrolibException
*/
public static void removeApplicationDebugTag(File file) throws AndrolibException {
if (file.exists()) {
try {
Document doc = loadDocument(file);
Node application = doc.getElementsByTagName("application").item(0);
// load attr
NamedNodeMap attr = application.getAttributes();
Node debugAttr = attr.getNamedItem("android:debuggable");
// remove application:debuggable
if (debugAttr != null) {
attr.removeNamedItem("android:debuggable");
}
saveDocument(file, doc);
} catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) {
}
}
}
/**
* Any @string reference in a <provider> value in AndroidManifest.xml will break on
* build, thus preventing the application from installing. This is from a bug/error

View File

@ -0,0 +1,90 @@
/**
* Copyright 2014 Ryszard Wiśniewski <brut.alll@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.androlib.res.util.ExtFile;
import brut.common.BrutException;
import brut.util.OS;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Logger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Connor Tumbleson <connor.tumbleson@gmail.com>
*/
public class DebugTagRetainedTest {
@BeforeClass
public static void beforeClass() throws Exception, BrutException {
TestUtils.cleanFrameworkFile();
sTmpDir = new ExtFile(OS.createTempDirectory());
sTestOrigDir = new ExtFile(sTmpDir, "issue1235-orig");
sTestNewDir = new ExtFile(sTmpDir, "issue1235-new");
LOGGER.info("Unpacking issue1235...");
TestUtils.copyResourceDir(BuildAndDecodeJarTest.class, "brut/apktool/issue1235/", sTestOrigDir);
LOGGER.info("Building issue1235.apk...");
ApkOptions apkOptions = new ApkOptions();
apkOptions.debugMode = true;
File testApk = new File(sTmpDir, "issue1235.apk");
new Androlib(apkOptions).build(sTestOrigDir, testApk);
LOGGER.info("Decoding issue1235.apk...");
ApkDecoder apkDecoder = new ApkDecoder(testApk);
apkDecoder.setOutDir(sTestNewDir);
apkDecoder.decode();
}
@AfterClass
public static void afterClass() throws BrutException {
OS.rmdir(sTmpDir);
}
@Test
public void buildAndDecodeTest() throws BrutException {
assertTrue(sTestNewDir.isDirectory());
}
@Test
public void DebugIsTruePriorToBeingFalseTest() throws BrutException, IOException {
String apk = "issue1235-new";
String expected = TestUtils.replaceNewlines("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n" +
"<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" package=\"com.ibotpeaches.issue1235\" platformBuildVersionCode=\"23\" platformBuildVersionName=\"6.0-2438415\">\n" +
" <application android:debuggable=\"true\"/>" +
"</manifest>");
byte[] encoded = Files.readAllBytes(Paths.get(sTmpDir + File.separator + apk + File.separator + "AndroidManifest.xml"));
String obtained = TestUtils.replaceNewlines(new String(encoded));
assertEquals(expected, obtained);
}
private static ExtFile sTmpDir;
private static ExtFile sTestOrigDir;
private static ExtFile sTestNewDir;
private final static Logger LOGGER = Logger.getLogger(BuildAndDecodeJarTest.class.getName());
}

View File

@ -66,7 +66,7 @@ public class ProviderAttributeTest {
apkDecoder.setOutDir(new File(sTmpDir + File.separator + apk + ".out.two"));
apkDecoder.decode();
String expected = replaceNewlines("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n" +
String expected = TestUtils.replaceNewlines("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n" +
"<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" package=\"com.ibotpeaches.issue636\" platformBuildVersionCode=\"23\" platformBuildVersionName=\"6.0-2438415\">\n" +
" <application android:allowBackup=\"true\" android:debuggable=\"true\" android:icon=\"@mipmap/ic_launcher\" android:label=\"@string/app_name\" android:theme=\"@style/AppTheme\">\n" +
" <provider android:authorities=\"com.ibotpeaches.issue636.Provider\" android:exported=\"false\" android:grantUriPermissions=\"true\" android:label=\"@string/app_name\" android:multiprocess=\"false\" android:name=\"com.ibotpeaches.issue636.Provider\"/>\n" +
@ -76,7 +76,7 @@ public class ProviderAttributeTest {
byte[] encoded = Files.readAllBytes(Paths.get(sTmpDir + File.separator + apk + ".out.two" + File.separator + "AndroidManifest.xml"));
String obtained = replaceNewlines(new String(encoded));
String obtained = TestUtils.replaceNewlines(new String(encoded));
assertEquals(expected, obtained);
}
@ -84,9 +84,5 @@ public class ProviderAttributeTest {
return Files.exists(Paths.get(sTmpDir.getAbsolutePath() + File.separator + filepath));
}
private String replaceNewlines(String value) {
return value.replace("\n", "").replace("\r", "");
}
private static ExtFile sTmpDir;
}

View File

@ -159,4 +159,8 @@ public abstract class TestUtils {
return controlType.equals(testType) && control.getAttribute("name").equals(test.getAttribute("name"));
}
}
public static String replaceNewlines(String value) {
return value.replace("\n", "").replace("\r", "");
}
}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.ibotpeaches.issue1235" xmlns:android="http://schemas.android.com/apk/res/android" platformBuildVersionCode="20" platformBuildVersionName="4.4W.2-1537038">
<application android:debuggable="false"/>
</manifest>

View File

@ -0,0 +1,12 @@
version: 2.0.0
apkFileName: issue1235.apk
isFrameworkApk: false
usesFramework:
ids:
- 1
packageInfo:
forced-package-id: '127'
versionInfo:
versionCode: '1'
versionName: '1.0'
compressionType: false

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">Hello World</string>
</resources>