使用 proguard 时的 ClassNotFound GoogleApiAvailability

ClassNotFound GoogleApiAvailability when proguard is used

我正在创建一个 android aar,我在其中使用 google 的愿景 API。要检查 Play 服务是否可用,我使用 GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context).

添加了检查

为了将其从混淆中排除,我添加了

-keep class com.google.android.gms.vision.**{*;}
-keep class com.google.android.gms.common.**{*;}

混淆时出现此错误:

07-11 00:36:50.970 26184-26184/com.example.anujakothekar.myapplication 

I/zygote: Rejecting re-init on previously-failed class java.lang.Class<com.example.textscan.scanner.camera.b>: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/vision/Detector$Processor;
07-11 00:36:50.970 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at java.lang.Object java.lang.Class.newInstance() (Class.java:-2)
07-11 00:36:50.970 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at android.app.Activity android.app.Instrumentation.newActivity(java.lang.ClassLoader, java.lang.String, android.content.Intent) (Instrumentation.java:1173)
07-11 00:36:50.970 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2708)
07-11 00:36:50.970 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2892)
07-11 00:36:50.970 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void android.app.ActivityThread.-wrap11(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
07-11 00:36:50.970 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1593)
07-11 00:36:50.970 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:105)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void android.os.Looper.loop() (Looper.java:164)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6540)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void com.android.internal.os.Zygote$MethodAndArgsCaller.run() (Zygote.java:240)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:767)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.vision.Detector$Processor" on path: DexPathList[[zip file "/data/app/com.example.anujakothekar.myapplication-tunZSOorubmtNQccOZwWPw==/base.apk", zip file "/data/app/com.example.anujakothekar.myapplication-tunZSOorubmtNQccOZwWPw==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.anujakothekar.myapplication-tunZSOorubmtNQccOZwWPw==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.anujakothekar.myapplication-tunZSOorubmtNQccOZwWPw==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.anujakothekar.myapplication-tunZSOorubmtNQccOZwWPw==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.anujakothekar.myapplication-tunZSOorubmtNQccOZwWPw==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.anujakothekar.myapplication-tunZSOorubmtNQccOZwWPw==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.anujakothekar.myapplication-tunZSOorubmtNQccOZwWPw==/split_lib_slice_5_apk.a
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:93)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:379)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at java.lang.Object java.lang.Class.newInstance() (Class.java:-2)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at android.app.Activity android.app.Instrumentation.newActivity(java.lang.ClassLoader, java.lang.String, android.content.Intent) (Instrumentation.java:1173)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2708)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2892)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void android.app.ActivityThread.-wrap11(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1593)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:105)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void android.os.Looper.loop() (Looper.java:164)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6540)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void com.android.internal.os.Zygote$MethodAndArgsCaller.run() (Zygote.java:240)
07-11 00:36:50.972 26184-26184/com.example.anujakothekar.myapplication I/zygote:     at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:767)

更新: 当我在也导入了此 aar 的项目中使用 playservices 依赖项时,我的代码运行良好。 有什么办法可以避免在项目中添加 playservices 依赖项,而只是从 aar 中使用它?

指令

-keep class com.google.android.gms.vision.** { *; }

使 ProGuard 保留 com.google.android.gms.vision 中的所有 类 但不保留 com.google.android.gms.vision.Detector$Processor.

等接口

添加第二条规则以保留接口:

-keep interface com.google.android.gms.vision.** { *; }
-keep interface com.google.android.gms.common.** { *; }

应该可以解决问题。