My android app Crash when trying to load a facebook Native Ad (java.lang.NoSuchMethodError: No static method isChildDirected())

Viewed 1582

My application crashes when trying to load a facebook Native Ad, here is the log Error :

2020-11-14 12:31:15.968 5710-5710/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.demopackage.name, PID: 5710
    java.lang.NoSuchMethodError: No static method isChildDirected()Z in class Lcom/facebook/ads/AdSettings; or its super classes (declaration of 'com.facebook.ads.AdSettings' appears in /data/app/com.demopackage.name-meNj0dpRfln7Tuz2WwJzdQ==/base.apk)
        at com.facebook.ads.redexgen.X.1l.A01(:3647)
        at com.facebook.ads.redexgen.X.S9.A0B(:53051)
        at com.facebook.ads.redexgen.X.S9.A0Q(:53123)
        at com.facebook.ads.redexgen.X.S9.A0R(:53125)
        at com.facebook.ads.redexgen.X.cR.A1M(:74774)
        at com.facebook.ads.redexgen.X.Jy.loadAd(:41976)
        at com.facebook.ads.redexgen.X.cR.loadAd(:74897)
        at com.facebook.ads.NativeAdBase.loadAd(NativeAdBase.java:242)
        at com.demopackage.name.activities.MainActivity.loadNativeAd(MainActivity.java:216)
        at com.demopackage.name.activities.MainActivity.onCreate(MainActivity.java:157)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Im using the latest version of Facebook Audience Network SDK 'com.facebook.android:audience-network-sdk:6.2.0' and also tried the 'com.facebook.android:audience-network-sdk:6.0.0'

here is the implementation that used on many other app and its working but not for this app:

        nativeAd = new NativeAd(this, getString(R.string.fb_native));

        NativeAdListener nativeAdListener = new NativeAdListener() {
            @Override
            public void onMediaDownloaded(Ad ad) {
                // Native ad finished downloading all assets
                Log.e(TAG, "Native ad finished downloading all assets.");
            }

            @Override
            public void onError(Ad ad, AdError adError) {
                // Native ad failed to load
                Log.e(TAG, "Native ad failed to load: " + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {

                // Render the Native Ad Template
                View adView = NativeAdView.render(MainActivity.this, nativeAd);
                LinearLayout nativeAdContainer = (LinearLayout) findViewById(R.id.native_ad_container);
                // Add the Native Ad View to your ad container.
                // The recommended dimensions for the ad container are:
                // Width: 280dp - 500dp
                // Height: 250dp - 500dp
                // The template, however, will adapt to the supplied dimensions.
                nativeAdContainer.addView(adView, new ViewGroup.LayoutParams(MATCH_PARENT, 800));
                // Native ad is loaded and ready to be displayed
                Log.d(TAG, "Native ad is loaded and ready to be displayed!");
            }

            @Override
            public void onAdClicked(Ad ad) {
                // Native ad clicked
                Log.d(TAG, "Native ad clicked!");
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                // Native ad impression
                Log.d(TAG, "Native ad impression logged!");
            }
        };

        // Request an ad
        nativeAd.loadAd(
                nativeAd.buildLoadAdConfig()
                        .withAdListener(nativeAdListener)
                        .withMediaCacheFlag(NativeAdBase.MediaCacheFlag.ALL)
                        .build());
   

the app is working fine when not calling the loadAd method.

and here is the dependencies that i'm using on my app:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test:runner:1.3.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.vectordrawable:vectordrawable-animated:1.1.0'
    implementation 'androidx.exifinterface:exifinterface:1.3.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.vectordrawable:vectordrawable-animated:1.1.0'
    implementation 'androidx.browser:browser:1.2.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.annotation:annotation:1.1.0'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation "androidx.work:work-runtime:2.4.0"
    implementation 'androidx.appcompat:appcompat:1.2.0'


    implementation 'com.hold1:keyboardheightprovider:0.0.9'
    implementation 'org.wysaid:gpuimage-plus:2.6.3'
    implementation 'com.karumi:dexter:6.2.1'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.steelkiwi:cropiwa:1.0.3'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
    implementation 'com.airbnb.android:lottie:3.4.1'
    implementation 'com.github.pavlospt:circleview:1.3'
    implementation 'de.hdodenhof:circleimageview:3.1.0'
    implementation 'com.github.siyamed:android-shape-imageview:0.9.+@aar'
    implementation 'com.cepheuen.elegant-number-button:lib:1.0.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.19'
    implementation 'com.makeramen:roundedimageview:2.3.0'
    implementation 'com.agrawalsuneet.androidlibs:dotsloader:1.4'
    implementation 'com.isseiaoki:simplecropview:1.1.8'

    implementation 'com.google.android.gms:play-services-ads:19.5.0'
    implementation 'androidx.annotation:annotation:1.1.0'
    implementation 'com.facebook.android:audience-network-sdk:6.2.0'


//    implementation 'com.unity3d.ads:unity-ads:3.4.6'

}
2 Answers

I think the problem is related to inconsistency between class files and audience_network.dex file in the sdk. Somehow we faced the exactly same problem and solved the issue by adding facebook sdk as aar file to the gradle file. You can download the audience network sdk from the following maven repository and try to add it directly.

Good luck.

https://mvnrepository.com/artifact/com.facebook.android/audience-network-sdk/6.2.0

It has been a little late but the problem may be you have and interfering dex file in your assets folder.

Related