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.

How to use AdSense in Android WebView?


Before starting you must know that

  • You can't show AdSense ad from any local html file stored in assets folder.
  • It must be live website, and the domain must be authorized from same Google account used for both AdSense and AdMob.



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; 



Set up app-ads.txt and ads.txt both

To avoid any confusion, create both app-ads.txt and ads.txt at the root folder of your domain. You should use information from both AdSense and AdMob. You may also 301 redirect app-ads.txt to ads.txt which is acceptable by Google. Here is the sample:

 

https://www.devilhunter.net/ads.txt 

 

It has code snippet from both Google AdSense and Google AdMob. Although they look very similar, but there are differences. You may add others too.

 

 

Final Step : Live Demonstration

Create APK file and see if the Ad appears or not.