尝试加载 facebook 原生广告时,我的 android 应用程序崩溃(java.lang.NoSuchMethodError:没有静态方法 isChildDirected())

Posted

技术标签:

【中文标题】尝试加载 facebook 原生广告时,我的 android 应用程序崩溃(java.lang.NoSuchMethodError:没有静态方法 isChildDirected())【英文标题】:My android app Crash when trying to load a facebook Native Ad (java.lang.NoSuchMethodError: No static method isChildDirected()) 【发布时间】:2020-11-14 11:59:50 【问题描述】:

我的应用程序在尝试加载 facebook 原生广告时崩溃,这是日志错误:

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)

我正在使用最新版本的 Facebook Audience Network SDK 'com.facebook.android:audience-network-sdk:6.2.0' 并尝试了 'com.facebook.android:audience-network-sdk:6.0.0'

这是在许多其他应用程序上使用的实现及其工作但不适用于此应用程序:

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

当不调用loadAd 方法时,应用运行良好。

这是我在我的应用程序上使用的依赖项:

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'


【问题讨论】:

嗨@hamid,您找到任何解决方案或解决方法了吗? 嗨@mhmtkrgz,不,我仍然没有找到解决这个问题的方法。 【参考方案1】:

我认为问题与sdk中的class文件和audience_network.dex文件不一致有关。不知何故,我们遇到了完全相同的问题,并通过将 facebook sdk 作为 aar 文件添加到 gradle 文件来解决该问题。可以从以下maven仓库下载audience network sdk,直接尝试添加。

祝你好运。

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

【讨论】:

【参考方案2】:

已经有点晚了,但问题可能是您的资产文件夹中存在干扰 dex 文件。

【讨论】:

以上是关于尝试加载 facebook 原生广告时,我的 android 应用程序崩溃(java.lang.NoSuchMethodError:没有静态方法 isChildDirected())的主要内容,如果未能解决你的问题,请参考以下文章

为 Facebook 的 Android 原生广告预取图像

Recyclerview - Facebook 原生广告 - 不可点击

Facebook 原生广告 API,仅显示视频

如何让 Facebook 广告直播?

Facebook原生广告无法点击,无反应

Facebook Audience Network 返回错误“广告重新加载过于频繁”