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 ''
    compile ''
    compile ''

apply plugin: ''

  • Add this dependency to the project's build.gradle file:
dependencies {
        classpath ''
        classpath ''
  • 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:

           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:

AdView mAdView = (AdView) v.findViewById(;
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest 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.

Friday, February 3, 2017

Java: Symbol not found due to duplicate jars

I was working on the Java program that used a 3rd party jar from Bouncy Castle. Although I was pretty sure the jar was included during the compilation, the cannot find symbol error was spit out.

       javac -cp:./bcxxxxxx.jar

After a while of confusion and frustration, I finally found out that there was another older version of the same Bouncy Castle jar lying on Java's extension directory. The compiler loaded the same class from the old jar first and ignored the duplicate one loaded later from the bcxxxxxx.jar that I tried to use. Unfortunately, the old one did not contain the method I was trying to use.

To solve this problem, I wanted javac to use my downloaded bcxxxxxx.jar and ignore the on the system's path. The compile option -Xbootclasspath/p:path came in handy:

            Prepend to the bootstrap class path.

To compile it:

      javac -Xbootclasspath/p:./bcxxxxxx.jar

To run it:

      java -Xbootclasspath/p:./bcxxxxxx.jar MyProgram

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:

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 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.
Get This <