1. 程式人生 > 實用技巧 >Link causes xamarin Android binding library project to crash

Link causes xamarin Android binding library project to crash

We all want our app packages to be as small as possible. Quite efficient way of decreasing the size of the app bundle is to useLinker. That's usually easier said than done. In my experience, the main problem with linker configuration is the identification of the problems caused by linker being too aggressive at stripping out the code that is actually necessary to run the app.

I've decided to write this blog post because I've recently ran into one of these issues where linker is doing "too good" job at removing code and therefore wanted to share with you my experiences.

Long story short

I've decided to update one of my Android apps. Unfortunately the process wasn't so smooth. At the first attempt of releasing it, after almost a year of no updates, the app decided to crash shortly after the splash screen showed up on the screen.

App crash

I can't say I was surprised that the app didn't launch successfully, after all I've updated most of the 3rd party libraries that the app relies on, added some new ones and I also decided to switch to the new and shinyd8 dexer & r8 shrinker. I actually feel that this is something quite usual to happen in such cases, especially considering that myLinking

setting was set to the most aggressiveSdk and User Assembliesoption:

d8 dexer, r8 shrinker and linker config

Debugging

There's usually not much to be done in terms of debugging when the app crashes at the launch time, as it's probably not even reaching your code. You usually are only left with device's logs to look into and work out what's wrong from them. On Android you would normally turn toadband it'slogcatcommand, which would print out everything that's happening on the device.

adb logcat

Fortunately there's an easier and a bit more organised way to read through these logs, as they can get very messy when reading them from within the command line. There's aDevice Logwindow in Visual Studio, that allows you to pick your connected device (or emulator) and show that device's logs. What's even more important it works for both Android and iOS.

Once I had the Device Log window in front of me, with the connected to the PC device selected from the left top corner drop-down, I was ready to start diagnosing.

Because there's so much logs coming in from the device, I always try isolate these that are important to me and my debugging session by making sure that the Device Log window is clear (hit the clear button in the toolbar) of any other logs just before I begin.

In my case the logs after launching the app and after the crash happened looked like so:

Device Log window

The important parts of these messages, when looked closer at are

java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.FitWindowsLinearLayout"

and

Error inflating class android.support.v7.widget.FitWindowsLinearLayout

Now, that we've got our suspects, we go hunting.

NOTE: There are cases where the log messages are much clearer about the issue. You can read from them that there was a problem with a specific type or a method. Once I was done with fixing the above problem I ran into this one:

System.MissingMethodException: Default constructor not found for type XamEffects.Droid.TouchEffectPlatform

It clearly states in the the above, that for some reason the constructor or the entire typeXamEffects.Droid.TouchEffectPlatformcannot be found (XamEffectsplugin). Usually it means only one thing, that this code has been stripped out by the linker when it shouldn't, therefore the additional research is not really necessary.

Research

Whenever messages in the logs are not very clear or obvious, I look for some answers using the exact error message or a part of it as a search phrase. I do that with these three mediums:

I was lucky enough to find some hits with Google, which were not exactly solutions to my problem but at least they gave me an idea of the source -linker. Here are some of them:

From the contents of the above links it would have seemed that somehow linker is removingFitWindowsLinearLayoutobject's code. The easiest way to check if we're truly dealing with an issues of stripped out code, is to change currently setLinkingoption toSdk Assemblies Onlyor straight toNoneand then build and run the app again to verify if the issue persists.

Linking options

In my case, changing toSdk and User Assemblieshelped, however there was also a7mbincrease in theapkpackage size, which was not acceptable for me.

The solution

To fix Xamarin.Forms linker related issues one needs to instruct it to keep the code that shouldn't be stripped out. There are multiple ways of achieving this forAndroidandiOS. The best way (IMO) is the cross-platform option with the use ofcustom linker configurationfile.

Linker configuration

The linker configuration file is an XML file that contains definition of which assemblies, types, methods or even fieldsshouldn't belinked out.

The most important thing for this file is to set theBuild Actionin its properties toLinkDescription. Naming of the file is irrelevant, however I would strongly suggest to name itlinker.config, so there's no confusion about its purpose.

Linker configuration file

The emptylinker.configfile would look like this:

<?xml version="1.0" encoding="utf-8"?>
<linker>
    <!-- Place for linker instructions of what shouldn't be stripped out -->
</linker>

The fix

Finally we've got to the point where we will update our existing (or newly created) linker configuration file with instructions that will hopefully fix our problem(s).

We add instructions to thelinker.configfile depending on what we've established from the device logs. If we're lucky and have clear understanding of what's the root cause of our app crash, like in the example above

Default constructor not found for type XamEffects.Droid.TouchEffectPlatform

we go ahead and update the instructions:

<?xml version="1.0" encoding="utf-8"?>
<linker>
    <assembly fullname="XamEffects.Droid">
        <type fullname="*" />
    </assembly>
</linker>

Theassemblytag and it's childtypetag will make sure that all of the types of theXamEffects.Droidlibrary will be left intact when linking away code. You can always be more specific and instead of using the wildcard (*) in thetypetag, you could provide the full name (including namespace) of that particular type (i.e.<type fullname="XamEffects.Droid.TouchEffectPlatform" />). Be aware though, that if there are any other types, methods or fields that will also need to be preserved from linking them out, they will require their own instructions to be added.

