devanshu_kaushik
devanshu_kaushik

Reputation: 979

ClassNotFoundException for com.facebook.FacebookActivity

I want to integrate Facebook's Android SDK (v4.11) into my Android app and my final apk is compiled via Flash Builder because of flex-sdk dependencies.

Unfortunately, I am getting a ClassNotFoundException for com.facebook.FacebookActivity in my stack-trace when my app tries to create the concerned Activity that initialises Facebook.

I have included the classes.jar in the Native Extension for my Android source code and dependencies. On decompiling the classes.dex file in the final .apk using dexdump from Android SDK build-tools via the following command:

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

I am able to see

Class descriptor  : 'Lcom/facebook/FacebookActivity;'

which indicates that FacebookActivity.class has been packaged and compiled in the .apk.

I also bundled all Facebook-sdk resources along with my project's resources in the res folder in my native extension (this is the first time I had to include third-party resources with my own in a native extension).

My onCreate() code which initialises the Facebook-SDK:

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

My AndroidManisfest.xml entries as per Facebook-documentation:

<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>

Am I missing something here?

EDIT:

I am using version 4.12 now with no change in results.

Also, here is my build.gradle entry: (Although that does not effect flex packaging much, as I have to use ziptree on the facebook sdk JAR).

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
        }
    }

EDIT 2:

Here's my custom gradle task which I am using to package the compiled dependencies and source classes:

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)

Upvotes: 13

Views: 5360

Answers (6)

Dgotlieb
Dgotlieb

Reputation: 1391

One possibility is, you will need to move the following to onresume instead of onCreate() :

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

It solved my problem before.

EDIT

try 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}");
}

And of course do not forget:

  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();
    }
};

Upvotes: 0

Ashish Rawat
Ashish Rawat

Reputation: 5839

It must have exceded the infamous 64k method limit, you need to enable your app for multidex. Here is how you do it. https://developer.android.com/tools/building/multidex.html

Upvotes: 0

Vishal Afre
Vishal Afre

Reputation: 1043

Add this line to your proguard file

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

Upvotes: 1

Chebyr
Chebyr

Reputation: 2181

Probably you are having duplicate Facebook libraries, one path through Facebook SDK jar file in libs folder and the other through the gradle Facebook reprository cache.

You can skip the compile 'com.facebook.android:facebook-android-sdk:[4,5)' altogether and use compile project(':libs:facebook') instead.

Upvotes: 0

Jorgesys
Jorgesys

Reputation: 126563

Add the dependency into your build.gradle file

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

or you can use the .jar, this is the way for exporting:

go to Project > Properties > Java Build Path > Order and Export and click on jar's checkbox.

Upvotes: 3

Chebyr
Chebyr

Reputation: 2181

From the information provided it appears that you have used the package name 'com.facebook' for your custom application. However, this might conflict with the 'com.facebook' package used by the facebook sdk.

Please change your package name and try.

Upvotes: 0

Related Questions