NullPointerExcepetion Facebook sdk v4.5.0 when ena

2019-02-21 11:43发布

I'm trying to login with Facebook using ParseFacebookUtils library and it's working good but when i enabled the Proguard file i have this exception on Facebook login activity startup:

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package.name/com.facebook.FacebookActivity}: java.lang.NullPointerException
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
       at android.app.ActivityThread.access$900(ActivityThread.java:175)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:146)
       at android.app.ActivityThread.main(ActivityThread.java:5602)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by java.lang.NullPointerException
       at java.lang.Enum$1.create(Enum.java:43)
       at java.lang.Enum$1.create(Enum.java:35)
       at libcore.util.BasicLruCache.get(BasicLruCache.java:54)
       at java.lang.Enum.getSharedConstants(Enum.java:209)
       at java.lang.Enum.valueOf(Enum.java:189)
       at com.facebook.login.LoginBehavior.valueOf(LoginBehavior.java:26)
       at com.facebook.login.LoginClient$Request.(LoginClient.java)
       at com.facebook.login.LoginClient$Request.(LoginClient.java)
       at com.facebook.login.LoginClient$Request$1.createFromParcel(LoginClient.java:2521)
       at android.os.Parcel.readParcelable(Parcel.java:2111)
       at android.os.Parcel.readValue(Parcel.java:2020)
       at android.os.Parcel.readArrayMapInternal(Parcel.java:2321)
       at android.os.Bundle.unparcel(Bundle.java:249)
       at android.os.Bundle.getString(Bundle.java:1118)
       at android.content.Intent.getStringExtra(Intent.java:5145)
       at com.google.android.gms.analytics.Tracker$zza.zziJ(Unknown Source:4000)
       at com.google.android.gms.analytics.GoogleAnalytics.isInitialized(Unknown Source)
       at com.google.android.gms.analytics.GoogleAnalytics$zzb.onActivityStarted(Unknown Source)
       at android.app.Application.dispatchActivityStarted(Application.java:219)
       at android.app.Activity.onStart(Activity.java:1087)
       at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:535)
       at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1177)
       at android.app.Activity.performStart(Activity.java:5461)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2386)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
       at android.app.ActivityThread.access$900(ActivityThread.java:175)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:146)
       at android.app.ActivityThread.main(ActivityThread.java:5602)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
       at dalvik.system.NativeStart.main(NativeStart.java)

My Proguard file:

-keep class !android.support.v7.internal.view.menu.**,android.support.v7.** {*;}
# -keep interface android.support.v7.** { *; }
-keep public class android.support.v7.widget.** { *; }
-keep public class android.support.v7.internal.widget.** { *; }

-keep public class * extends android.support.v4.view.ActionProvider {
    public <init>(android.content.Context);
}

# Keep source file names, line numbers, and Parse class/method names for easier debugging
-keepattributes SourceFile,LineNumberTable
-keepnames class com.parse.** { *; }

# Required for Parse
-keepattributes *Annotation*
-keepattributes Signature
-dontwarn android.net.SSLCertificateSocketFactory
-dontwarn android.app.Notification
-dontwarn com.squareup.**
-dontwarn okio.**

# Required for crashlytics
-keep class com.crashlytics.** { *; }
-keep class com.crashlytics.android.**

-dontwarn android.support.v4.**

# Required for eventbus
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
-keepclassmembers class ** {
    public void onEvent*(**);
}

My manifest file:

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />

        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

        <provider android:authorities="com.facebook.app.FacebookContentProviderMyAppID"
            android:name="com.facebook.FacebookContentProvider"
            android:exported="true" />

Login code:

ParseFacebookUtils.logInWithReadPermissionsInBackground(this,
                Collections.singletonList("public_profile"),
                new LogInCallback() {
                    @Override
                    public void done(ParseUser user, ParseException err) {
                        if (user == null) {

                            mErrorMessage = ParseErrorHandler.handleParseError(err);


                            showContent(true);

                        } else if (user.isNew()) {

                            syncData();

                        } else {

                            syncData();

                        }
                    }
                });

Note:App running normally when set minifyEnabled false, So my problem with Proguard File
And i know that "Android Facebook SDK documentation":

You don't have to perform any additional steps to use ProGuard for the Facebook Android SDK.

3条回答
走好不送
2楼-- · 2019-02-21 12:27

I just added this line and it's working:

-keep class com.facebook.** { *; }
查看更多
孤傲高冷的网名
3楼-- · 2019-02-21 12:35

You have to add below lines into your file, That will resolve your problem.

Only need to add below lines.

-dontwarn com.facebook.**
-keep class com.facebook.** { *; }
-keepattributes Signature  
  1. ignore the warning, or don't warn for the package com.facebook
  2. keep the classes & inner classes as well inside that package.
  3. keep the signature of attribute.
查看更多
smile是对你的礼貌
4楼-- · 2019-02-21 12:46

try to add

-keep class com.facebook.* -keep class com.facebook.login.*

in your proguard file

查看更多
登录 后发表回答