相同的代码在发布模式下不起作用(android 共享菜单)

Same codes do not work in release mode (android share menu)

以下代码在调试模式下完美运行:

menu_post_view.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      tools:context="com.mydream.bgz.activities.PostViewActivity">
    <item
            android:id="@+id/action_share"
            android:title="@string/action_share"
            app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
            app:showAsAction="always"/>
</menu>

PostViewActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_post_view, menu);

    ShareActionProvider mShareActionProvider;
    MenuItem item = menu.findItem(R.id.action_share);
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
    // mShareActionProvider.setOnShareTargetSelectedListener(this);
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(Intent.EXTRA_SUBJECT, str_mPostTitle);
    sharingIntent.putExtra(Intent.EXTRA_TEXT, str_mPostUrl);
    mShareActionProvider.setShareIntent(sharingIntent);

    return super.onCreateOptionsMenu(menu);
}

一切都发生在onCreate()方法中。我用 try-catch 包围了我的所有代码,并得到了以下打印的堆栈跟踪。

06-13 20:36:41.817 689-689/? W/SupportMenuInflater: Cannot instantiate class: android.support.v7.widget.ShareActionProvider
                                                    java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.ShareActionProvider" on path: DexPathList[[zip file "/data/app/com.mydream.bgzaz-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mydream.bgzaz-1/lib/arm64, /vendor/lib64, /system/lib64]]
                                                        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                        at android.support.v7.view.g$b.a(Unknown Source)
                                                        at android.support.v7.view.g$b.b(Unknown Source)
                                                        at android.support.v7.view.g.a(Unknown Source)
                                                        at android.support.v7.view.g.inflate(Unknown Source)
                                                        at com.mydream.bgz.activities.PostViewActivity.onCreateOptionsMenu(Unknown Source)
                                                        at android.app.Activity.onCreatePanelMenu(Activity.java:2881)
                                                        at android.support.v4.b.l.onCreatePanelMenu(Unknown Source)
                                                        at android.support.v7.view.i.onCreatePanelMenu(Unknown Source)
                                                        at android.support.v7.app.f$a.onCreatePanelMenu(Unknown Source)
                                                        at android.support.v7.view.i.onCreatePanelMenu(Unknown Source)
                                                        at android.support.v7.app.o.j(Unknown Source)
                                                        at android.support.v7.app.o.run(Unknown Source)
                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                        at android.os.Looper.loop(Looper.java:148)
                                                        at android.app.ActivityThread.main(ActivityThread.java:5525)
                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                                                        Suppressed: java.lang.ClassNotFoundException: android.support.v7.widget.ShareActionProvider
                                                        at java.lang.Class.classForName(Native Method)
                                                        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                ... 20 more
                                                     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
06-13 20:36:41.822 689-689/? D/AndroidRuntime: Shutting down VM


                                               --------- beginning of crash
06-13 20:36:41.823 689-689/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.mydream.bgzaz, PID: 689
                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.bf.a(android.content.Intent)' on a null object reference
                                                   at com.mydream.bgz.activities.PostViewActivity.onCreateOptionsMenu(Unknown Source)
                                                   at android.app.Activity.onCreatePanelMenu(Activity.java:2881)
                                                   at android.support.v4.b.l.onCreatePanelMenu(Unknown Source)
                                                   at android.support.v7.view.i.onCreatePanelMenu(Unknown Source)
                                                   at android.support.v7.app.f$a.onCreatePanelMenu(Unknown Source)
                                                   at android.support.v7.view.i.onCreatePanelMenu(Unknown Source)
                                                   at android.support.v7.app.o.j(Unknown Source)
                                                   at android.support.v7.app.o.run(Unknown Source)
                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:148)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5525)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
06-13 20:36:41.828 1313-3539/? W/ActivityManager:   Force finishing activity com.mydream.bgzaz/com.mydream.bgz.activities.PostViewActivity

build.gradle

compile files('libs/YouTubeAndroidPlayerApi.jar')
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-vector-drawable:25.3.1'

我从来没有遇到过这样的问题。所以,我不明白为什么一些 normal 代码在发布模式下不起作用,但调试构建版本。请注意,我没有在 onCreate() 中编写任何 与共享操作菜单相关的 代码。再次请帮助我了解为什么会出现此问题以及如何解决?

那是因为您在发布模式下混淆了代码。

将此添加到您的混淆器配置文件中。

-keep class android.support.v7.internal.** { *; }
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.internal.** { *; }
-keep interface android.support.v7.** { *; }

您可以在 proguard 配置中添加它。

-keep class android.support.** { *; }
-dontwarn android.support.**
-dontnote android.support.**

你可以在你的日志中看到这一行 java.lang.NullPointerException: 尝试在空对象引用

上调用虚拟方法'void android.support.v7.widget.bf.a(android.content.Intent)'

因为原码被混淆了,所以我们应该在proguard中设置不混淆这个文件