0

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?

Stoberdo
  • 11
  • 1
  • 4

2 Answers2

0

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.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • Thanks for the hints, but unfortunately adding more proguard configs didn't help. I edited my question with what I found out: It seems that a change in AGP vom 4.0.1 to 4.1.0 causes my error, but I don't know what exact change. – Stoberdo Mar 17 '22 at 07:48
  • The issue is about the native Gradle Plugin, you can post a new question with the Android Studio and Gradle Plugin tag. – Liyun Zhang - MSFT Mar 17 '22 at 08:07
  • In general, the the message you get in Logcat should mean that there was a library that you missed in your project after you set the minifyenabled as true. – Liyun Zhang - MSFT Mar 17 '22 at 08:15
0

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

Stoberdo
  • 11
  • 1
  • 4