[英]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;
这是我遇到的一些问题;
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. -optimizations !field/removal/writeonly,!field/marking/private,!class/merging/*,!code/allocation/variable
and r8 doesn't like it. -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不喜欢它。 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
- app with multidex+proguard = apk size 26,4mb dex size 3,4mb
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
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.