How to fix the Android AdMob "Unable to obtain a JavascriptEngine" error?

Viewed 9018

MainActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        MobileAds.initialize(this) {}
        MobileAds.setRequestConfiguration(
            RequestConfiguration.Builder()
                .setTestDeviceIds(listOf("ABCDEF012345"))
                .build()
        )

        val adRequest = AdRequest.Builder().build()
        Log.d("Activity", "Is Test Device? : ${adRequest.isTestDevice(this)}")
        adView.loadAd(adRequest)

        adView.adListener = object : AdListener() {
            override fun onAdLoaded() {
                super.onAdLoaded()
                Log.d("Activity", "@@ onAdLoaded()")
            }

            override fun onAdFailedToLoad(err: LoadAdError?) {
                super.onAdFailedToLoad(err)
                Log.d("Activity", "@@ onAdFailedToLoad()\n$err")
            }

            override fun onAdOpened() {
                super.onAdOpened()
                Log.d("Activity", "@@ onAdOpened()")
            }

            override fun onAdClicked() {
                super.onAdClicked()
                Log.d("Activity", "@@ onAdClicked()")
            }

            override fun onAdLeftApplication() {
                super.onAdLeftApplication()
                Log.d("Activity", "@@ onAdLeftApplication()")
            }

            override fun onAdClosed() {
                super.onAdClosed()
                Log.d("Activity", "@@ onAdClosed()")
            }
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout                       
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id_for_test" />

</LinearLayout>

string.xml

<resources>
    <string name="admob_app_id">ca-app-pub-000000000000~00000000000</string>
    <string name="bannera_ad_unit_id_for_test">ca-app-pub-3940256099942544/6300978111</string>
</resources>
    

I created <meta-dat> in the manifest and added admob_app_id as the android:value value inside. In the AVD test device provided by Android Studio, a test advertisement appeared without any problems, but the test advertisement did not appear on the actual device. So, as in the MainActivity code above, I called RequestConfiguration and added my mobile device ID("ABCDEF012345") to the test device ID, but the problem was not resolved. The contents of the error were as follows.

enter image description here

5 Answers

Probably not the solution for everyone, but fixed it for me: don't forget to turn off all kinds of adblockers.

I had a adblocker DNS configured, took me some time to finally figure that out.

Most of the time AdMob does not perform as expected on the emulator, because there was no same stuff like play store services on it. Try on a real device. Sometimes a cold boot on the emulator solve the problem.

I was using manual proxy for the WIFI, ad forget to change and this strange thing happened to me was Unable to obtain a JavascriptEngine in onAdFailedToLoad, just soled by modifying the WIFI Network to Proxy>NONE in the Advance option by Long Tap on the connected WIFI

I've got a similar issue on Android when using native ads. The error information is very similar :

code:0 message:Unable to obtain a JavascriptEngine. domain:com.google.android.gms.ads cause:null responseInfo:{
  "Response ID": "null",
  "Mediation Adapter Class Name": "",
  "Adapter Responses": []
}

But, I've got a clue that might be the reason for this: As I've noticed weird crashes sometimes on the app, I've decided to add a check that indeed the app seems valid, by checking its signature.

What I've found is that in this case, at least 2 devices had the app changed (maybe by the same person). So this user for some reason tried to change the app. No idea what he tried to do with it, but I hope this could solve your problem.

If you want, here's a basic way to do it:

private var sIsValidInstall: Boolean? = null
private const val BYTE_SIZE_FOR_DEBUG_SIGNATURE=...
private const val CRC_FOR_DEBUG_SIGNATURE=...
private const val BYTE_SIZE_FOR_DEBUG_SIGNATURE=...
private const val CRC_FOR_DEBUG_SIGNATURE=...

@SuppressLint("PackageManagerGetSignatures")
fun isValidInstall(context: Context): Boolean {
    sIsValidInstall?.let { return it }
    var isValid = false
    val signatures = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
        context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_SIGNING_CERTIFICATES).signingInfo.apkContentsSigners
    else context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_SIGNATURES).signatures
    for (signature in signatures) {
        val bytes = signature.toByteArray()
        if (bytes != null) {
            val bytesSize = bytes.size
            val checksum = CRC32()
            checksum.update(bytes, 0, bytes.size)
            val crc = checksum.value
            isValid = (BuildConfig.DEBUG && bytesSize == BYTE_SIZE_FOR_DEBUG_SIGNATURE && crc == CRC_FOR_DEBUG_SIGNATURE) || (!BuildConfig.DEBUG && bytesSize == BYTE_SIZE_FOR_DEBUG_SIGNATURE && crc == CRC_FOR_DEBUG_SIGNATURE)
            if (isValid)
                break

        }
    }
    sIsValidInstall = isValid
    return isValid
}

I got same issue, i fixed it just by change device. Detail: I integrated google admob and mediation for native android. Based on a google tutorial, when building for samsung s7, i got problem “Unable to obtain a JavascroptEngine". After that I found a way to solve that but nothing change. I tried by using another device (shark 1) and the ad loaded. If you got the same problem, try using another device.

Related