B4A Library GoogleMobileAds(FirebaseAdmob 2021)

1616070586175.png


Hi there;)
Ok,I think the time has come to release my library over the new version of Google Mobile Ads SDK.

This library is fully compatible with the new version(20.0.0) of GoogleMobileAds SDK.
Have a look at it. (https://developers.google.com/admob/android/migration)
All changes in the link above have been applied in this version of my library.

Supported Ad Formats:
1616071098905.png


Note:
Before doing anything, choose whether you want to use Admob with Firebase? Or Standalone?
You can read the difference between using them in this link.(Use more features of Google Analytics and Firebase with AdMob apps)

How to implement Admob(With Firebase):
1616072696186.png

  • Add this code to your "Project Attributes" of Main activity:
B4X:
#AdditionalJar: com.google.firebase:firebase-ads

How to implement Admob(Without Firebase):
B4X:
AddApplicationText(
  <activity
  android:name="com.google.android.gms.ads.AdActivity"
  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
  android:theme="@android:style/Theme.Translucent" />
  <activity android:name="com.google.android.gms.ads.purchase.InAppPurchaseActivity"
  android:theme="@style/Theme.IAPTheme"/>
    <provider
            android:name="com.google.android.gms.ads.MobileAdsInitProvider"
            android:authorities="${applicationId}.mobileadsinitprovider"
            android:exported="false"
            android:initOrder="100" />
   <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="$ADMOB_APP_ID$"/>
)

Note: Instead of code above you can use this one from Firebase integration tutorial too:
B4X:
CreateResourceFromFile(Macro, FirebaseAdMob.FirebaseAds)

  • Then add your AdmobAppId and GooglePlayService macro to your manifest:
B4X:
AddReplacement($ADMOB_APP_ID$, ca-app-pub-3940256099942544~3347511713)
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
  • Add this code to your "Project Attributes" of Main activity:
B4X:
#AdditionalJar: com.google.android.gms:play-services-ads


Ok, Now everything is ready to display your ads.But you need to know a few important points.
  • This library is compatible with b4a v10.7 or higher.
  • This library is compatible with Admob version 19.7 or higher, So if you see any errors, please update your SDK first. To install it please start SDK Manager and in search field type: com.google.android.gms: play-services-ads and com.google.firebase:firebase-ads and install it.
1616073706300.png

  • You should Initializes the Google Mobile Ads SDK. The SDK will configure itself. Call this method as early as possible, and only once per application launch(Maybe in Starter_Create).
B4X:
Dim MobileAds As MobileAds
    MobileAds.Initialize
  • By default, the Google Mobile Ads SDK initializes app measurement and begins sending user-level event data to Google immediately when the app starts. This initialization behavior ensures you can enable AdMob user metrics without making additional code changes. However, if your app requires user consent before these events can be sent, you can delay app measurement until you explicitly initialize the Mobile Ads SDK or load an ad. To delay app measurement, add the following <meta-data> tag in your Manifest. Delay app measurement until MobileAds.initialize() is called.
B4X:
AddApplicationText(
    <meta-data
        android:name="com.google.android.gms.ads.DELAY_APP_MEASUREMENT_INIT"
        android:value="true"/>
)
  • For video ads to show successfully in your banner ad views or in native video ads, hardware acceleration must be enabled.(Add to manifest)
B4X:
SetApplicationAttribute(android:hardwareAccelerated, "true")

  • You can also use lite version of google mobile ads sdk. just replace your code in "Project Attributes" of Main activity with lite versions. Like any Android library, the Google Play services SDK increases the size of apps that include it. The Google Mobile Ads Lite SDK is a lightweight version of the Google Mobile Ads SDK built to reduce that impact. It is a fraction of the size of the regular SDK.
    Alongside reduced size, using the Lite SDK decreases the total number of methods referenced in an app. This is especially useful when deploying to older versions of Android, where developers can run into the 64K reference limit. (https://developers.google.com/admob/android/lite-sdk)
#AdditionalJar: com.google.firebase:firebase-ads ==> #AdditionalJar: com.google.firebase:firebase-ads-lite
#AdditionalJar: com.google.android.gms:play-services-ads ==> #AdditionalJar: com.google.android.gms:play-services-ads-lite


Limitations of the Lite SDK
The Lite SDK should only be used in apps distributed through the Google Play store.​
 

Attachments

  • GoogleMobileAds.zip
    73.7 KB · Views: 284
  • GoogleMobileAds-Example.zip
    19 KB · Views: 306
  • GoogleMobileAds-20.zip
    73.6 KB · Views: 261
Last edited:

ArminKh1993

Active Member
Thank you, but i think itsn't the solution. Im re-installing the SDK and i will test the sample again.

Rubén
This is not a definitive solution, it is a temporary solution.
But what I am sure of is that the problem is the SDK, not the library.
You can use my SDK until I know exactly which part of the SDK is the problem.
 

SMOOTSARA

Active Member
Licensed User
Longtime User
HI @ArminKh1993

This error is displayed when compiling

B4X:
B4A Version: 10.70
Parsing code.    (0.01s)
    Java Version: 11
Building folders structure.    (0.05s)
Compiling code.    (0.08s)
Compiling layouts code.    (0.02s)
Organizing libraries.    (0.00s)
    (AndroidX SDK)
Compiling resources    (1.80s)
Linking resources    (0.54s)
Compiling debugger engine code.    (1.69s)
Compiling generated Java code.    Error
B4A line: 44
container.AddView(NativeAd.AdView.
src\test\google\b4a\actnative.java:498: error: cannot access NativeAdView
_container.AddView((android.view.View)(mostCurrent._nativead.getAdView().build()),(int) (0),(int) (0),_container.getWidth(),_container.getHeight());
                                                                              ^
  class file for com.google.android.gms.ads.nativead.NativeAdView not found
 
Last edited:

ArminKh1993

Active Member
HI @ArminKh1993

This error is displayed when compiling

B4X:
B4A Version: 10.70
Parsing code.    (0.01s)
    Java Version: 11
Building folders structure.    (0.05s)
Compiling code.    (0.08s)
Compiling layouts code.    (0.02s)
Organizing libraries.    (0.00s)
    (AndroidX SDK)
Compiling resources    (1.80s)
Linking resources    (0.54s)
Compiling debugger engine code.    (1.69s)
Compiling generated Java code.    Error
B4A line: 44
container.AddView(NativeAd.AdView.
src\test\google\b4a\actnative.java:498: error: cannot access NativeAdView
_container.AddView((android.view.View)(mostCurrent._nativead.getAdView().build()),(int) (0),(int) (0),_container.getWidth(),_container.getHeight());
                                                                              ^
  class file for com.google.android.gms.ads.nativead.NativeAdView not found
View attachment 114668
Unfortunately, for some reason I am no longer active in this forum
You can use the @Erel 's library
The error I see is because it seems your SDK is not up to date or your are not add addtionalJar ... to your main activity
Good luck
 

Hani86

New Member
please help...when add ExoPlyer Library with GoogleAdmob ..App dont work ...
 

Attachments

  • Capture.PNG
    Capture.PNG
    12.1 KB · Views: 70
Top