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 only. 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 the abnormal behavior.
This is the expected and normal behavior 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 guard file, in the “exclusion” section, etc. but nothing works.
In my guard file, I have these combinations:
-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.** { *; }
And I also tried to add:
-keep class tk.supernovaic.MaterialSearchBar.** { *; }
But the results were exactly the same. I don't know what I can exclude being honest. Any idea?
P.S.:
I can create the package to be published that is not my problem.
My library is open-source if you want to give me any advice on how to change this logic and fix this problem:
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.
In my opinion, the issue happens in this section:
public void OnClick(View v)
{
int id = v.Id;
if (id == Id)
{
if (!IsSearchEnabled)
{
EnableSearch();
}
}
else if (id == Resource.Id.mt_arrow)
{
DisableSearch();
}
else if (id == Resource.Id.mt_search)
{
if (ListenerExists())
{
OnSearchActionListener.OnButtonClicked(BUTTON_SPEECH);
}
}
else if (id == Resource.Id.mt_clear)
{
SearchEdit.Text = "";
}
else if (id == Resource.Id.mt_menu)
{
PopupMenu.Show();
}
else if (id == Resource.Id.mt_nav)
{
int button = IsSearchEnabled ? BUTTON_BACK : BUTTON_NAVIGATION;
if (IsSearchEnabled)
{
DisableSearch();
}
if (ListenerExists())
{
OnSearchActionListener.OnButtonClicked(button);
}
}
}
Update 1:
I have added to my library:
[Preserve(AllMembers = true)]
[assembly: LinkerSafe]
And this to my project:
--linkskip=tk.supernovaic.MaterialSearchBar
And the bug persists; therefore, it should be related to the animations library or something like that.
Update 2:
I have tried also:
-keep class android.animation.ObjectAnimator.** { *; } \-keep class **.R$* { public static <fields>; }
and:
-keep class android.animation.** { *; }
Both still fails but it seems it´s related to the animations:
https://github.com/FANMixco/Xamarin-SearchBar/blob/master/tk.supernovaic.MaterialSearchBar/Resources/animator/menu_to_back_rotation.xml
Also, I added --linkskip=android.animation --linkskip=tk.supernovaicMaterialSearchBar
without any result.
Update 3:
I copied the files from my lib to my project and kept all previous rules without any positive result.
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:
-keep class **.R
-keepclassmembers class tk.supernovaic.MaterialSearchBar.* {
<fields>;
<init>();
<methods>;
}
All this without any positive result. The bug persists.
Update 5:
I have raised a ticket to Microsoft because I'm almost convinced this is a bug related to the linker itself:
https://developercommunity.visualstudio.com/t/xamarinandroid-linking-libs-not-working-as-expecte/1482147?from=email
My conclusions are based on these three points:
- I added the original files (animations) to the project.
- I added a rule to keep the XMLs in the project and library.
- I added rules to preserve the R classes and everything (resources) in the ProGuard file.
It makes no sense that with the previous 3 points the animations are not preserved, in my opinion. If you have any other thoughts feel free to share them, I'm open to listening to them.
Update 6:
I also opened a ticket in GitHub because I believe it's a bug:
https://github.com/xamarin/xamarin-android/issues/6156