本文介绍了如何在Xamarin.Forms适用于Android的应用程序中使用D8,R8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚下载了Vs 2019 pre,它为xamarin android提供了更多配置,如这里

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

建议按以下方式使用,并告知proguard不能与r8一起使用,因为它可以代替proguard.

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>

但是我真的不明白r8应该如何工作?因为使用proguard,我已经做了很多配置,例如定义保留哪个库,类,函数.所以我们不需要这些吗?只需设置r8即可?我已经按照建议尝试,但出现错误8>R8 : error : Compilation can't be completed because some library classes are missing.

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.

此外,这些设置如何将效果捆绑到本机程序集,AotAssemblies,LLVM等中?我们可以和他们一起使用吗?我的配置如下.但不幸的是,它不起作用.如果我删除D8和R8,它的工作原理.我尝试仅使用Proguard + D8,但也无法正常工作.

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>

更新:

我删除了r8并启用了proguard.因为一般阅读后(不仅是xamarin),r8还不如proguard成熟.所以我只让d8高于configuarion和EnableProguard = true.但是我收到有关r8的警告和错误

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>

推荐答案

对于那些为Xamarin表单应用程序使用D8和R8感到兴奋的人,我想在这里进行更新.

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

最后一件事情是,它还没有准备好,也没有明显的优势.不要浪费时间.

我花了整整一天的时间使用现有的proguard运行我的现有应用程序,因为它被保证可以与我现有的应用程序一起使用.这是我遇到的一些问题;

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. 某些nuget软件包在r8上失败,而在proguard上运行良好.例如,我在使用OneSignal库时遇到了问题.这是github上的 issue .我相信其他一些图书馆也会有问题.
  2. r8无法识别
  3. Proguard 优化.这太疯狂了,因为您没有得到确切的错误消息,而只得到error : java.lang.StringIndexOutOfBoundsException: String index out of range : 4735消息.尽管我启用了诊断构建,但您甚至不知道它的含义.我通过删除proguard中的每一行来弄清楚,每次都必须在发行版中进行重建.您可能会想象这是多么痛苦,因为每个构建可能要花费5至10分钟的时间,并且您必须重复几次.最终我发现我的proguard -optimizations !field/removal/writeonly,!field/marking/private,!class/merging/*,!code/allocation/variable中有这行,而r8不喜欢它.
  4. 我最终可以在手机上进行重建和部署而没有任何错误,并且应用程序在启动时崩溃了.我没有修复它,因为我想在尝试修复某些东西之前先了解一下自己的收获.
  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.

结果:

据说,apk和dex文件的大小有所减少,但实际情况却很少.我使用以下启用的设置; Aot+lvvm+bundle into native assemblies+ CreatePackagePerAbi+ Full linking用于每两次测试

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. 具有multidex + proguard = apk大小26,4mb的应用程序dex大小3,4mb的应用程序
  2. d8 + r8 +没有multidex +没有proguard = apk大小为26,2mb dex大小为2.7mb
  3. 的应用程序
  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

在Xamarin的早期阶段看到d8和r8支持绝对令人兴奋和乐观.我们很感激,但是对于那些想要像我一样快尝试的人,这还不值得.将来也许我们可以重新考虑这些选项,但是对于现在的多dex,proguard似乎是更好的选择.

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.

这篇关于如何在Xamarin.Forms适用于Android的应用程序中使用D8,R8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 09:48