Java.Lang.ClassNotFoundException:未找到 class "com.google.android.gms.vision.text.TextRecognizer$Builder" Xamarin Android

Java.Lang.ClassNotFoundException: Didn't find class "com.google.android.gms.vision.text.TextRecognizer$Builder" Xamarin Android

在 Xamarin 的下面一行出现 Java.Lang.ClassNotFoundException 错误 Android

TextRecognizer textRecognizer = new TextRecognizer.Builder(ApplicationContext).Build();

{Java.Lang.ClassNotFoundException: Didn't find class "com.google.android.gms.vision.text.TextRecognizer$Builder" on path: DexPathList[[zip file "/data/app/dine.dine-4Lcxwvl8j7kQmp8qGBPDCA==/base.apk"],nativeLibraryDirectories=[/data/app/dine.dine-4Lcxwvl8j7kQmp8qGBPDCA==/lib/x86, /data/app/dine.dine-4Lcxwvl8j7kQmp8qGBPDCA==/base.apk!/lib/x86, /system/lib]]
      at Java.Interop.JniEnvironment+Types.FindClass (System.String classname) [0x00114] in <286213b9e14c442ba8d8d94cc9dbec8e>:0 
      at Java.Interop.JniType..ctor (System.String classname) [0x00006] in <286213b9e14c442ba8d8d94cc9dbec8e>:0 
      at Java.Interop.JniType.GetCachedJniType (Java.Interop.JniType& cachedType, System.String classname) [0x00018] in <286213b9e14c442ba8d8d94cc9dbec8e>:0 
      at Java.Interop.JniPeerMembers.get_JniPeerType () [0x0000c] in <286213b9e14c442ba8d8d94cc9dbec8e>:0 
      at Java.Interop.JniPeerMembers+JniInstanceMethods.get_JniPeerType () [0x0000a] in <286213b9e14c442ba8d8d94cc9dbec8e>:0 
      at Java.Interop.JniPeerMembers+JniInstanceMethods.StartCreateInstance (System.String constructorSignature, System.Type declaringType, Java.Interop.JniArgumentValue* parameters) [0x0003f] in <286213b9e14c442ba8d8d94cc9dbec8e>:0 
      at Android.Gms.Vision.Texts.TextRecognizer+Builder..ctor (Android.Content.Context context) [0x0005b] in <3d29e02cc4534fbf8547810969e46514>:0 
      at dine.MainActivity.Ocr (Android.Graphics.Bitmap bitmap) [0x00001] in D:\dine\dine\MainActivity.cs:753 
      at dine.MainActivity.StartCapture () [0x001ad] in D:\dine\dine\MainActivity.cs:711 
      --- End of managed Java.Lang.ClassNotFoundException stack trace ---
    java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.vision.text.TextRecognizer$Builder" on path: DexPathList[[zip file "/data/app/dine.dine-4Lcxwvl8j7kQmp8qGBPDCA==/base.apk"],nativeLibraryDirectories=[/data/app/dine.dine-4Lcxwvl8j7kQmp8qGBPDCA==/lib/x86, /data/app/dine.dine-4Lcxwvl8j7kQmp8qGBPDCA==/base.apk!/lib/x86, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at mono.java.lang.RunnableImplementor.n_run(Native Method)
        at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    }

如果您启用了混淆器,请在项目的 proguard.cfg 文件中添加以下内容:

# Google GMS
-keep public class com.google.android.gms.* { public *; }
-keep class com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver
-dontwarn com.google.android.gms.**

这将在编译时保留提到的 类,因此在运行时可用。

编辑

当您在项目设置中启用 Proguard 时,您需要在项目中添加一个 *.cfg 文件,即。 proguard.cfgmypg.cfg

然后在新建文件上右击->Select Properties然后设置:

  • Build Action : ProguardConfiguration
  • Copy to Output Directory : Do not copy

最后将你的 proguard 行添加到这个文件中。每行都是一个单独的设置

EDIT2

也添加 GMS TextRecognizer 部分,看看它是否有效?

# Google GMS
-keep public class com.google.android.gms.* { public *; }
-keep class com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver
-dontwarn com.google.android.gms.**

# GMS TextRecognizer
-keep public class com.google.android.gms.vision.text.TextRecognizer { public *; }
-keep public class com.google.android.gms.vision.text.TextRecognizer.** { public *; }
-keep class com.google.android.gms.vision.text.TextRecognizer
-keep class com.google.android.gms.vision.text.TextRecognizer.**

问题是我使用的是导致问题的预览版包,我将预览版包更改为稳定版,即 Xamarin.GooglePlayServices.Vision 71.1502.0-preview1Xamarin.GooglePlayServices.Vision 60.1142.1 , 现在没有例外了。