In case there's no understanding of which 3rd party library or part of it is being stripped out (i.e. no clear indication from the device logs), I tend to go through all of the app dependencies and try to work out which ones could be potentially the source of the problem. It usually comes down to expanding your head projectReferencesfolder in theSolution Explorerand trying to narrow down the candidates based on their names.

From our earlier discoveries, we're looking for something related toandroid.support.v7.widget.FitWindowsLinearLayout. Unfortunately there's no exact match forandroid.support.v7.widgetnamespace in the names of the referenced libraries.

Application's references in solution explorer

However, we can easily pick a bunch of potential candidates, that most likely hold the namespace of our interests.

At this stage of our investigations we are ready to start updating linker configuration. We could user a brute force approach and literally list all of these candidates as new instructions in the linker configuration files or we could be a bit more sophisticated with the process and have a "peek" at what's actually inside of thesedll'sto verify which would be a match.

Brute force approach

The good thing about this approach is that it will work even when you don't have any clue about what's the cause of the linking issues. You list all of your suspected assemblies (or all of the project dependencies if you have no candidates) as instructions in thelinker.configfile and then eliminate them one by one by removing them or commenting them out. If I go with this approach I tend to be a bit smarter about the elimination process and comment out half of them at a time (i.e.divide and conquer), instead of actually doing it one by one.

We have narrowed down our suspects to 5 libraries, therefore we add them in:

<?xml version="1.0" encoding="utf-8"?>
<linker>
    <assembly fullname="XamEffects.Droid">
        <type fullname="*" />
    </assembly>
    
    <!-- Let's divide and concquer! -->
    <assembly fullname="Xamarin.Android.Support.v7.AppCompat">
        <type fullname="*" />
     </assembly>
    <assembly fullname="Xamarin.Android.Support.v7.CardView">
        <type fullname="*" />
     </assembly>
    <assembly fullname="Xamarin.Android.Support.v7.MediaRouter">
        <type fullname="*" />
     </assembly>
    <assembly fullname="Xamarin.Android.Support.v7.Palette">
        <type fullname="*" />
     </assembly>
    <assembly fullname="Xamarin.Android.Support.v7.RecyclerView">
        <type fullname="*" />
     </assembly>
</linker>

Once you verify that having these in fixes the crashing (more about the process in the next blog post) then you proceed with dividing and conquering. We remove or comment out some of the entries, to narrow down the candidates, then we verify if the app crashes or not and we do so, until we are left with the one and only.

<?xml version="1.0" encoding="utf-8"?>
<linker>
    <assembly fullname="XamEffects.Droid">
        <type fullname="*" />
    </assembly>
    
    <!-- Here's our winner! -->
    <assembly fullname="Xamarin.Android.Support.v7.AppCompat">
        <type fullname="*" />
     </assembly>        
</linker>

NOTE: For those of you who are here because of theandroid.support.v7.widget.FitWindowsLinearLayoutproblem, theXamarin.Android.Support.v7.AppCompatassembly is the one that you need to preserve.

The disadvantage of this this approach is that it's time consuming, especially if you don't have any idea why your app is crashing. Going through the process of removing instructions, rebuilding the project etc. can be quite tiresome.

The peek'y approach

With this approach we're going to be a bit more clever about which assembly instructions we will add to the linker configuration. To determine this we will require a decompiling tool. I'm using for these purposesILSpy(latest version) but you could use any tool of your choice/preference (e.g.dotPeek).

Having the ability to peek inside of thedllin this case is priceless. We proceed from the top to the bottom of the list of our candidates, starting withXamarin.Android.Support.v7.AppCompat.dll. The file itself could be found in theobj\Debug\90\linksrcdirectory of your project, along the side of all the otherdll's- the files will be there provided that you have built your app in theDebugmode beforehand. The result of having a look inside of it clearly indicates that we have our guy - there's namespace and the class name match.

FitWindowsLinearLayout class

This approach, as opposed to the brute force, is making the elimination process much less reliant on luck and guessing, which in turn makes it much more efficient, as it reduces the app crashing verification cycles.

In case we wouldn't find a match in theXamarin.Android.Support.v7.AppCompat.dlllibrary we would proceed further with our examinations with the next dependency on the list and continue the procedure until the match was found.

We could potentially optimise what we currently have in ourlinker.configby being more specific about which exact type we want to preserve. Instead of making sure all of the types (wildcard) from this assembly will stay untouched, we narrow it down just to theFitWindowsLinearLayouttype.

<?xml version="1.0" encoding="utf-8"?>
<linker>
    <assembly fullname="XamEffects.Droid">
        <type fullname="*" />
    </assembly>
    
    <!-- Here's our more specific winner! -->
    <assembly fullname="Xamarin.Android.Support.v7.AppCompat">
        <type fullname="Android.Support.V7.Widget.FitWindowsLinearLayout" />
     </assembly>        
</linker>

DISCLAIMER: I haven't checked if the above configuration actually works and fixes the crash, it's more to make a point that linker configuration can be more flexible.

Summary

Linker related problems are rarely easy to fix and are usually tricky to track down, so please don't be too hard on yourself if it takes some time for you to get it right or you won't succeed with your first few attempts.

Hopefully this little guide will be at least a road sign for you to understand better how to "bite" fixing linker related problems.