How to use AdSense in Android WebView?
If you are not interested to use AdMob Ad in your WebView Android App, then alternative solution is AdSense. Prerequisites are
- Google Mobile Ads SDK version 20.6.0 or higher.
- Android API level 21 or higher.
First Step: build.gradle
Add dependencies in app/build.gradle
dependencies {
implementation 'androidx.browser:browser:1.8.0'
implementation 'com.google.android.gms:play-services-ads:23.3.0
}
Note: You should use latest version for both. When you read this article the version 23.3.0 version may become old.
Second Step: AndroidManifest.xml
Add the following <meta-data> between <application> tag in your AndroidManifest.xml file
<meta-data
android:name="com.google.android.gms.ads.INTEGRATION_MANAGER"
android:value="webview"/>
Third Step : MainActivity.java
onCreate(Bundle) is where you initialize your activity. Here add these activities
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
MobileAds.registerWebView(webView);
After sync, import
import android.webkit.CookieManager;
import com.google.android.gms.ads.MobileAds;
Final Step : Live Demonstration
Create APK file and see if the Ad appears or not.