com.facebook.FacebookActivity的ClassNotFoundException

时间:2016-05-19 12:00:46

标签: android facebook flex facebook-android-sdk

我想将Facebook的Android SDK(v4.11)集成到我的Android应用程序中,因为flex-sdk依赖,我的最终apk是通过Flash Builder编译的。

不幸的是,当我的应用尝试创建初始化Facebook的相关活动时,我的堆栈跟踪中的ClassNotFoundExceptioncom.facebook.FacebookActivity

我已将native.jar包含在Native扩展中,用于我的Android源代码和依赖项。在使用Android SDK构建工具中的dexdump通过以下命令反编译最终.apk中的classes.dex文件时:

./dexdump classes.dex | grep 'Class descriptor'

我能看到

Class descriptor  : 'Lcom/facebook/FacebookActivity;'

表示已在.apk中打包并编译了FacebookActivity.class

我还将所有Facebook-sdk资源和我的项目资源捆绑在我的原生扩展中的res文件夹中(这是我第一次在原生扩展中包含我自己的第三方资源)

初始化Facebook-SDK的onCreate()代码:

FacebookSdk.sdkInitialize(getApplicationContext()); //throws the ClassNotFoundException
 AppEventsLogger.activateApp(this);

根据Facebook文档我的AndroidManisfest.xml条目:

<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:theme="@android:style/Theme.Translucent.NoTitleBar"
                  android:label="@string/app_name" />
    <activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="@string/fb_login_protocol_scheme" />
            </intent-filter>
        </activity>

我在这里错过了什么吗?

修改

我现在使用的是版本4.12而结果没有变化。

另外,这是我的build.gradle条目:(虽然这不会影响flex包装,因为我必须在facebook sdk JAR上使用ziptree)。

dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile "com.android.support:support-v4:23.0.+"
        compile 'com.facebook.android:facebook-android-sdk:[4,5)'
        /**
         * newer cardview has compatibility issues with android-sdk default styles.
        **/
        compile('com.android.support:cardview-v7:23.2.0') {
            force = true
        }
    }

编辑2:

这是我的自定义gradle任务,我用它来打包已编译的依赖项和源类:

task fatJar(type: Jar) {
        //external libariries - their jars containing compiled classes obtained from the .idea -> libraries -> <libName>.xml file
        from (zipTree("build/intermediates/exploded-aar/com.android.support/support-v4/23.2.0/jars/classes.jar"))
        from (zipTree("build/intermediates/exploded-aar/com.android.support/cardview-v7/23.2.0/jars/classes.jar"))
        from ("build/intermediates/exploded-aar/com.android.support/cardview-v7/23.2.0/res")
        from (zipTree("build/intermediates/exploded-aar/com.android.support/customtabs/23.4.0/jars/classes.jar"))
        from ("build/intermediates/exploded-aar/com.android.support/customtabs/23.4.0/res")
        from (zipTree("build/intermediates/exploded-aar/com.facebook.android/facebook-android-sdk/4.12.1/jars/classes.jar"))
//        from ("build/intermediates/exploded-aar/com.facebook.android/facebook-android-sdk/4.12.0/res")
        from(zipTree("/$USER_HOME/.gradle/caches/modules-2/files-2.1/com.parse.bolts/bolts-android/1.4.0/cc174c559b5177982887bf6e1b76003aebad9516/bolts-android-1.4.0.jar"))
        from(zipTree("/$USER_HOME/.gradle/caches/modules-2/files-2.1/com.parse.bolts/bolts-applinks/1.4.0/8ad21bf21784dacce5f2043afb97218cc377e835/bolts-applinks-1.4.0.jar"))
        from(zipTree("/$USER_HOME/.gradle/caches/modules-2/files-2.1/com.parse.bolts/bolts-tasks/1.4.0/d85884acf6810a3bbbecb587f239005cbc846dc4/bolts-tasks-1.4.0.jar"))
        //soucre code
        from ('build/intermediates/classes/release/') {
            exclude '**/BuildConfig.class'
            exclude '**/R$*.class'
            exclude '**/R.class'
        }
        //jar name and destination directory
        archiveName = "src_and_dependencies.jar"
        destinationDir = file("/$USER_HOME/Ane/build/ane/Android-ARM")
    }

    //before running fatJar task, the old jar should be deleted and the project should be re-built
    fatJar.dependsOn(clearJar, build)

6 个答案:

答案 0 :(得分:0)

根据提供的信息,您似乎已经使用了包裹名称&#39; com.facebook&#39;为您的自定义应用程序但是,这可能会与“com.facebook”相冲突。 facebook sdk使用的包。

请更改您的包裹名称并尝试。

答案 1 :(得分:0)

将依赖项添加到build.gradle文件

dependencies {
    compile 'com.facebook.android:facebook-android-sdk:4.12.0'
}

或者您可以使用.jar,这是导出的方式:

转到Project&gt; Properties&gt; Java Build Path&gt; Order and Export并点击jar的复选框。

答案 2 :(得分:0)

可能你有重复的Facebook库,一条路径通过libs文件夹中的Facebook SDK jar文件,另一条通过gradle Facebook reprository缓存。

您可以完全跳过compile 'com.facebook.android:facebook-android-sdk:[4,5)'并改为使用compile project(':libs:facebook')

答案 3 :(得分:0)

一种可能性是,您需要将以下内容移至onresume而不是onCreate():

FacebookSdk. sdkinitialize     
(getApplicationContext();
Apparentlyogger.activateApp(this);

之前解决了我的问题。

修改

试试这个:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_main);
    callbackManager = CallbackManager.Factory.create();
    loginManager = LoginManager.getInstance();
    List<String> permissionNeeds = Arrays.asList("publish_actions");

}

@Override
protected void onResume() {
    super.onResume();

    AppEventsLogger.activateApp(this,"{your facebook app id}");
}

当然不要忘记:

  private FacebookCallback<Sharer.Result> shareCallback = new FacebookCallback<Sharer.Result>() {
    @Override
    public void onCancel() {
        Log.d("HelloFacebook", "Canceled");
    }

    @Override
    public void onError(FacebookException error) {
        Log.d("HelloFacebook", String.format("Error: %s", error.toString()));
        String title = "error";
        String alertMessage = error.getMessage();
        showResult(title, alertMessage);
    }

    @Override
    public void onSuccess(Sharer.Result result) {
        Log.d("HelloFacebook", "Success!");
        if (result.getPostId() != null) {
            String title ="success";
            String id = result.getPostId();
            String alertMessage ="successfully_posted_post, id";
            showResult(title, alertMessage);
        }
    }

    private void showResult(String title, String alertMessage) {
        new AlertDialog.Builder(MainActivity.this)
                .setTitle(title)
                .setMessage(alertMessage)
                .setPositiveButton("ok", null)
                .show();
    }
};

答案 4 :(得分:0)

将此行添加到您的proguard文件

-keep class com.facebook.FacebookActivity { 
    public *; 
} 

答案 5 :(得分:0)

它必须超出臭名昭着的64k方法限制,您需要为multidex启用您的应用程序。这是你如何做到的。 https://developer.android.com/tools/building/multidex.html