Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

Getting Java.Lang.ClassNotFoundException error at the below line in Xamarin 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)
                another tip, you should always try and compile with proguard and without multidex first, proguard does a pretty good job in reducing the method count, so try without multidex first.
– Pierre
                Apr 1, 2019 at 5:14

If you have proguard enabled, in your proguard.cfg file in your project, add the following:

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

This will keep the mentioned classes when compiling and thus be available in runtime.

When you enable Proguard in your project settings, you need to add a *.cfgfile in your project ie. proguard.cfg or mypg.cfg

Then right-click on the new file -> Select Properties then set:

  • Build Action : ProguardConfiguration
  • Copy to Output Directory : Do not copy
  • Finally add your proguard lines into this file. Each line is a separate setting

    EDIT2

    Add the GMS TextRecognizer part as well and see if it works?

    # 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.**
                    @VINNUSAURUS look at the edit, add the file then the GMS lines - you should be good to go
    – Pierre
                    Apr 1, 2019 at 5:26
                    added to the root of the project and ran the app, but still same error prntscr.com/n5pjs6
    – VINNUSAURUS
                    Apr 1, 2019 at 6:00
                    @VINNUSAURUS Did you set the Build Action to ProguardConfiguration? Disable MultiDex as well
    – Pierre
                    Apr 1, 2019 at 6:19
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.