diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java index c2499924..da0d6871 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/Androlib.java @@ -239,6 +239,14 @@ public class Androlib { } if (in.containsDir("META-INF")) { in.copyToDir(originalDir, "META-INF"); + + if(in.containsDir("META-INF/services")){ + // If the original APK contains the folder META-INF/services folder + // that is used for service locators (like coroutines on android), + // copy it to the destination folder so it does not get dropped. + LOGGER.info("Copying META-INF/services directory"); + in.copyToDir(outDir, "META-INF/services"); + } } } catch (DirectoryException ex) { throw new AndrolibException(ex); @@ -560,6 +568,7 @@ public class Androlib { buildLibrary(appDir, "lib"); buildLibrary(appDir, "libs"); buildLibrary(appDir, "kotlin"); + buildLibrary(appDir, "META-INF/services"); } public void buildLibrary(File appDir, String folder) throws AndrolibException { diff --git a/brut.apktool/apktool-lib/src/test/java/brut/androlib/decode/DecodeKotlinCoroutinesTest.java b/brut.apktool/apktool-lib/src/test/java/brut/androlib/decode/DecodeKotlinCoroutinesTest.java new file mode 100644 index 00000000..df1f73cc --- /dev/null +++ b/brut.apktool/apktool-lib/src/test/java/brut/androlib/decode/DecodeKotlinCoroutinesTest.java @@ -0,0 +1,107 @@ +/** + * Copyright (C) 2018 Ryszard Wiśniewski + * Copyright (C) 2018 Connor Tumbleson + *

+ * 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.decode; + +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import brut.androlib.Androlib; +import brut.androlib.AndrolibException; +import brut.androlib.ApkDecoder; +import brut.androlib.BaseTest; +import brut.androlib.TestUtils; +import brut.common.BrutException; +import brut.directory.DirectoryException; +import brut.directory.ExtFile; +import brut.util.OS; + +import static org.junit.Assert.assertTrue; + +/** + * @author Adib Faramarzi + */ +public class DecodeKotlinCoroutinesTest extends BaseTest { + private static String apk = "test-kotlin-coroutines.apk"; + @BeforeClass + public static void beforeClass() throws Exception { + TestUtils.cleanFrameworkFile(); + sTmpDir = new ExtFile(OS.createTempDirectory()); + TestUtils.copyResourceDir(DecodeKotlinCoroutinesTest.class, "decode/kotlin-coroutines/", sTmpDir); + } + + @AfterClass + public static void afterClass() throws BrutException { + OS.rmdir(sTmpDir); + } + + + @Test + public void kotlinCoroutinesDecodeTest() throws IOException, AndrolibException, DirectoryException { + + // decode kotlin coroutines + ApkDecoder apkDecoder = new ApkDecoder(new File(sTmpDir + File.separator + apk)); + apkDecoder.setOutDir(new File(sTmpDir + File.separator + apk + ".out")); + apkDecoder.setForceDelete(true); + apkDecoder.decode(); + File coroutinesExceptionHandler = new File(sTmpDir + File.separator + apk + ".out"+File.separator + "META-INF" + File.separator + "services", "kotlinx.coroutines.CoroutineExceptionHandler"); + File coroutinenMainDispatcherHandler = new File(sTmpDir + File.separator + apk + ".out"+File.separator + "META-INF" + File.separator + "services", "kotlinx.coroutines.internal.MainDispatcherFactory"); + + assert (coroutinesExceptionHandler.exists()); + assert (coroutinenMainDispatcherHandler.exists()); + } + + @Test + public void kotlinCoroutinesEncodeAfterDecodeTest() throws IOException, BrutException { + + // decode kotlin coroutines + ApkDecoder apkDecoder = new ApkDecoder(new File(sTmpDir + File.separator + apk)); + apkDecoder.setOutDir(new File(sTmpDir + File.separator + apk + ".out")); + apkDecoder.setForceDelete(true); + apkDecoder.decode(); + + // build kotlin coroutines + ExtFile testApk = new ExtFile(sTmpDir, apk + ".out"); + new Androlib().build(testApk, null); + String newApk = apk + ".out" + File.separator + "dist" + File.separator + apk; + assertTrue(fileExists(newApk)); + + // decode kotlin coroutines again + apkDecoder = new ApkDecoder(new File(sTmpDir + File.separator + newApk)); + apkDecoder.setOutDir(new File(sTmpDir + File.separator + apk + ".out.two")); + apkDecoder.setForceDelete(true); + apkDecoder.decode(); + + Files.readAllBytes(Paths.get(sTmpDir + File.separator + apk + ".out.two" + File.separator + "AndroidManifest.xml")); + File coroutinesExceptionHandler = new File(sTmpDir + File.separator + apk + ".out.two" + File.separator + "META-INF" + File.separator + "services", "kotlinx.coroutines.CoroutineExceptionHandler"); + File coroutinenMainDispatcherHandler = new File(sTmpDir + File.separator + apk + ".out.two" + File.separator + "META-INF" + File.separator + "services", "kotlinx.coroutines.internal.MainDispatcherFactory"); + + assert (coroutinesExceptionHandler.exists()); + assert (coroutinenMainDispatcherHandler.exists()); + } + + private boolean fileExists(String filepath) { + return Files.exists(Paths.get(sTmpDir.getAbsolutePath() + File.separator + filepath)); + } +} \ No newline at end of file diff --git a/brut.apktool/apktool-lib/src/test/resources/decode/kotlin-coroutines/test-kotlin-coroutines.apk b/brut.apktool/apktool-lib/src/test/resources/decode/kotlin-coroutines/test-kotlin-coroutines.apk new file mode 100644 index 00000000..f2cb8400 Binary files /dev/null and b/brut.apktool/apktool-lib/src/test/resources/decode/kotlin-coroutines/test-kotlin-coroutines.apk differ