android: Xamarin.Android Linker is not working as expected. Animations can be deleted regardless of your ProGuard or excluding configuration
I have an app that is working fine but since I cannot link the libraries (because it causes an abnormal behavior) it becomes significantly bigger (20 MiB more). The main issue happens with one of my own libraries:
https://www.nuget.org/packages/Xamarin-MaterialSearchBar
I “excluded” the package and its dependencies:
Xamarin.AndroidX.AppCompat;Xamarin.AndroidX.ConstraintLayout;Xamarin.AndroidX.RecyclerView;Xamarin.AndroidX.CardView
But the “linking” options behave quite odd because they are not excluded neither in linking all or only linking SDK. The animations got somehow mad and they are working in a “particular” way, the hamburger menu transforms into an arrow when it shouldn’t and vice-versa and if I do click on the X, it just removes the X forever and doesn’t clean. The following image can give you an idea of its abnormal behavior.
This is the expected behavior and normal when I don’t link anything:
The app only works properly when I use Don’t Link. However, I want to understand what I must exclude (or how to do it properly) or change to avoid this issue. At this point, I tried excluding it in the ProGuard file, in the “exclusion” section, etc. but nothing works. This is a relatively old bug that I just “ignored” for a while because I prioritized other topics before.
My library is open-source so you can check it and tell me if I’m doing something wrong or I should add something else:
https://github.com/FANMixco/Xamarin-SearchBar
It’s important to highlight that I wrote this library in C# and Xamarin. This is not the binding of another one.
UPDATE 1:
I modified my library and added:
[Preserve(AllMembers = true)]
and:
[assembly: LinkerSafe]
And the bug persists.
Update 2:
I excluded each of the libraries and the bug persisted in the ProGuard file and nothing worked. I think the linker is messing with the animations here:
- https://github.com/FANMixco/Xamarin-SearchBar/tree/master/tk.supernovaic.MaterialSearchBar/Resources/anim
- https://github.com/FANMixco/Xamarin-SearchBar/tree/master/tk.supernovaic.MaterialSearchBar/Resources/animator
Because I cannot find any other logic.
The code:
-keep class androidx.work.** { *; }
-keep class androidx.concurrent.** { *; }
-keep class androidx.tracing.** { *; }
-keep class androidx.paging.** { *; }
-keep class com.google.android.gms.** { *; }
-keep class androidx.appcompat.widget.** { *; }
-keep class com.google.android.material.** { *; }
-keep class android.animation.ObjectAnimator.** { *; }
-keep class **.R
-keep class **.R$* { public static <fields>; }
-keepclassmembers class tk.supernovaic.MaterialSearchBar.* {
<fields>;
<init>();
<methods>;
}
Update 3:
Someone in a forum believes that the bug is here:
But both linkers ignore these lines when I added them in the ProGuard file:
-keep class android.animation.ObjectAnimator.** { *; }
-keep class android.animation.** { *; }
-keep class **.R$* { public static ; }
Also, I added --linkskip=android.animation --linkskip=tk.supernovaic.MaterialSearchBar without any positive result.
I also tried to copy and paste the animations from my lib to my app, which didn’t work. Plus, this would have been a terrible solution because my lib has considerable downloads (82 from the latest update a couple of days ago and over 3000 in total).
Update 4:
I updated my library and app and added a new folder in the Resources called raw that contains a keep.xml with the following code:
<?xml version="1.0" encoding="UTF-8" ?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@anim/fade_in_left,@anim/fade_in_right,@anim/fade_out,@anim/fade_out_left,@animator/back_to_menu_morph,@animator/back_to_menu_rotation,@animator/menu_to_back_morph,@animator/menu_to_back_rotation" />
And added to my ProGuard file:
-keep class **.R
-keepclassmembers class tk.supernovaic.MaterialSearchBar.* {
;
();
;
}
All this without any positive result. The bug persists.
Any idea why? Or what I am doing wrong?
Thanks for your support.
P.S.:
I can create the package to be published, this is not my problem.
Also, I have tested in both preview and stable versions of Visual Studio including the Windows one and it’s not working. This is the one that I’m running right now:
Visual Studio Community 2019 for Mac Version 8.10.6 (build 10) Installation UUID: 9295cc6a-682b-49ad-8980-ff4b3d4afc2c GTK+ 2.24.23 (Raleigh theme) Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)
Package version: 612000140
Mono Framework MDK Runtime: Mono 6.12.0.140 (2020-02/51d876a041e) (64-bit) Package version: 612000140
Roslyn (Language Service) 3.10.0-4.21269.26+029847714208ebe49668667c60ea5b0a294e0fcb
NuGet Version: 5.9.0.7134
.NET Core SDK SDK: /usr/local/share/dotnet/sdk/5.0.302/Sdks SDK Versions: 5.0.302 5.0.301 5.0.203 5.0.202 5.0.103 5.0.102 5.0.101 5.0.100 3.1.411 3.1.410 3.1.409 3.1.408 3.1.406 3.1.405 3.1.404 3.1.403 3.1.402 2.1.810 2.1.701 MSBuild SDKs: /Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/Sdks
.NET Core Runtime Runtime: /usr/local/share/dotnet/dotnet Runtime Versions: 5.0.8 5.0.7 5.0.6 5.0.5 5.0.3 5.0.2 5.0.1 5.0.0 3.1.17 3.1.16 3.1.15 3.1.14 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.2 2.1.22
.NET Core 3.1 SDK SDK: 3.1.411
Xamarin.Profiler Version: 1.6.12.29 Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler
Updater Version: 11
Apple Developer Tools Xcode 12.5.1 (18212) Build 12E507
Xamarin.Mac Version: 7.14.0.24 (Visual Studio Community) Hash: c4b89cddb Branch: d16-10 Build date: 2021-06-15 22:03:00-0400
Xamarin Designer Version: 16.10.0.119 Hash: 36a2d986f Branch: remotes/origin/d16-10 Build date: 2021-06-02 19:41:34 UTC
Xamarin.Android Version: 11.3.0.4 (Visual Studio Community) Commit: xamarin-android/d16-10/ae14caf Android SDK: /Users/fanmixco/Library/Developer/Xamarin/android-sdk-macosx Supported Android versions: None installed
SDK Tools Version: 26.1.1 SDK Platform Tools Version: 31.0.2 SDK Build Tools Version: 30.0.2
Build Information: Mono: b4a3858 Java.Interop: xamarin/java.interop/d16-10@f39db25 ProGuard: Guardsquare/proguard/v7.0.1@912d149 SQLite: xamarin/sqlite/3.35.4@85460d3 Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-10@c5732a0
Microsoft OpenJDK for Mobile Java SDK: /Users/fanmixco/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25 1.8.0-25 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL
Android SDK Manager Version: 16.10.0.12 Hash: e240b8c Branch: remotes/origin/d16-10 Build date: 2021-06-01 18:26:34 UTC
Android Device Manager Version: 16.10.0.14 Hash: e340248 Branch: remotes/origin/d16-10 Build date: 2021-06-01 18:26:52 UTC
Xamarin.iOS Version: 14.20.0.24 (Visual Studio Community) Hash: c4b89cddb Branch: d16-10 Build date: 2021-06-15 22:03:01-0400
Build Information Release ID: 810060010 Git revision: 263d2fc9398e8092c231fa5d310cd72205cd627c Build date: 2021-07-08 14:37:29-04 Build branch: release-8.10
Operating System Mac OS X 10.16.0 Darwin 20.6.0 Darwin Kernel Version 20.6.0 Wed Jun 23 00:26:31 PDT 2021 root:xnu-7195.141.2~5/RELEASE_X86_64 x86_64
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 17 (9 by maintainers)


I’m going to close this issue as we know what is happening. It is not something we should fix in the linker as we should be linking out unused classes to save on apk size.
If others encounter this kind if issue this is a good read https://docs.microsoft.com/en-us/xamarin/android/deploy-test/linker it outlines the various ways you can stop types getting linked away.
ok. so its not a linker bug, but I understand what is happening in this case.
So the code in the MaterialSearchBar library is relying on the
AnimatedVectorDrawableclass deal with its animations. However there is no code in the library which actually uses it directly. So the type is linked out as part of the SdkOnly/Full linker step. The java side of the code will still exist , but all the glue on the C# side which will react to events being raised from the java side is all gone. So when you access theNavIcon.Drawableproperty you just get aAndroid.Graphics.Drawabletype back which does not supportIAnimatable, because the actual type which the java type is , doesn’t exist on the C# side.The reason why my workaround works is that is stops the type from being linked away. So you can either use the work around I provided or add something like this to the MaterialSearchBar class
as described in https://docs.microsoft.com/en-us/xamarin/android/deploy-test/linker#falseflag.
So its not a bug, its how the linker works. There isn’t anything we can really fix on this side. If the code is not directly using a type, it will be removed. The code above fools the linker into thinking the type is used.