简体   繁体   中英

Google AdMob AdView is always blank

I'm attempting to get AdMob ads working in an Android app but no matter what I try the AdView always seems to be blank regardless of whether it's a test ad or a real ad I'm trying to show and whether it's being shown on a virtual device or a physical one.

I've now created a blank project and attempted to copy the Google Tutorial directly still with no luck. I'm still using the sample IDs in the tutorial but the result is still the same when using my own ID.

I have the following added to my project level build.gradle:

allprojects {
repositories {
    google()
    jcenter()

    }
}

I am getting an error on my app level build.gradle but I can find very little information about what is causing it or how to go about fixing it. About the only information I found was a line to tell Android studio to stop reporting the error. I have the following in my app level build.gradle:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.android.gms:play-services-ads:17.2.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

The error occurs on the second "implementation" line and is:

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 28.0.0, 26.1.0. Examples include com.android.support:animated-vector-drawable:28.0.0 and com.android.support:customtabs:26.1.0

Other than this there do not appear to be any other errors in the code.

I've added the AdView to activity_main.xml as follows:

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="1000px"
        android:layout_height="500px"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544~3347511713">
    </com.google.android.gms.ads.AdView>

I've also switched to using RelativeLayout as that is what's used in the tutorial.

I'm then using the following Java to (hopefully) request an ad.

    package com.jakecharman.myapplication;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import com.google.android.gms.ads.MobileAds;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;

    public class MainActivity extends AppCompatActivity {

        private AdView mAdView;

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

            // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
            MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
            mAdView = findViewById(R.id.adView);
            AdRequest adRequest = new 
            mAdView.loadAd(adRequest);
        }
    }

When I run the app on the virtual device it runs as expected but the AdView is not visible. I get some errors in the logs, none of which I can find much useful information about online:

E/Ads: Google Mobile Ads SDK initialization functionality unavailable for this session. Ad requests can be made at any time.
I/Ads: This request is sent from a test device.

I/Ads: Ad failed to load : 0
W/cr_ChildProcessConn: onServiceDisconnected (crash or killed by oom): pid=15347

I'm also getting:

W/Ads: Invoke Firebase method getInstance error.
W/Ads: The Google Mobile Ads SDK will not integrate with Firebase. Admob/Firebase integration requires the latest Firebase SDK jar, but Firebase SDK is either missing or out of date

But as far as I understand it this is fine since I'm not trying to use Firebase?

Any help on this would be greatly appreciated, this is the first time I've properly tried writing anything for Android and this is the last issue holding me back.

Make sure you've added the admob gradle dependency to your project in the app level build.gradle file. This can be done in 2 ways, either by adding play-services-ads directly

implementation 'com.google.firebase:firebase-ads:18.2.0'

or if you're already using (or plan to use) firebase by adding firebase-ads library .

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

Benefit of using play-services-ads is that the minSdkVersion is 14 while the firebase-ads implementation requires a minSdkVersion of 16.

In order for these dependencies to be resolved you'll also need to add google() repository to you project level build.gradle file. I see in your example code you did so I won't repeat that part here.

As of version 17.0.0 of these libraries you need to add your Admob ApplicationId in the AndroidManifest.xml file of your project.

<application
      ...>
     <!-- meta data -->
     <meta-data
         android:name="com.google.android.gms.ads.APPLICATION_ID"
         android:value="@string/cAppId" /> 
</application>

And as a final step initialise the admob in your project somewhere on startup of app using the Admob.initialize() method. That is also in place in your example I see.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM