2

我想让我的应用程序更小。起初,我是用 proguard 来解决这个问题的,然后我今天找到了 R8。我无法使用 proguard 缩小我的应用程序。R8也不行。

我将 R8 与Visual Studio Professional 2019一起使用。编译成功并出现一些警告,但是在我使用 R8 在发布模式下编译我的应用程序后,我的应用程序崩溃了。与proguard相同。警告如下所述。在调试模式或发布模式下编译时它工作正常而不收缩。

这是我在手机上运行应用程序时获得的设备日志。它发生在应用程序显示其飞溅期间。

12-02 07:56:06.459: E/AndroidRuntime(13285): FATAL EXCEPTION: main
12-02 07:56:06.459: E/AndroidRuntime(13285): Process: com.myproject.myproject, PID: 13285
12-02 07:56:06.459: E/AndroidRuntime(13285): android.runtime.JavaProxyThrowable: System.TypeInitializationException: The type initializer for 'Registry' threw an exception. ---> System.TypeInitializationException: The type initializer for 'DryIoc.WrappersSupport' threw an exception. ---> System.TypeInitializationException: The type initializer for 'DryIoc.ReflectionTools' threw an exception. ---> DryIoc.ContainerException: Undefined Method '"GetDefault"' in Type DryIoc.ReflectionTools (including non-public=True)
12-02 07:56:06.459: E/AndroidRuntime(13285):   at DryIoc.Throw.ThrowIfNull[T] (T arg, System.Int32 error, System.Object arg0, System.Object arg1, System.Object arg2, System.Object arg3) [0x0002b] in <b786dc28ccda4f9cada791c5ec73aee8>:0 
12-02 07:56:06.459: E/AndroidRuntime(13285):   at DryIoc.ReflectionTools.SingleMethod (System.Type type, System.String name, System.Boolean includeNonPublic) [0x00008] in <b786dc28ccda4f9cada791c5ec73aee8>:0 
12-02 07:56:06.459: E/AndroidRuntime(13285):   at DryIoc.ReflectionTools..cctor () [0x00000] in <b786dc28ccda4f9cada791c5ec73aee8>:0 
12-02 07:56:06.459: E/AndroidRuntime(13285):    --- End of inner exception stack trace ---
12-02 07:56:06.459: E/AndroidRuntime(13285):   at DryIoc.WrappersSupport..cctor () [0x000e6] in <b786dc28ccda4f9cada791c5ec73aee8>:0 
12-02 07:56:06.459: E/AndroidRuntime(13285):    --- End of inner exception stack trace ---
12-02 07:56:06.459: E/AndroidRuntime(13285):    --- End of inner exception stack trace ---
12-02 07:56:06.459: E/AndroidRuntime(13285):   at Prism.DryIoc.PrismApplication.CreateContainerExtension () [0x00006] in <54b3690e7a884e5392d6564fe1058f17>:0 
12-02 07:56:06.459: E/AndroidRuntime(13285):   at Prism.PrismApplicationBase.Initialize () [0x00000] in <69c22ecea5f44afaad9314eb228e8fb3>:0 
12-02 07:56:06.459: E/AndroidRuntime(13285):   at Prism.PrismApplicationBase.InitializeInternal () [0x00006] in <69c22ecea5f44afaad9314eb228e8fb3>:0 
12-02 07:56:06.459: E/AndroidRuntime(13285):   at Prism.PrismApplicationBase..ctor (Prism.IPlatformInitializer platformInitializer, System.Boolean setFormsDependencyResolver) [0x00038] in <69c22ecea5f44afaad9314eb228e8fb3>:0 
12-02 07:56:06.459: E/AndroidRuntime(13285):   at Prism.PrismApplicationBase..ctor (Prism.IPlatformInitializer platformInitializer) [0x00000] in <69c22ecea5f44afaad9314eb228e8fb3>:0 
12-02 07:56:06.459: E/AndroidRuntime(13285):   at Prism.DryIoc.PrismApplication..ctor (Prism.IPlatformInitializer platformInitializer) [0x00000] in <54b3690e7a884e5392d6564fe1058f17>:0 
12-02 07:56:06.459: E/AndroidRuntime(13285):   at myproject.App..ctor (Prism.IPlatformInitializer initializer) [0x00000] in <530a18ff74e7452ca04e0d812ab2f3ef>:0 
12-02 07:56:06.459: E/AndroidRuntime(13285):   at myproject.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x0008c] in <c1eac816320349f19a935c6995da137f>:0 
12-02 07:56:06.459: E/AndroidRuntime(13285):   at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x00011] in <f1027f4df0db4d02bec2ca8d90067419>:0 
12-02 07:56:06.459: E/AndroidRuntime(13285):   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.10(intptr,intptr,intptr)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at md5bd5429e46ce9bb42cba5f37d4b72f961.MainActivity.n_onCreate(Native Method)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at md5bd5429e46ce9bb42cba5f37d4b72f961.MainActivity.onCreate(Unknown Source:0)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at android.app.Activity.performCreate(Activity.java:6986)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1232)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2863)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at android.app.ActivityThread.-wrap11(Unknown Source:0)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at android.os.Handler.dispatchMessage(Handler.java:105)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at android.os.Looper.loop(Looper.java:180)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at android.app.ActivityThread.main(ActivityThread.java:6950)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at java.lang.reflect.Method.invoke(Native Method)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
12-02 07:56:06.459: E/AndroidRuntime(13285):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:835)
12-02 07:56:06.464: E/ActivityManager(1722): App crashed! Process: com.myproject.myproject
12-02 07:56:07.026: E/[B+]AppInfoService(4722): null
12-02 07:56:07.026: E/[B+]AppInfoService(4722): android.os.DeadObjectException
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at android.os.BinderProxy.transactNative(Native Method)
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at android.os.BinderProxy.transact(Binder.java:756)
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at com.htc.pitroad.appminer.services.b$a$a.a(SourceFile:122)
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at com.htc.pitroad.appminer.services.AppInfoService.a(SourceFile:852)
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at com.htc.pitroad.appminer.b.a.a(SourceFile:119)
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at com.htc.pitroad.appminer.e.o.a(SourceFile:154)
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at com.htc.pitroad.appminer.services.AppInfoService.a(SourceFile:1747)
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at com.htc.pitroad.appminer.e.p$a$1.invoke(SourceFile:77)
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at java.lang.reflect.Proxy.invoke(Proxy.java:913)
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at $Proxy0.onResult(Unknown Source)
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at android.os.RemoteCallback.sendResult(RemoteCallback.java:73)
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at android.os.RemoteCallback$2.sendResult(RemoteCallback.java:50)
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at android.os.IRemoteCallback$Stub.onTransact(IRemoteCallback.java:56)
12-02 07:56:07.026: E/[B+]AppInfoService(4722):     at android.os.Binder.execTransact(Binder.java:682)

