'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 1:[1]

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

Solution 2:[2]

When you set the minifyenabled as true. The r8 will choose the configuration files but not only the proguard-rules.pro and the app/build.gradle to shrink, obfuscate, and optimize your app.

There are some others files such as AAR libraries: <library-dir>/proguard.txt and JAR libraries: <library-dir>/META-INF/proguard/ and so on. So this error may be caused by the native library losing when you set the set the minifyenabled as true.

If you need more information, please check the official document:https://developer.android.com/studio/build/shrink-code#enable

In addition, you can check the 'proguard-android-optimize.txt' and when you add the -dontoptimize to proguard-rules.pro may cause a conflict.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stoberdo
Solution 2 Liyun Zhang - MSFT