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.

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 MyProgram.java

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:

      -Xbootclasspath/p:path
            Prepend to the bootstrap class path.

To compile it:

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

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