在第三行可以看到 “DryIoc.ContainerException: Undefined Method '"GetDefault"' in Type DryIoc.ReflectionTools (包括 non-public=True)”。 我假设 R8 删除了 DryIoc 中的 GetDefault 方法,因为我可以在 DryIoc 代码的第 12,052 行看到 GetDefault 方法。 https://github.com/dadhi/DryIoc/blob/master/src/DryIoc/Container.cs

然后我在 proguard.cfg 中写了“keep”,如下所示。但它似乎不起作用。错误是一样的,应用程序的大小也是一样的。

-keep class dryioc.** { *; }

我不确定它是否相关,但是当我使用 R8 编译此应用程序时收到了一些警告。它说没有办法解决两个 mscrolib 版本之间的冲突。到目前为止,我也找不到解决这些警告的方法。

1>  myproject.Android -> C:\Users\koki\source\repos\myproject\myproject\myproject\myproject\myproject.Android\bin\Release\myproject.Android.dll
1>  "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" と "mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e" の間の競合を解決する方法がありません。一時的に、"mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" を選択します。
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Xamarin\Android\Xamarin.Android.D8.targets(81,5): warning XA4306: R8 does not support `MultiDexMainDexList` files when android:minSdkVersion >= 21
1>R8 : warning : The rule `-keep public class * extends androidx.versionedparcelable.VersionedParcelable {
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MSFTSIG.SF' already exists.
1>R8 : warning : Resource 'META-INF/MSFTSIG.RSA' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MSFTSIG.SF' already exists.
1>R8 : warning : Resource 'META-INF/MSFTSIG.RSA' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.
1>R8 : warning : Resource 'META-INF/MANIFEST.MF' already exists.

这是当前Android项目下的proguard.cfg。

-keep class android.support.v7.widget.** { *; }
-keep class android.support.v7.widget.FitWindowsLinearLayout { *; }

-keep class com.google.common.** { *; }
-dontwarn com.google.common.**

-keep class io.grpc.** { *; }
-dontwarn io.grpc.**

-keep class io.opencensus.trace.** { *; }
-dontwarn io.opencensus.trace.**

-keep class DryIoc.** { *; }

-keep class DryIoc.** {
    native <methods>;
}

-keepclassmembers class ** {
   public static *** GetDefault(***);
}

关于 DryIoc 的最后三个规则似乎不起作用。它不会改变归档结果的大小。

谢谢!

4

1 回答 1

1

最后,我解决了这个问题。

我所做的是,将链接选项从Sdk 和用户程序集更改为在 Android 项目上的 Sdk 程序集。

我假设Sdk and User Assemblies比Sdk Assemblies Only保留更多的代码,但显然不是。

无论如何,感谢您的帮助!

于 2019-12-06T08:15:49.010 回答