如何在 xamarin 表单中使用 proguard 优化器工具

Posted

技术标签:

【中文标题】如何在 xamarin 表单中使用 proguard 优化器工具【英文标题】:How to use proguard optimizer tool with xamarin forms 【发布时间】:2020-01-29 12:52:23 【问题描述】:

我想减小 .apk 文件的大小,所以我在 android 项目的属性窗口中检查 Enable ProGuard 选项,但出现编译时错误

错误“java.exe”以代码 1 退出。

我尝试了一些类似的东西

磁头大小已经是 1G。 Android 目标版本为 9.0(Pie)。 发布模式下的我的解决方案配置。 使用最新的 ProGuard 文件(6.1.1) 在新的 ProGuard 文件中添加了 proguard-android.txt、proguard-android-optimize.txt 和 proguard-project.txt。

日志:

1>------ Rebuild All started: Project: DirectionApp, Configuration: Release Any CPU ------
1>DirectionApp -> D:\New\West\ReadOut\DirectionApp\DirectionApp\DirectionApp\bin\Release\netstandard2.0\DirectionApp.dll
2>------ Rebuild All started: Project: DirectionApp.Android, Configuration: Release Any CPU ------
2>  DirectionApp.Android -> D:\New\West\ReadOut\DirectionApp\DirectionApp\DirectionApp.Android\bin\Release\DirectionApp.Android.dll
2>  No way to resolve conflict between "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" and "mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e". Choosing "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5614e089" arbitrarily.
2>PROGUARD : warning : md5606e28f7ad4f2edd4892d456667ca4fb.EmdkWrapper: can't find superclass or interface com.symbol.emdk.EMDKManager$EMDKListener
2>PROGUARD : warning : mono.com.symbol.emdk.EMDKManager_EMDKListenerImplementor: can't find superclass or interface com.symbol.emdk.EMDKManager$EMDKListener
2>PROGUARD : warning : mono.com.symbol.emdk.EMDKManager_StatusListenerImplementor: can't find superclass or interface com.symbol.emdk.EMDKManager$StatusListener
2>PROGUARD : warning : mono.com.symbol.emdk.ProfileManager_DataListenerImplementor: can't find superclass or interface com.symbol.emdk.ProfileManager$DataListener
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.BarcodeManager_ScannerConnectionListenerImplementor: can't find superclass or interface com.symbol.emdk.barcode.BarcodeManager$ScannerConnectionListener
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.Scanner_DataListenerImplementor: can't find superclass or interface com.symbol.emdk.barcode.Scanner$DataListener
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.Scanner_StatusListenerImplementor: can't find superclass or interface com.symbol.emdk.barcode.Scanner$StatusListener
2>PROGUARD : warning : mono.com.symbol.emdk.scanandpair.ScanAndPairManager_StatusListenerImplementor: can't find superclass or interface com.symbol.emdk.scanandpair.ScanAndPairManager$StatusListener
2>PROGUARD : warning : mono.com.symbol.emdk.simulscan.SimulScanReader_DataListenerImplementor: can't find superclass or interface com.symbol.emdk.simulscan.SimulScanReader$DataListerner
2>PROGUARD : warning : mono.com.symbol.emdk.simulscan.SimulScanReader_StatusListenerImplementor: can't find superclass or interface com.symbol.emdk.simulscan.SimulScanReader$StatusListerner
2>PROGUARD : warning : md5606e28f7ad4f2edd4892d456667ca4fb.EmdkWrapper: can't find referenced class com.symbol.emdk.EMDKManager$EMDKListener
2>PROGUARD : warning : md5606e28f7ad4f2edd4892d456667ca4fb.EmdkWrapper: can't find referenced class com.symbol.emdk.EMDKManager
2>PROGUARD : warning : md5606e28f7ad4f2edd4892d456667ca4fb.EmdkWrapper: can't find referenced class com.symbol.emdk.EMDKManager
2>PROGUARD : warning : md5606e28f7ad4f2edd4892d456667ca4fb.EmdkWrapper: can't find referenced class com.symbol.emdk.EMDKManager
2>PROGUARD : warning : mono.com.symbol.emdk.EMDKManager_EMDKListenerImplementor: can't find referenced class com.symbol.emdk.EMDKManager$EMDKListener
2>PROGUARD : warning : mono.com.symbol.emdk.EMDKManager_EMDKListenerImplementor: can't find referenced class com.symbol.emdk.EMDKManager
2>PROGUARD : warning : mono.com.symbol.emdk.EMDKManager_EMDKListenerImplementor: can't find referenced class com.symbol.emdk.EMDKManager
2>PROGUARD : warning : mono.com.symbol.emdk.EMDKManager_EMDKListenerImplementor: can't find referenced class com.symbol.emdk.EMDKManager
2>PROGUARD : warning : mono.com.symbol.emdk.EMDKManager_StatusListenerImplementor: can't find referenced class com.symbol.emdk.EMDKManager$StatusListener
2>PROGUARD : warning : mono.com.symbol.emdk.EMDKManager_StatusListenerImplementor: can't find referenced class com.symbol.emdk.EMDKManager$StatusData
2>PROGUARD : warning : mono.com.symbol.emdk.EMDKManager_StatusListenerImplementor: can't find referenced class com.symbol.emdk.EMDKManager
2>PROGUARD : warning : mono.com.symbol.emdk.EMDKManager_StatusListenerImplementor: can't find referenced class com.symbol.emdk.EMDKManager$StatusData
2>PROGUARD : warning : mono.com.symbol.emdk.EMDKManager_StatusListenerImplementor: can't find referenced class com.symbol.emdk.EMDKBase
2>PROGUARD : warning : mono.com.symbol.emdk.EMDKManager_StatusListenerImplementor: can't find referenced class com.symbol.emdk.EMDKManager$StatusData
2>PROGUARD : warning : mono.com.symbol.emdk.EMDKManager_StatusListenerImplementor: can't find referenced class com.symbol.emdk.EMDKBase
2>PROGUARD : warning : mono.com.symbol.emdk.ProfileManager_DataListenerImplementor: can't find referenced class com.symbol.emdk.ProfileManager$DataListener
2>PROGUARD : warning : mono.com.symbol.emdk.ProfileManager_DataListenerImplementor: can't find referenced class com.symbol.emdk.ProfileManager$ResultData
2>PROGUARD : warning : mono.com.symbol.emdk.ProfileManager_DataListenerImplementor: can't find referenced class com.symbol.emdk.ProfileManager
2>PROGUARD : warning : mono.com.symbol.emdk.ProfileManager_DataListenerImplementor: can't find referenced class com.symbol.emdk.ProfileManager$ResultData
2>PROGUARD : warning : mono.com.symbol.emdk.ProfileManager_DataListenerImplementor: can't find referenced class com.symbol.emdk.ProfileManager$ResultData
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.BarcodeManager_ScannerConnectionListenerImplementor: can't find referenced class com.symbol.emdk.barcode.BarcodeManager$ScannerConnectionListener
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.BarcodeManager_ScannerConnectionListenerImplementor: can't find referenced class com.symbol.emdk.barcode.BarcodeManager$ConnectionState
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.BarcodeManager_ScannerConnectionListenerImplementor: can't find referenced class com.symbol.emdk.barcode.BarcodeManager
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.BarcodeManager_ScannerConnectionListenerImplementor: can't find referenced class com.symbol.emdk.barcode.ScannerInfo
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.BarcodeManager_ScannerConnectionListenerImplementor: can't find referenced class com.symbol.emdk.barcode.BarcodeManager$ConnectionState
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.BarcodeManager_ScannerConnectionListenerImplementor: can't find referenced class com.symbol.emdk.barcode.ScannerInfo
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.BarcodeManager_ScannerConnectionListenerImplementor: can't find referenced class com.symbol.emdk.barcode.BarcodeManager$ConnectionState
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.Scanner_DataListenerImplementor: can't find referenced class com.symbol.emdk.barcode.Scanner$DataListener
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.Scanner_DataListenerImplementor: can't find referenced class com.symbol.emdk.barcode.Scanner
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.Scanner_DataListenerImplementor: can't find referenced class com.symbol.emdk.barcode.ScanDataCollection
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.Scanner_DataListenerImplementor: can't find referenced class com.symbol.emdk.barcode.ScanDataCollection
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.Scanner_StatusListenerImplementor: can't find referenced class com.symbol.emdk.barcode.Scanner$StatusListener
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.Scanner_StatusListenerImplementor: can't find referenced class com.symbol.emdk.barcode.Scanner
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.Scanner_StatusListenerImplementor: can't find referenced class com.symbol.emdk.barcode.StatusData
2>PROGUARD : warning : mono.com.symbol.emdk.barcode.Scanner_StatusListenerImplementor: can't find referenced class com.symbol.emdk.barcode.StatusData
2>PROGUARD : warning : mono.com.symbol.emdk.scanandpair.ScanAndPairManager_StatusListenerImplementor: can't find referenced class com.symbol.emdk.scanandpair.ScanAndPairManager$StatusListener
2>PROGUARD : warning : mono.com.symbol.emdk.scanandpair.ScanAndPairManager_StatusListenerImplementor: can't find referenced class com.symbol.emdk.scanandpair.ScanAndPairManager
2>PROGUARD : warning : mono.com.symbol.emdk.scanandpair.ScanAndPairManager_StatusListenerImplementor: can't find referenced class com.symbol.emdk.scanandpair.StatusData
2>PROGUARD : warning : mono.com.symbol.emdk.scanandpair.ScanAndPairManager_StatusListenerImplementor: can't find referenced class com.symbol.emdk.scanandpair.StatusData
2>PROGUARD : warning : mono.com.symbol.emdk.simulscan.SimulScanReader_DataListenerImplementor: can't find referenced class com.symbol.emdk.simulscan.SimulScanReader$DataListerner
2>PROGUARD : warning : mono.com.symbol.emdk.simulscan.SimulScanReader_DataListenerImplementor: can't find referenced class com.symbol.emdk.simulscan.SimulScanReader
2>PROGUARD : warning : mono.com.symbol.emdk.simulscan.SimulScanReader_DataListenerImplementor: can't find referenced class com.symbol.emdk.simulscan.SimulScanData
2>PROGUARD : warning : mono.com.symbol.emdk.simulscan.SimulScanReader_DataListenerImplementor: can't find referenced class com.symbol.emdk.simulscan.SimulScanData
2>PROGUARD : warning : mono.com.symbol.emdk.simulscan.SimulScanReader_StatusListenerImplementor: can't find referenced class com.symbol.emdk.simulscan.SimulScanReader$StatusListerner
2>PROGUARD : warning : mono.com.symbol.emdk.simulscan.SimulScanReader_StatusListenerImplementor: can't find referenced class com.symbol.emdk.simulscan.SimulScanReader
2>PROGUARD : warning : mono.com.symbol.emdk.simulscan.SimulScanReader_StatusListenerImplementor: can't find referenced class com.symbol.emdk.simulscan.SimulScanStatusData
2>PROGUARD : warning : mono.com.symbol.emdk.simulscan.SimulScanReader_StatusListenerImplementor: can't find referenced class com.symbol.emdk.simulscan.SimulScanStatusData
2>PROGUARD : warning : there were 57 unresolved references to classes or interfaces.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(2550,3): error MSB6006: "java.exe" exited with code 1.
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========

