Epsilon3 Epsilon3 - 8 months ago 104
Android Question

Implementing Google ads via Android-Studio and the 'New' Google Play Services SDK

Using Android Studio to compile Google Ads via Google Play Services SDK. When I add this tag to my Android manifest file:

<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />


I get a compiler error that the integer value for 'google_play_services_version' is not defined - the text inside these quotes is also error red. What is the problem?

Answer Source

You will find many 'tips' and 'tricks' to get Android ads running using the 'new' Google play services library. Most of these will have you chasing your tail adding jar files to the 'libs' folder and adding references to the libraries found inside these jar files within your project settings/structure interfaces. In fact, adding Google ads to an Android project via Android Studio requires:

1) Add this meta tag to your Android manifest file - it can go between the opening "Application" xml tag and before the first (main) "Activity" xml tag (the text, as indicated in the question above, will be error red initially - we'll fix that in another step):

    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

2) Add this activity to your Android manifest file:

    <activity android:name="com.google.android.gms.ads.AdActivity"          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

3) To your "build.gradle" file (the one in your project folder, not the one in the root folder - i.e., the one in-line with your "src" folder, not the one in-line with your package name folder), add this to the "dependencies" group (this is the step that alleviates the error described in the question):

compile 'com.google.android.gms:play-services:4.0.30'

4) Minimim target SDK is 9+, target is 13+.

5) Use "com.google.android.gms.ads.AdView" rather than the "com.google.gms.ads..." widgets.

6) Either in code (example below) or in xml layout, instantiate the AdView and, before you call "mAdView.loadAd(adRequest);", be certain to set:

    mAdView.setAdSize(AdSize.BANNER); // Or whatever size you like
    mAdView.setAdUnitId("YOUR_AD_UNIT_ID"); //  Use your ID, requires an AdMob account

7) If you are coding on MacOS and you get a compiler update error (update to 51 from 50, for example), then you are compiling using the Java 6.0 SDK - this is a problem, because Android Studio will only run with 6.0, but certain of the AdView libraries you might have added (review the "finally" section in this answer next), require a 7.0 compiler. This is really a non-issue here (given the note in the "finally" section below), but here is a good resource for using Java 7 SDK to compile - follow these instructions, then restart Android and let it re-install the 6 SDK for its operational needs: http://www.cc.gatech.edu/~simpkins/teaching/gatech/cs2340/guides/java7-macosx.html

Finally, if you've been chasing your tail and added jar/lib settings as described above, when you compile you will get errors indicating that you've duplicated your inclusions of AdView/AdRequest, etc... or if you're getting compiler version errors (urging you to update from 50 to 51). Just delete these jar/lib additions and rely on the gradle edits described in step #3.

This is the official link that solves this problem: https://developers.google.com/mobile-ads-sdk/docs/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download