How to integrate admob to Dukescript generated apk?

290 Views Asked by At

How do you add AdMob advertising functionality to your dukescript generated android project?

Let's say for example that I addeded android to the Hello World example and that the app is already published so I can add some ad units trough admob site, for example Ad unit ID ca-app-pub-99999999999999999/9999999999 for an interstitial and Ad unit ID ca-app-pub-9999999999999999/9999999998 for banner.

As far as I know you can't show them with javascript only, you should add some jars to the libraries folders and call explicitly some of their functions.

1

There are 1 best solutions below

1
On BEST ANSWER

You need to add the required libraries. There's a related question how to install them to your local repository here.

After you installed the libraries and reference them in your android project, you should be able to use AdMob. Follow the description here to wrap the DukeScript MainActivity in your own MainActivity. Then you should be able to create ads in the onCreate method as described here.

    import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

public class AndroidMain extends Activity {

    private InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mInterstitialAd = new InterstitialAd(this.getApplicationContext());
        mInterstitialAd.setAdUnitId("pub-99999999999999999/9999999999");
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice("adb logcat will show you the id")
                .build();
        // Prepare an Interstitial Ad Listener
        mInterstitialAd.setAdListener(new AdListener() {

            @Override
            public void onAdLoaded() {
                displayInterstitial();
            }

            @Override
            public void onAdClosed() {
                startActivity(new Intent(getApplicationContext(),
                        com.dukescript.presenters.Android.class));
                finish();
            }

        });
        mInterstitialAd.loadAd(adRequest);
    }

    public void displayInterstitial() {
        // If Ads are loaded, show Interstitial else show nothing.
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Toast.makeText(getApplication(), "The asd has not Loaded.", Toast.LENGTH_SHORT).show();
        }

    }

    public static void main(String... args) throws Exception {
        Main.onPageLoad();
    }
}

If you've finished that, modify your pom.xml to include not only the jar, but also the apklib of google play services:

    <groupId>com.google.android.gms</groupId>
    <artifactId>google-play-services</artifactId>
    <version>28.0.0</version>
    <type>apklib</type>
</dependency>
<dependency>
    <groupId>com.google.android.gms</groupId>
    <artifactId>google-play-services</artifactId>
    <version>28.0.0</version>
    <type>jar</type>
</dependency>

You wil also need to modify your AndroidManifest.xml and add some permissions and an activity. Here's the code for your app:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="web.aprendiendola.dictionaryweb.aprendiendola.dictionary.DiccionarioDeJava"
          android:versionCode="1"
          android:versionName="1.0-SNAPSHOT" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="DiccionarioDeJava"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
        <activity android:name="web.aprendiendola.dictionaryweb.aprendiendola.dictionary.DiccionarioDeJava.AndroidMain" 
                  android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.dukescript.presenters.Android" 
                  android:configChanges="orientation|screenSize"
                  android:exported="false"
        >
        </activity>
         <activity android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

        <!-- Configuration section. Defines: 
           - the HTML page to load on start
           - the class that contains the main initialization method
           - name of the initialization method in the given class
        -->
        <meta-data android:name="loadPage" android:value="file:///android_asset/pages/index.html" />
        <meta-data android:name="loadClass" android:value="web.aprendiendola.dictionaryweb.aprendiendola.dictionary.DiccionarioDeJava.AndroidMain" />
        <meta-data android:name="invoke" android:value="main" />
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />        
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>

That's it, you should now be able to display an ad. It works for me with the application you sent me.