我该如何解决这个问题?我想修复以上error 不仅是警告。

【问题讨论】:

关注此 URL 并根据您使用的库自定义规则。 docs.microsoft.com/en-us/xamarin/android/deploy-test/… 他们告诉我要添加新的 proguard 文件,但我很困惑,我是否需要添加 proguard.txt 或 proguard.cfg 文件,对于上述日志,我不确定在自定义 proguard 文件中添加什么。 proguard.cfg 必须创建,您也可以编辑该文件。 我需要先添加 .cs 类文件,然后需要将该文件的扩展名更改为 .cfg。现在那将是空文件。所以我必须根据上面的错误填写这个文件。你能告诉我在那里写什么吗? 您必须编写排除无用类的规则。看起来像这样-dontwarn org.apache.http.** 【参考方案1】:

按照以下步骤进行

首先您需要将 proguard.cfg 添加到您的 android 项目级别。 将 proguard.cfg 文件构建操作更改为 ProguardConfiguration 将使用 Notepad++ 的 proguard.cfg 文件编码更改为 UTF-8

如果您收到警告,则需要使用-dontwarn 属性,如下所示

-dontwarn com.symbol.emdk.**
-dontwarn org.apache.http.**
-dontwarn com.appdynamics.eumagent.runtime.**

您必须需要删除/解决警告,因为这会导致上述错误。上面的代码将删除您的所有警告。 如果您在执行应用程序后遇到异常,您需要使用-keep 属性。如下所示为必修课

-keep public class android.support.v7.widget.**  *; 

【讨论】:

以上是关于如何在 xamarin 表单中使用 proguard 优化器工具的主要内容,如果未能解决你的问题,请参考以下文章

如何在 xamarin 表单中使用 proguard 优化器工具

如何在 xamarin 表单中获得更多输入?

如何在 xamarin 表单中动态创建标签上的点击事件

如何在 Xamarin 表单中的视图模型之间最好地传递信息 - Prism [关闭]

如何在 xamarin 表单中更改搜索栏取消按钮图像

如何在 Xamarin 表单中使用 MVVM 仅为集合视图中的选定框架设置颜色?