H.JiMan
H.JiMan

Reputation: 350

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

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

Upvotes: 21

Views: 21432

Answers (9)

Antonio Cappiello
Antonio Cappiello

Reputation: 514

I had the exact same issue and I was able to solve it by turning off the VPN on my test device. So if you are currently using a VPN connection, that might be one of the things to try.

Upvotes: 0

Ali Exalter
Ali Exalter

Reputation: 420

this might fix but I have not tested on device yet but its working on android 14 simulator.

dependencies {
    implementation("androidx.javascriptengine:javascriptengine:1.0.0-beta01")
}

In variable.gradle or build.gradle(app) set minSdkVersion = 26

Upvotes: 0

Muzammil Hussain
Muzammil Hussain

Reputation: 1292

In my case i have adGuard on. To turn that off:

Goto settings > Network & internet > Private DNS > Off

Then test your ads it should work fine.

Upvotes: 5

android developer
android developer

Reputation: 116010

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
}

Another possible reason :

The time setting is wrong on the device.

Upvotes: -2

Hesham Yemen
Hesham Yemen

Reputation: 872

Add these lines in proguard-rule file

-keep class com.google.android.gms.ads.** { *; }
-keep class com.google.ads.** { *; }

Also read about Proguard and AdMob

Proguard and AdMob mediation

Upvotes: 0

ucMax
ucMax

Reputation: 5476

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.

  1. Visit the AdMob Policy Center to view any pending violations.
  2. Don't use VPN, Don't use Proxy at all.
  3. Just make sure to use test ad unit ID while debugging.
  4. Perform a cold boot on the emulator.
  5. Use a real device instead emulator.

Upvotes: 4

Alexander Fedoseev
Alexander Fedoseev

Reputation: 41

For Russia where AdMob is blocked by Google nowdays, you otherwise should switc VPN On.

Upvotes: 3

Ali Tamoor
Ali Tamoor

Reputation: 936

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

Upvotes: 1

Merthan Erdem
Merthan Erdem

Reputation: 6068

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.

Upvotes: 33

Related Questions