简体   繁体   English

如何在Android版的Xamarin.Forms应用程序中使用D8,R8?

[英]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 我刚才下载Vs的2019前和它xamarin Android提供了更多的配置说明这里

It suggests to use as below and it tells that proguard cant be use together with r8 as it is replacement for proguard. 它建议使用如下,它告诉proguard不能与r8一起使用,因为它是proguard的替代品。

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

but I dont really understand how r8 supposed to work? 但我真的不明白r8应该如何工作? because using proguard I have done a lot of configurations like defining which library, class, function keep. 因为使用proguard我已经做了很多配置,比如定义哪个库,类,函数保持。 So we dont need any of those? 所以我们不需要任何这些? simply set up r8 and go? 只需设置r8然后去? I attempted already as suggested but i get error that 8>R8 : error : Compilation can't be completed because some library classes are missing. 我已经按照建议进行了尝试,但是我收到错误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? 除此之外,这些设置如何影响捆绑到本机程序集,AotAssemblies,LLVM等? can we use together with them. 我们可以和他们一起使用吗? my configuration is as below. 我的配置如下。 But unfortunately it doesnt work. 但不幸的是它不起作用。 if i remove D8 and R8 it works. 如果我删除D8和R8它的工作原理。 I tried using Proguard+D8 only but also doesnt work. 我尝试过只使用Proguard + D8,但也没用。

<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. 我删除了r8并启用了proguard。 because after reading in general (not only xamarin) r8 is not as mature as proguard. 因为在阅读之后(不仅是xamarin)r8并不像proguard那样成熟。 so I let d8 only above configuarion and EnableProguard =true. 所以我让d8只在configuarion和EnableProguard = true之上。 But i get warning and errors regarding r8 但我得到关于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>

I would like to give an update here for those who are excited to use D8 and R8 for xamarin forms apps. 我想在这里为那些对使用D8和R8 for xamarin表单应用程序感到兴奋的人提供更新。

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. 我花了差不多一整天用现有的proguard运行我现有的应用程序,因为它承诺它应该与我现有的应用程序一起工作。 here are some problems I had; 这是我遇到的一些问题;

  1. some nuget packages fail with r8 while they work fine with proguard. 一些nuget包用r8失败,而它们与proguard一起工作正常。 for example, i had problem with OneSignal library. 例如,我有OneSignal库的问题。 here is the issue on github. 这是github上的问题 I am sure that some other library will have problem as well. 我相信其他一些图书馆也会有问题。
  2. Proguard optimizations doesn't recognized by r8. r8不承认Proguard 优化 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 . 这很疯狂,因为你没有得到确切的错误消息,只有消息为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. 我通过删除我的proguard中的每一行来计算它,并且每次都必须在发布时重建。 You may imagine how painful it is as each build might take up to 5-10 mins and you have to repeat several times. 你可以想象它是多么痛苦,因为每次构建可能需要5-10分钟,你必须重复几次。 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. 最后我发现我在我的proguard -optimizations !field/removal/writeonly,!field/marking/private,!class/merging/*,!code/allocation/variable有这一行-optimizations !field/removal/writeonly,!field/marking/private,!class/merging/*,!code/allocation/variable和r8不喜欢它。
  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. 据说有apk和dex文件大小减少,但现实是非常小的。 I use following setting enabled; 我使用了以下设置; Aot+lvvm+bundle into native assemblies+ CreatePackagePerAbi+ Full linking used for each 2 tests Aot+lvvm+bundle into native assemblies+ CreatePackagePerAbi+ Full linking用于每两个测试的Aot+lvvm+bundle into native assemblies+ CreatePackagePerAbi+ Full linking

  1. app with multidex+proguard = apk size 26,4mb dex size 3,4mb 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 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. 在Xamarin的早期阶段看到d8和r8支持是绝对令人兴奋和积极的。 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. 在未来也许我们可以重新审视这些选项,但对于现在的多dex,proguard似乎是更好的选择。

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... 除了我的构建失败与org.apache.http(已通过https://github.com/xamarin/xamarin-android/issues/2670修复)的问题之外,我发现这个配置对我有用...

 <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. 这部署成功,它更快,即使添加AOT后APK大小现在略小于我没有使用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. 请注意,Xamarin Studio(社区)不允许您为Android设置AOT / LLVM选项,因此您必须直接编辑项目。

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. 同样值得让构建/存档窗口打开,因为它需要很长时间(特别是如果你在每个架构上吐出一个单独的ABI),有一段时间我认为Xamarin Studio卡住了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM