android: App linked using d8 fails at runtime with java.lang.InstantiationException

Steps to Reproduce

I decided to experiment with D8 as a replacement for Proguard. To that end, I added the following two lines to my working debug build:

    <AndroidLinkTool>r8</AndroidLinkTool>
    <AndroidR8ExtraArguments>--lib &quot;C:\Program Files (x86)\Android\android-sdk\platforms\android-28\optional\org.apache.http.legacy.jar&quot;</AndroidR8ExtraArguments>

cf https://github.com/xamarin/xamarin-android/issues/2693 if you’re wondering about the second line.

This is the debug build’s property group:

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>portable</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug</OutputPath>
    <DefineConstants>DEBUG;</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <AndroidLinkMode>SdkOnly</AndroidLinkMode>
    <AotAssemblies>false</AotAssemblies>
    <EnableLLVM>false</EnableLLVM>
    <BundleAssemblies>false</BundleAssemblies>
    <EmbedAssembliesIntoApk>false</EmbedAssembliesIntoApk>
    <JavaMaximumHeapSize>1G</JavaMaximumHeapSize>
    <AndroidSupportedAbis />
    <AndroidUseSharedRuntime>true</AndroidUseSharedRuntime>
    <NoWarn>AvoidAsyncVoid</NoWarn>
    <AndroidEnableMultiDex>true</AndroidEnableMultiDex>
    <EnableProguard>false</EnableProguard>
    <LangVersion>latest</LangVersion>
    <AndroidUseAapt2>true</AndroidUseAapt2>
    <AndroidLinkTool>r8</AndroidLinkTool>
    <AndroidR8ExtraArguments>--lib &quot;C:\Program Files (x86)\Android\android-sdk\platforms\android-28\optional\org.apache.http.legacy.jar&quot;</AndroidR8ExtraArguments>
  </PropertyGroup>

After this, my project compiles, but fails at launch with the following exception:

Time	Device Name	Type	PID	Tag	Message
04-07 18:08:58.350	android_8_1	Error	11764	AndroidRuntime	java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.InstantiationException: java.lang.Class<android.support.v4.content.FileProvider> cannot be instantiated
	at android.app.ActivityThread.installProvider(ActivityThread.java:6242)
	at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805)
	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722)
	at android.app.ActivityThread.-wrap1(Unknown Source:0)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loop(Looper.java:164)
	at android.app.ActivityThread.main(ActivityThread.java:6494)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

I should note the build has one new warning… and not the most useful one.

4>R8 : warning : The rule `-keep public class *extends java.lang.annotation.Annotation {

Expected Behavior

A working app, with r8 as a drop-in replacement for proguard and multidex.

Actual Behavior

App crashes on launch.

Version Information

VS 2019 RTM

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 30 (14 by maintainers)

Most upvoted comments

Followed up with @mfeingol over email, he was able to get a Release build of his app using VS 2019 16.1:

  • <AndroidDexTool>d8</AndroidDexTool>
  • <AndroidLinkTool>r8</AndroidLinkTool>

NOTE: for Debug builds, remove <AndroidLinkTool> and use multidex if needed

The proguard rules were simply:

-keep class android.support.v7.widget.FitWindowsLinearLayout { *; }
-dontwarn android.support.v7.widget.FitWindowsLinearLayout

Using Xamarin.Forms 3.6.0.344457.

Closing for now, I’ll follow up to see if we need to update Xamarin.Forms proguard file. Thanks @mfeingol for the report and diagnosis!

@xamarindevelopervietnam I filed an issue here about the warnings to follow up later: https://github.com/xamarin/xamarin-android/issues/3622

It’s not only related to debugging. You can easily generate such a crash with these lines (on a 64bit device I believe), no matter if a debugger is attached or not:

try { throw new FileLoadException(); }
catch (System.Exception e) when (e is FileLoadException) { }

That bug was introduced with the Mono version that shipped with VS19 16.1 Also happens when a library you are using is rethrowing exceptions internally: https://github.com/xamarin/xamarin-android/issues/3112#issuecomment-495354178

@mfeingol I think I just figured out what your issue may be, can you turn off multidex and try again?

<!--Or just remove this completely-->
<AndroidEnableMultiDex>False</AndroidEnableMultiDex>

Also, see if you can use VS 2019 16.1 that just hit stable.

I think there is an issue the way we are running r8 when you have all of the options on:

  • d8
  • multidex
  • r8

You shouldn’t need multidex anyway, if you have r8 turned on. r8 should make the Java code small enough, where you won’t need multidex.

@jdluzen your issue is just some general rules needed for a Xamarin.Forms app. You would have the same problem using proguard:

Can you add a proguard.cfg file that looks like this? https://github.com/xamarin/Xamarin.Forms/blob/master/.nuspec/proguard.cfg

@mfeingol sent me his .dex files (and other info) over email.

The current problem is a java.lang.InstantiationException at runtime:

Java.Lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: 
    java.lang.InstantiationException: java.lang.Class<android.support.v4.content.FileProvider> cannot be instantiated

Looking at a diff of the dex files (dexdump output), r8 is on the left, proguard on the right:

image

r8 is marking the class abstract???

Still looking into this…