AdMob Plugin (Premium)
This premium plugin facilitates the integration of Google AdMob ads (Banner, Interstitial, and Rewarded) into your Smart WebView application.
This is a premium plugin and is not included in the open-source project. Its source code is only available to Project Sponsors.
Setup and Configuration
- Obtain the Plugin: Acquire the plugin files through a GitHub sponsorship.
- Add to Project: Place the
AdMobPlugin.java
file in theplugins/
directory. - AdMob App ID:
- Add your AdMob App ID to
AndroidManifest.xml
:<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="YOUR_ADMOB_APP_ID"/>
- Also, update the
app_id
string inres/values/ads.xml
.
- Add your AdMob App ID to
- Enable Plugin: Ensure the plugin is enabled in
SmartWebView.java
:put("AdMobPlugin", true);
- Configure Ad Units: In
AdMobPlugin.java
, replace the default test ad unit IDs in thestatic
block with your real ad unit IDs for production.// In AdMobPlugin.java static { Map<String, Object> config = new HashMap<>(); // ... config.put("bannerAdUnitId", "YOUR_BANNER_ID"); config.put("interstitialAdUnitId", "YOUR_INTERSTITIAL_ID"); config.put("rewardedAdUnitId", "YOUR_REWARDED_ID"); // ... }
Usage
The plugin can be controlled from native code or via a JavaScript interface.
Displaying Ads from JavaScript
The plugin injects a window.AdMob
object into your web page.
// Show a banner ad at the bottom of the screen
window.AdMob.showBanner();
// Hide the banner ad
window.AdMob.hideBanner();
// Show an interstitial ad (if one is loaded)
window.AdMob.showInterstitial();
// Show a rewarded ad (if one is loaded)
window.AdMob.showRewarded();
// Check if an ad is ready to be shown
if (window.AdMob.isInterstitialReady()) {
// Safe to call showInterstitial()
}
Callbacks in JavaScript
You can define callback functions in your JavaScript to react to ad events.
// Called when a user earns a reward from a rewarded ad
window.AdMob.onUserEarnedReward = function(reward) {
console.log("User earned reward!", reward.amount, reward.type);
// Grant the user their reward in the web app
};