Search code examples
androidxamarinxamarin.androidapktoolpackage-name

Xamarin.Android edit apk package name with ApkTool causes crash


I need to be able to change out the package name of an existing .apk in order to run simultaneous instances of an Android app I created in Visual Studio using Xamarin. However, I will not reliably have access to the computer with the Xamarin license when I need to change this package name, so I can't simply edit the manifest pre-build. I tried using ApkTool to unpack the apk, edit the manifest, then re-package and re-sign the apk. However, while I am able to install this new apk on a device side-by-side with the original apk, the new app instance fails to start. I receive the following error:

"monodroid" "No assemblies found in '(null)' or '/storage/emulated/0/Android/data/com.newpackagename/files/.override'. Assuming this is part of Fast Deployment. Exiting..."

I assume this means that somehow changing the package name post-build has lost some link to the assemblies. However, I can find no remaining reference to my original package name even after searching the entire unpacked apk (both folders and within files). So I'd like to figure out how to regain these assemblies to allow my app to run. I understand this is very unusual and it may not even be the right way to go about it, but I need the ability to change my package name without having access to Xamarin. This seems like the most promising solution.


Solution

  • Xamarin relies on storing files, unknown to aapt (Android Asset Packaging Tool), within the final built apk. These files are inserted usually in the assemblies folder within the root of the apk with the STORED compression type so that Xamarin can access the files without decompression.

    Apktool used to ignore all files that aapt would ignore. IE. If the file isn't a folder/file of this array

    private final static String[] APK_STANDARD_ALL_FILENAMES = new String[] {
            "classes.dex", "AndroidManifest.xml", "resources.arsc", "res", "lib", "libs", "assets", "META-INF" };
    

    than Apktool would ignore it. However, an attempt was made during development of 2.0.x to include these unknown files.

    A bug with the Java 7 NIO library caused all "unknown" files to be stored as DEFLATED. This changed the storage format of the DLLs from STORED to DEFLATED thus this error.

    This error was fixed on March 25 - https://github.com/iBotPeaches/Apktool/commit/628286c022e3a872d6ab6bfb3431579f98743c25

    As of April 8, 2015 - There are no official releases with this fix in it. You may build Apktool yourself though until 2.0.0 Gold is released.