Ofek
Ofek

Reputation: 404

Android studio - Google ads crash application

I'm trying to implement google ads services into my app with no luck..

I've tried to change the app's sdk version and also google ads implementation version and every single time the app just crashes on start, I have no idea what I'm doing wrong, any help is appreciated!

These are the stpes I followed:

  1. Dependencies for google ads:
 implementation 'com.google.android.gms:play-services-ads:18.3.0'
  1. AdView inside my activity_main.xml:
    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/bannerId"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true" />
  1. Initialization code in MainActivity.java
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
                AdView mAdView = (AdView)findViewById(R.id.adView);
                AdRequest adRequest = new AdRequest.Builder().build();
                mAdView.loadAd(adRequest);
            }
        });

Also I upgraded to sdk 28 (was 27), used the 'migrate to AndroidX button' to do so.

That's how my app gradle looks like:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.ofekTe.MyWorkouts"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    maven { url 'https://jitpack.io' }
    mavenCentral()
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.2.0-alpha01'
    implementation 'com.google.android.material:material:1.2.0-alpha03'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
    implementation 'com.miguelcatalan:materialsearchview:1.4.0'
    implementation 'com.github.amlcurran.showcaseview:library:5.4.3'
    implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'

    implementation 'com.google.android.gms:play-services-ads:18.3.0'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.3.0-alpha03'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha03'
}

Thats the logcat i get (The important parts atleast, if anything is missing make sure to tell me and i'll upload the whole log):

Logcat

Again, any help is appreciated. thanks!

Upvotes: 0

Views: 1411

Answers (2)

Brainiac001
Brainiac001

Reputation: 11

I had the same issue with my app, I had to add some meta-datas to my app manifest file

<application>
<meta-data
    android:name="com.google.android.gms.ads.AD_MANAGER_APP"
    android:value="true"/>
<meta-data
    android:name="com.google.android.gms.ads.com.example.appname"
    android:value="enter your AdMob App ID e.g. ca-app-pub-3940256099942544~3347511713"/>
</application>

This step is required as of Google Mobile Ads SDK version 17.0.0. Failure to add this tag results in a crash with the message: The Google Mobile Ads SDK was initialized incorrectly. for more info visit https://developers.google.com/admob/android/quick-start#update_your_androidmanifestxml

and https://developers.google.com/ad-manager/mobile-ads-sdk/android/quick-start#update_your_androidmanifestxml

Upvotes: 0

Sundeep
Sundeep

Reputation: 61

I think you are missing the codes required inside AndroidManifest.xml file

  1. Dependencies for google ads:

    implementation 'com.google.android.gms:play-services-ads:18.3.0'

  2. Add the below 'meta-data' codes inside your AndroidManifest.xml file Create your appID and use it, else for testing you can use the sample AdMob App ID in place of the "ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"

    <manifest>
    <application>
        <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
    </application>
    

With this the app should not crash at startup

The below codes will load the test ads from google. Compare them with your codes to change accordingly

  1. activity_main.xml file

    <com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    app:adSize="BANNER"
    app:adUnitId="ca-app-pub-3940256099942544/6300978111"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"></com.google.android.gms.ads.AdView>
    
  2. MainActivity.java file

    public class MainActivity extends AppCompatActivity {
    
         private AdView mAdView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
               mAdView = findViewById(R.id.adView);
               AdRequest adRequest = new AdRequest.Builder().build();
               mAdView.loadAd(adRequest);
        }}
    

    I created a new app with above codes and it showed the banner with a test ad as below

enter image description here

Upvotes: 1

Related Questions