Johnny Gale
Johnny Gale

Reputation: 1

Why does my android app crashes when I implement android.gms:play-services-ads:19.3.0

Long story short, I develop a simple android java app to which I wish to implement some ads using the adsense service. The app itself works and does its purpose, but once I add the implementation 'com.google.android.gms:play-services-ads:19.3.0' in the gradle mobile module, the app crashes.

I also tried to complete all the possible necessary steps, including the android name & value metadata part in the Android manifest, and also set up the ads with libraries and the function of MobileAds.initialize() in the Main Activity.

I followed the steps in here:

https://developers.google.com/admob/android/quick-start?hl=import_the_mobile_ads_sdk

but as i tried to isolate the problem, I saw that by only including the implementation 'com.google.android.gms:play-services-ads:19.3.0' in the gradle mobile script (together with the google() spec in the repository) the app crashes from the start. It doesn't even enter.

What can I do? Why does this happen? I'll be glad if I receive some help.

Upvotes: 0

Views: 998

Answers (2)

Md Rehan
Md Rehan

Reputation: 353

Use the meta data tag in menifest like the given example code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.WatchAndEarn"
    tools:targetApi="31"
    android:usesCleartextTraffic="true">

    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-3304471310252738~5701061203"/>

    <activity
        android:name=".Home"
        android:exported="false">
        <meta-data
            android:name="android.app.lib_name"
            android:value="" />
    </activity>
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.lib_name"
            android:value="" />
    </activity>
</application>
</manifest>

And in MainActivity use initialization like the example given below

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
        }
    });
}

Upvotes: 0

Ben P.
Ben P.

Reputation: 54204

The documentation you've linked to includes this section: Update your AndroidManifest.xml

Important: This step is required as of Google Mobile Ads SDK version 17.0.0. Failure to add this <meta-data> tag results in a crash with the message: The Google Mobile Ads SDK was initialized incorrectly.

You've said that all you've done is add the dependency to your build.gradle file, so you've probably not done this step and that's your issue. You will need to find your App ID and add it to AndroidManifest.xml, as described in the documentation.

Upvotes: 1

Related Questions