Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use D8, R8 in Xamarin.Forms apps for Android?

I have just downloaded Vs 2019 pre and it offers more configuration for xamarin android as stated here

It suggests to use as below and it tells that proguard cant be use together with r8 as it is replacement for proguard.

<Project>
    <PropertyGroup>
        <AndroidEnableMultiDex>True</AndroidEnableMultiDex>
        <AndroidDexTool>d8</AndroidDexTool>
        <AndroidLinkTool>r8</AndroidLinkTool>
    </PropertyGroup>
</Project>

but I dont really understand how r8 supposed to work? because using proguard I have done a lot of configurations like defining which library, class, function keep. So we dont need any of those? simply set up r8 and go? I attempted already as suggested but i get error that 8>R8 : error : Compilation can't be completed because some library classes are missing.

Beside that how are those settings effect bundle into native assemblies, AotAssemblies, LLVM etc? can we use together with them. my configuration is as below. But unfortunately it doesnt work. if i remove D8 and R8 it works. I tried using Proguard+D8 only but also doesnt work.

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
    <DebugType>portable</DebugType>
    <AndroidLinkMode>Full</AndroidLinkMode>
    <EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>
    <AndroidCreatePackagePerAbi>true</AndroidCreatePackagePerAbi>
    <JavaMaximumHeapSize>1G</JavaMaximumHeapSize>
    <AotAssemblies>true</AotAssemblies>
    <EnableLLVM>true</EnableLLVM>
    <AndroidAotAdditionalArguments>no-write-symbols,nodebug</AndroidAotAdditionalArguments>
    <DebugSymbols>false</DebugSymbols>
    <BundleAssemblies>true</BundleAssemblies>
    <AndroidEnableMultiDex>True</AndroidEnableMultiDex>
    <EnableProguard>false</EnableProguard>
    <Debugger>Xamarin</Debugger>
    <AndroidSupportedAbis>armeabi-v7a;x86;x86_64</AndroidSupportedAbis>
    <AndroidLinkSkip> </AndroidLinkSkip>
    <AndroidEnableMultipleDex>true</AndroidEnableMultipleDex>
    <AndroidExplicitCrunch>true</AndroidExplicitCrunch>
    <AndroidDexTool>d8</AndroidDexTool>
    <AndroidLinkTool>r8</AndroidLinkTool>
  </PropertyGroup>

UPDATE:

I removed r8 and enabled proguard. because after reading in general (not only xamarin) r8 is not as mature as proguard. so I let d8 only above configuarion and EnableProguard =true. But i get warning and errors regarding r8

8>"\myApp.Droid\myApp.Droid.csproj" (Rebuild;BuiltProjectOutputGroup;BuiltProjectOutputGroupDependencies;DebugSymbolsProjectOutputGroup;DebugSymbolsProjectOutputGroupDependencies;DocumentationProjectOutputGroup;DocumentationProjectOutputGroupDependencies;SatelliteDllsProjectOutputGroup;SatelliteDllsProjectOutputGroupDependencies;SGenFilesOutputGroup;SGenFilesOutputGroupDependencies target) (1) ->
8>(_CompileToDalvikWithD8 target) -> 
8>  R8 : warning : Missing class: com.amazon.device.messaging.ADMMessageReceiver
8>  R8 : warning : Missing class: com.google.android.gms.location.LocationListener
8>  R8 : warning : Missing class: com.amazon.device.messaging.ADMMessageHandlerBase
8>  R8 : warning : Missing class: com.amazon.device.iap.PurchasingListener
8>  R8 : warning : Missing class: org.apache.http.client.methods.HttpEntityEnclosingRequestBase
8>
8>
8>"\myApp.Droid\myApp.Droid.csproj" (Rebuild;BuiltProjectOutputGroup;BuiltProjectOutputGroupDependencies;DebugSymbolsProjectOutputGroup;DebugSymbolsProjectOutputGroupDependencies;DocumentationProjectOutputGroup;DocumentationProjectOutputGroupDependencies;SatelliteDllsProjectOutputGroup;SatelliteDllsProjectOutputGroupDependencies;SGenFilesOutputGroup;SGenFilesOutputGroupDependencies target) (1) ->
8>(_CompileToDalvikWithD8 target) -> 
8>  R8 : error : Compilation can't be completed because some library classes are missing.
8>
8>    45 Warning(s)
8>    1 Error(s)
8>
like image 952
Emil Avatar asked Jan 27 '19 18:01

Emil


People also ask

What is the difference between Xamarin forms and Xamarin Android?

Generally, the main distinction between the two platforms is that Xamarin. Forms allows reusing the same UI code for multiple OS, whereas Xamarin Native is adapted to APIs, specific to a specific platform – Windows, iOS, Android.

How do I deploy Xamarin to Android?

Manual InstallationCopy the file to your Android device's physical memory or an SD card and then run the file from your device. Android, by default, blocks installation of Apps that are not from PlayStore. To install your App, you must enable it to accept the App installation from the Settings.

Can we use Xamarin in Android Studio?

I assume that when you are talking about Android Studio you mean Native Android Development and by Visual Studio you mean Xamarin. If you want to create crossplatform app then Native Android Development is NOT a way to go. Xamarin might work for you, BUT - you'd rather recommend you to go with Flutter.


1 Answers

I would like to give an update here for those who are excited to use D8 and R8 for xamarin forms apps.

Last thing first, it is not ready and has no significant advantage yet. Dont waste your time.

I had spend almost whole day to run my existing app with existing proguard as it was promised that it should work with my existing app. here are some problems I had;

  1. some nuget packages fail with r8 while they work fine with proguard. for example, i had problem with OneSignal library. here is the issue on github. I am sure that some other library will have problem as well.
  2. Proguard optimizations doesn't recognized by r8. this was crazy because, you don't get exact error message but only message as error : java.lang.StringIndexOutOfBoundsException: String index out of range : 4735. You dont even know what it is about although i enabled diagnostic build. I figured it out by remove each line in my proguard and everytime had to rebuild in release. You may imagine how painful it is as each build might take up to 5-10 mins and you have to repeat several times. Finally i found out that i had this line in my proguard -optimizations !field/removal/writeonly,!field/marking/private,!class/merging/*,!code/allocation/variable and r8 doesn't like it.
  3. I was able to rebuild and deploy on my phone without any error at the end and app crashed on start. I didnt fix it because I wanted to see my gain before i attempt to fix something.

Result:

it was said to have apk and dex file size decrease but reality is quite minimal. I use following setting enabled; Aot+lvvm+bundle into native assemblies+ CreatePackagePerAbi+ Full linking used for each 2 tests

  1. app with multidex+proguard = apk size 26,4mb dex size 3,4mb
  2. app with d8+r8+ no multidex+no proguard=apk size 26,2mb dex size 2,7mb

it is definitely exciting and positive to see d8 and r8 support in early stage in Xamarin. we appreciate that but for those who wants to try so quickly like me, it doesnt worth it yet. in the future maybe we can revisit those options but for now multi-dex, proguard seems to be better options.

like image 199
Emil Avatar answered Oct 08 '22 00:10

Emil