Search code examples
androidxamarin.androidandroid-gradle-pluginproguardandroid-r8

What does 'minifyEnabled=true' do when all configuration options are disabled?


I have a complicated build setup for an android app which basically consists of a normal android app fused together with a Xamarin/Mono project in order to include an important C# library (like this: https://github.com/royd/KotlinAppWithXamarinDependency)

Everything is working fine except if I enable minification in my app/build.gradle via minifyEnabled true the app instantly crashes on startup because the Mono-runtime can't find native assemblies that are definitely contained in the apk.

This is the message I get in Logcat:

A/monodroid: No assemblies found in '(null)' or '<unavailable>'. Assuming this is part of Fast Deployment. Exiting...

With minifyEnabled false everything is working fine so I tried disabling all config options in my proguard-rules.pro:

-dontobfuscate
-dontoptimize
-dontshrink

And I also added the following lines to my app/build.gradle

packagingOptions {
    doNotStrip "*/armeabi/*.so"
    doNotStrip "*/armeabi-v7a/*.so"
    doNotStrip "*/x86/*.so"
}

Unfortunately all this doesn't help.

I also decompiled a working and a broken apk with dex2jar to compare the bytecode. It seems to be exactly the same except for some enum-optimizations that shouldn't matter.

According to the error message in Logcat the error seems to be thrown from the native library libmonodroid.so.

So my question: What does minifyenabled flag do when all these config options are disabled?

Edit: I have found out that minification works as intended when I use version 4.0.1 of Android Gradle Plugin (from July 2020). Upgrading the version to 4.1.0 (August 2020) breaks my app. Now the question is what changed between these two versions?


Solution

  • I found out, that in the Android Gradle Plugin versions 3.6.0 to 4.1.0 they switched to a more performant tool for building apks called zipflinger. This tool can be disabled by adding this line to my gradle.properties:

    android.useNewApkCreator=false
    

    When building the apk zipflinger stores the external .NET assemblies as DEFLATED zip entries instead of STORED and thats why monodroid cant read them.

    References:
    https://github.com/xamarin/xamarin-android/issues/6838#issuecomment-1110816027 https://copyfuture.com/blogs-details/20210119115509664T