Reputation: 6893
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>
Upvotes: 13
Views: 10907
Reputation: 1370
Other than an issue where my build failed with errors about org.apache.http (which was fixed with https://github.com/xamarin/xamarin-android/issues/2670) I found this configuration worked for me...
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidLinkMode>SdkOnly</AndroidLinkMode>
<AndroidSupportedAbis>armeabi-v7a;x86;arm64-v8a;x86_64</AndroidSupportedAbis>
<AndroidCreatePackagePerAbi>true</AndroidCreatePackagePerAbi>
<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
<!-- Alternative to using Proguard -->
<AndroidDexTool>d8</AndroidDexTool>
<AndroidLinkTool>r8</AndroidLinkTool>
<!-- (A) Recommended AOT settings based on https://forums.xamarin.com/discussion/104165/when-will-be-aot-available-again -->
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk> <!-- See (A) -->
<BundleAssemblies>True</BundleAssemblies> <!-- See (A) -->
<AotAssemblies>True</AotAssemblies> <!-- See (A) -->
<EnableLLVM>True</EnableLLVM> <!-- See (A) -->
<!-- TODO Startup Tracing -->
</PropertyGroup>
This deployed successfully, it's faster and the APK size even after adding AOT is now slightly smaller than my original builds that didn't use ProGuard / AOT.
Note that Xamarin Studio (Community) doesn't enable you to set the AOT / LLVM options for Android, so you must edit the project directly.
Also it's worth having the build / archive windows open as it takes a very long time (especially if you're spitting out a separate ABI per architecture), for a while there I thought Xamarin Studio got stuck.
Upvotes: 0
Reputation: 6893
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;
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.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
- app with multidex+proguard = apk size 26,4mb dex size 3,4mb
- 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.
Upvotes: 13