Showing posts with label Admob. Show all posts
Showing posts with label Admob. Show all posts

Monday, February 6, 2017

Android programming: Banner Ads


I managed to set up the banner ad in my little app. Here is the summary of the changes:
  • Set minSdkVersion to 9 or higher.
  • To use Firebase, add these dependencies and plugin to the app/build.gradle file:
dependencies {
...
    compile 'com.google.android.gms:play-services:10.0.1'
    compile 'com.google.firebase:firebase-ads:10.0.1'
    compile 'com.google.android.gms:play-services-ads:10.0.1'
}

apply plugin: 'com.google.gms.google-services'

  • Add this dependency to the project's build.gradle file:
dependencies {
        classpath 'com.google.gms:google-services:3.0.0'
        classpath 'com.android.tools.build:gradle:2.2.3'
}
  • Add permission to use INTERNET in app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
  • Add the Ad unit ID and app ID to the strings.xml file. The following shows the test IDs used in Google's example. To get the IDs for my real app, I went to the Admob web site and registered my app. Admob then generated the Ad unit ID and the app ID, and offered to send the IDs and setup instruction to my Email.
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
<string name="banner_ad_app_id">ca-app-pub-3940256099942544~3347511713</string>

  •  Now the real stuff, add the AdView widget to the app:
...
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"

          xmlns:ads="http://schemas.android.com/apk/res-auto"
...
<com.google.android.gms.ads.AdView
           android:id="@+id/adView"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerHorizontal="true"
           android:layout_alignParentBottom="true"
           ads:adSize="BANNER"
           ads:adUnitId="@string/banner_ad_unit_id" />

  • In the onCreate() method of the Activity, or the onCreateView() method of the Fragment, add the code to fetch the Ad:
MobileAds.initialize(getActivity().getApplicationContext(), 
                     getString(R.string.banner_ad_app_id));

AdView mAdView = (AdView) v.findViewById(R.id.adView);
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest adRequest = builder.build();
mAdView.loadAd(adRequest);

  •  Note: I have tried to use builder.addKeyword() to retrieve targeting Ads. Unfortunately, this feature is not supported by Admob yet. And using builder.addKeyword() makes Ads rendering much slower.

Wednesday, February 1, 2017

Admob + Firebase: google-services.json is missing


After adding Firebase Admob into my Android app project, Gradle build failed with this error:

Error:Execution failed for task ':app:processDebugGoogleServices'.
> File google-services.json is missing. The Google Services Plugin cannot function without it.
   Searched Location:
  ...\app\src\debug\google-services.json
  ...\app\google-services.json

Where can I get the google-services.json?

I encountered this problem because I hadn't set up my project in Firebase yet.

I went to https://console.firebase.google.com/ and signed in. Followed the Create New Project step by step to set up the project in Firebase. After the project was successfully set up, it prompted me to download the google-services.json file. Then I copied the file to my project's app/ directory.

Then I re-synchronized Gradle and this time, no errors.

Admob + Firebase: Plugin with id 'com.google.gms.google-services' not found.


I was following the instruction in https://firebase.google.com/docs/admob/android/quick-start to add Admob into my Android App.

It sounded as simple as adding two lines into the app level's build.gradle:
...
    dependencies {
            compile fileTree(dir: 'libs', include: ['*.jar'])
            compile 'com.android.support:appcompat-v7:xx.x.x'
            compile 'com.google.firebase:firebase-ads:10.0.1'
        }
...

apply plugin: 'com.google.gms.google-services'
 However, after doing the Sync as the IDE required, this error was encountered:

   Gradle sync failed: Plugin with id 'com.google.gms.google-services' not found.

It seems the tutorial has left out some details. After I added the following two lines, this problem was solved.

In the Project level build.gradle, add dependency: classpath 'com.google.gms:google-services:3.0.0':

dependencies { 
     classpath 'com.google.gms:google-services:3.0.0'
     classpath 'com.android.tools.build:gradle:2.2.3'...
}

In the app level build.gradle, add dependency: compile 'com.google.android.gms:play-services:10.0.1'

dependencies { 
... 
    compile 'com.google.android.gms:play-services:10.0.1' 
    compile 'com.google.firebase:firebase-ads:10.0.1'
}

After that, click on the Sync link on the top right corner to synchronize Gradle.


 
Get This <