xamarin - 如何在适用于 Android 的 Xamarin.Forms 应用程序中使用 D8、R8?

标签 xamarin xamarin.forms xamarin.android

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

它建议使用如下,它告诉 proguard 不能与 r8 一起使用,因为它是 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.
除此之外,这些设置如何将效果捆绑到 native 程序集、AotAssemblies、LLVM 等中?我们可以和他们一起使用吗?我的配置如下。但不幸的是它不起作用。如果我删除 D8 和 R8 它工作。我尝试仅使用 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>

更新:

我删除了 r8 并启用了 proguard。因为在阅读一般(不仅是 xamarin)之后,r8 不如 proguard 成熟。所以我只让 d8 高于 configuarion 和 EnableProguard = true。但我收到有关 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>

最佳答案

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

最后一件事,它还没有准备好,还没有显着的优势。不要浪费你的时间。

我几乎花了一整天的时间使用现有的 proguard 运行我现有的应用程序,因为它 promise 它应该可以与我现有的应用程序一起使用。这是我遇到的一些问题;

  • 一些 nuget 包在 r8 下失败,而它们与 proguard 一起工作正常。例如,我在使用 OneSignal 库时遇到了问题。这是issue在github上。我相信其他一些图书馆也会有问题。
  • Proguard optimizations r8 无法识别。这太疯狂了,因为您没有收到确切的错误消息,而只能收到 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 不喜欢它。
  • 我能够在我的手机上重建和部署,最后没有任何错误,应用程序在启动时崩溃。我没有修复它,因为我想在尝试修复某些东西之前看到我的收获。

  • 结果:

    据说 apk 和 dex 文件大小减小,但实际情况非常小。
    我使用以下设置启用;
    Aot+lvvm+bundle into native assemblies+ CreatePackagePerAbi+ Full linking用于每 2 次测试

    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 支持绝对令人兴奋和积极。我们很感激,但对于像我这样想尽快尝试的人来说,这还不值得。将来也许我们可以重新审视这些选项,但就目前而言,multi-dex、proguard 似乎是更好的选择。

    关于xamarin - 如何在适用于 Android 的 Xamarin.Forms 应用程序中使用 D8、R8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54391422/

    相关文章:

    mysql - 如何在没有 Web 服务的情况下将 Xamarin.Forms 应用程序连接到 mysql 数据库

    c# - "Repair"VS 2019 中的 Android SDK 管理器失败

    xamarin.android - 工具栏后退单击在 Xamarin android 中不起作用

    c# - 如何在 Xamarin for Android 中压缩文件?

    xamarin.ios - 在绑定(bind)项目中找不到 BaseTypeAttribute

    azure - 如何使用 azure 移动服务清除本地数据库?

    xamarin.forms - 如何在Xamarin表单中绘制平滑曲线(签名)?

    c# - System.TypeInitializationException : The type initializer for 'SQLite.SQLiteConnection' threw an exception

    java - 从路径到列表集合的图像到 ImageView ?

    xaml - 如何在 Xamarin 表单上使用 Microsoft XAML 边框