B4A Library FirebaseAdmobPlus

FirebaseAdmobPlus is no longer supported, please check out GoogleMobileAds lib
https://www.b4x.com/android/forum/threads/googlemobileadssdk.128787/


Hi
This is a full wrapper over goolgle ads sdk.
https://developers.google.com/admob/android/quick-start

It supports :
- Banner (Also Adaptive Banner) | https://developers.google.com/admob/android/banner
- Interstitial | https://developers.google.com/admob/android/interstitial
- Rewerded | https://developers.google.com/admob/android/rewarded-ads
- AppOpenAds | https://developers.google.com/admob/android/app-open-ads
- UnifiedNative | https://developers.google.com/android/reference/com/google/android/gms/ads/formats/UnifiedNativeAd

First you need to follow the Firebase integration tutorial: https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/
Make sure to include the Ads manifest snippet.

Note:
- This library is compatible with Admob version 19.6 or higher, So if you see any errors, please update your SDK first.
1609934035079.png


- This library will be updated as soon as Google launches the new Google Mobile Ads SDK for Android version 20.0.0 In early 2021. So several APIs currently available will be changed or retired. See this https://developers.google.com/admob/android/migration

- You should initialialize MobileSdk at first once and wait for MobileAds_onInitializationComplete
then you can add your ads.
like this:
B4X:
    Dim MobileAds As MobileAds
        MobileAds.Initialize
    Wait For MobileAds_onInitializationComplete
        ' Here You Can Load Your Ads

- For video ads to show successfully in your banner ad views, hardware acceleration must be enabled.(Add to manifest)
B4X:
SetApplicationAttribute(android:hardwareAccelerated, "true")

- 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"/>
)
 

Attachments

  • FirebaseAdmobPlus.jar
    57.7 KB · Views: 491
  • FirebaseAdmobPlus.xml
    73.1 KB · Views: 437
  • Example.zip
    15.1 KB · Views: 486
Last edited:

ArminKh1993

Active Member
Sample Of Adaptive Banner:
Android_adaptive.png


B4X:
Sub Activity_Create(FirstTime As Boolean)
        Dim MobileAds As MobileAds
            MobileAds.Initialize
        Wait For MobileAds_onInitializationComplete
            Log("MobileAds_onInitializationComplete")
            
            ' Here You Can Load Your Ads
            Dim AdView As AdView
                AdView.Initialize("AdView","ca-app-pub-3940256099942544/6300978111",AdView.CreateAdaptiveSize(100%x))
                Activity.AddView(AdView,0,0,100%x,AdView.MeasureAdaptiveBannerHeight(100%x))
                AdView.LoadAd
End Sub
Sub AdView_onAdLoaded
        Log("AdView_onAdLoaded")    
End Sub
Sub AdView_onAdFailedToLoad (ErrorCode As Int)
        Log($"AdView_onAdFailedToLoad, ErrorCode: ${ErrorCode}"$)
End Sub
Sub AdView_onAdOpened
        Log("AdView_onAdOpened")    
End Sub
Sub AdView_onAdLeftApplication
        Log("AdView_onAdLeftApplication")    
End Sub
Sub AdView_onAdClicked
        Log("AdView_onAdClicked")    
End Sub
 

ArminKh1993

Active Member
UnifiedNativeAd:
1609930954025.png


B4X:
Sub Globals
        Dim UnifiedNativeAd As UnifiedNativeAd
End Sub
Sub Activity_Create(FirstTime As Boolean)
        Dim MobileAds As MobileAds
            MobileAds.Initialize
        Wait For MobileAds_onInitializationComplete
            Log("MobileAds_onInitializationComplete")
            CreateUnifiedNativeAd
End Sub


Sub CreateUnifiedNativeAd
        Dim VideoOptions As UnifiedNativeVideoOptions
            VideoOptions.setStartMuted(False)
        Dim AdOptions As UnifiedNativeAdOptions
            AdOptions.setAdChoicesPlacement(AdOptions.ADCHOICES_TOP_RIGHT).setVideoOptions(VideoOptions).setRequestMultipleImages(True)
            
        UnifiedNativeAd.Initialize("UnifiedNativeAd")
        UnifiedNativeAd.WithAdOptions(AdOptions)
        UnifiedNativeAd.Load("ca-app-pub-3940256099942544/2247696110")
End Sub
Sub UnifiedNativeAd_onAdLoaded
        Log("UnifiedNativeAd_onAdLoaded")   
        Log("IsAdAboutApps: " & UnifiedNativeAd.AdData.IsAdAboutApps)
        Log("HasImage: " & UnifiedNativeAd.AdData.HasImage)
        Log("ImagesCount: " & UnifiedNativeAd.AdData.ImagesCount)
        Log("getImageUriAt: " & UnifiedNativeAd.AdData.getImageUriAt(0))

        
        ' AdContainer
            Dim container As Panel
                container.Initialize("")
                Activity.addview(container,0,0,100%x,100%y)
                container.AddView(UnifiedNativeAd.AdView.build,0,0,container.Width,container.Height)
                container.Color = 0xffFAFAFA
    
            
        ' Check If Has Icon Or Not
            If UnifiedNativeAd.AdData.HasIcon Then
                Dim IconView As ImageView
                    IconView.Initialize("")
                    container.AddView(IconView,16dip,16dip,48dip,48dip)
                    IconView.Background = UnifiedNativeAd.AdData.Icon
                    UnifiedNativeAd.AdView.IconView = IconView                       
            End If
    
                  
        ' Headline
            Dim HeadlineView As Label
                HeadlineView.Initialize("")
                container.AddView(HeadlineView,16dip,16dip,container.Width - 32dip,30dip)
                HeadlineView.Text = UnifiedNativeAd.AdData.Headline
                HeadlineView.TextSize = 18
                HeadlineView.TextColor = Colors.Black
                UnifiedNativeAd.AdView.HeadlineView = HeadlineView
                
                ' Fix Dimen If Has Icon
                If UnifiedNativeAd.AdData.HasIcon Then
                    HeadlineView.Left = IconView.Left + IconView.Width + 16dip
                    HeadlineView.Width = container.Width - HeadlineView.Left - 16dip
                End If
                

        ' Advertiser
            Dim AdvertiserView As Label
                AdvertiserView.Initialize("")
                container.AddView(AdvertiserView,HeadlineView.Left,HeadlineView.Top + HeadlineView.Height,HeadlineView.Width,20dip)
                AdvertiserView.Text = UnifiedNativeAd.AdData.Advertiser
                AdvertiserView.TextSize = 16
                AdvertiserView.TextColor = Colors.Black
                UnifiedNativeAd.AdView.AdvertiserView = AdvertiserView
                If AdvertiserView.Text.Trim.Length = 0 Then AdvertiserView.Height = 0

          
        ' Call To Action
            Dim cta As Button
                cta.Initialize("")
                container.AddView(cta,16dip,container.Height - 48dip - 16dip,container.Width - 32dip,48dip)
                cta.Text = UnifiedNativeAd.AdData.CallToAction
                cta.Color = 0xff1976D2
                cta.TextColor = Colors.White
                cta.TextSize = 18
                cta.Gravity = Gravity.CENTER
                cta.Typeface = Typeface.DEFAULT_BOLD
                UnifiedNativeAd.AdView.CallToActionView = cta
          
        '  Body
            Dim BodyView As Label
                BodyView.Initialize("")
                container.AddView(BodyView,16dip,AdvertiserView.Top + AdvertiserView.Height + 32dip,container.Width - 32dip,50dip)
                BodyView.Text = UnifiedNativeAd.AdData.Body
                BodyView.TextSize = 16
                BodyView.TextColor = 0x99000000
                UnifiedNativeAd.AdView.BodyView = BodyView
                
                          
        'MediaView
            Dim mediaView As MediaView
                mediaView.Initialize("")
                container.AddView(mediaView,16dip,BodyView.Top + BodyView.Height + 16dip,container.Width - 32dip,200dip)
                mediaView.Height = mediaView.Width / UnifiedNativeAd.AdData.MediaContent.AspectRatio
                mediaView.Left = (container.Width - mediaView.Width) /2
                UnifiedNativeAd.AdView.MediaView = mediaView
 
            
            
        ' Set As NativeAd At *** Final ***
            UnifiedNativeAd.setAsNativeAd
End Sub
Sub UnifiedNativeAd_onAdFailedToLoad(ErrorCode As Int)
        Log($"UnifiedNativeAd_onAdFailedToLoad, ErrorCode: ${ErrorCode}"$)
End Sub
Sub UnifiedNativeAd_onAdOpened
        Log("UnifiedNativeAd_onAdOpened")
End Sub
Sub UnifiedNativeAd_onAdLeftApplication
        Log("UnifiedNativeAd_onAdLeftApplication")
End Sub
Sub UnifiedNativeAd_onAdClosed
        Log("UnifiedNativeAd_onAdClosed")
End Sub
 

ArminKh1993

Active Member
AppOpenAds:
1609931358284.png


B4X:
Sub Globals
        Dim AppOpenAds As AppOpenAds
End Sub
Sub Activity_Create(FirstTime As Boolean)
        Dim MobileAds As MobileAds
            MobileAds.Initialize
        Wait For MobileAds_onInitializationComplete
            Log("MobileAds_onInitializationComplete")
            LoadAppOpenAds
End Sub
Sub LoadAppOpenAds
        AppOpenAds.Initialize("AppOpenAds")
        AppOpenAds.LoadAd(AppOpenAds.APP_OPEN_AD_ORIENTATION_PORTRAIT,"ca-app-pub-3940256099942544/3419835294")
End Sub
Sub AppOpenAds_onAppOpenAdLoaded
        Log("AppOpenAds_onAppOpenAdLoaded")
        AppOpenAds.Show
End Sub
Sub AppOpenAds_onAppOpenAdFailedToLoad(ErrorCode As Int)
        Log($"AppOpenAds_onAppOpenAdFailedToLoad, ErrorCode: ${ErrorCode}"$)
End Sub
Sub AppOpenAds_onAdShowedFullScreenContent
        Log("AppOpenAds_onAdShowedFullScreenContent")
End Sub
Sub AppOpenAds_onAdDismissedFullScreenContent
        Log("AppOpenAds_onAdDismissedFullScreenContent")
End Sub
Sub AppOpenAds_onAdFailedToShowFullScreenContent(ErrorCode As Int)
        Log($"AppOpenAds_onAdFailedToShowFullScreenContent, ErrorCode: ${ErrorCode}"$)
End Sub
 

ArminKh1993

Active Member
RewardedAd:
1609931525434.png


B4X:
Sub Globals
        Dim RewardedAd As RewardedAd
End Sub
Sub Activity_Create(FirstTime As Boolean)
        Dim MobileAds As MobileAds
            MobileAds.Initialize
        Wait For MobileAds_onInitializationComplete
            Log("MobileAds_onInitializationComplete")
            LoadRewardedAd
End Sub
Sub LoadRewardedAd
        RewardedAd.Initialize("RewardedAd")
        RewardedAd.LoadAd("ca-app-pub-3940256099942544/5224354917")
End Sub
Sub RewardedAd_onRewardedAdLoaded(Item As RewardItem)
    Log($"RewardedAd_onRewardedAdLoaded, Type: ${Item.Type}, Amount: ${Item.Amount}"$)
        If RewardedAd.IsLoaded Then RewardedAd.Show
End Sub
Sub RewardedAd_onRewardedAdFailedToLoad (ErrorCode As Int)
        Log($"RewardedAd_onRewardedAdFailedToLoad, ErrorCode: ${ErrorCode}"$)
End Sub
Sub RewardedAd_onRewardedAdOpened
        Log("RewardedAd_onRewardedAdOpened")
End Sub
Sub RewardedAd_onRewardedAdFailedToShow (ErrorCode As Int)
        Log($"RewardedAd_onRewardedAdFailedToShow, ErrorCode: ${ErrorCode}"$)
End Sub
Sub RewardedAd_onUserEarnedReward (Item As RewardItem)
        Log($"RewardedAd_onUserEarnedReward, Type: ${Item.Type}, Amount: ${Item.Amount}"$)
End Sub
Sub RewardedAd_onRewardedAdClosed
        Log("RewardedAd_onRewardedAdClosed")
End Sub
 

ArminKh1993

Active Member
InterstitialAd:
1609931734334.png


B4X:
Sub Globals
        Dim InterstitialAd As InterstitialAd
End Sub
Sub Activity_Create(FirstTime As Boolean)
        Dim MobileAds As MobileAds
            MobileAds.Initialize
        Wait For MobileAds_onInitializationComplete
            Log("MobileAds_onInitializationComplete")
            LoadInterstitialAd
End Sub
Sub LoadInterstitialAd
        InterstitialAd.Initialize("InterstitialAd")
        InterstitialAd.LoadAd("ca-app-pub-3940256099942544/1033173712")
End Sub
Sub InterstitialAd_onAdLoaded
        Log("InterstitialAd_onAdLoaded")
        If InterstitialAd.isLoaded Then InterstitialAd.Show
End Sub
Sub InterstitialAd_onAdFailedToLoad (ErrorCode As Int)
        Log($"InterstitialAd_onAdFailedToLoad, ErrorCode: ${ErrorCode}"$)
End Sub
Sub InterstitialAd_onAdOpened
        Log("InterstitialAd_onAdOpened")   
End Sub
Sub InterstitialAd_onAdClosed
        Log("InterstitialAd_onAdClosed")
End Sub
Sub InterstitialAd_onAdLeftApplication
        Log("InterstitialAd_onAdLeftApplication")
End Sub
 

ArminKh1993

Active Member
AdOpen is crashing, Other ones are working


B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
AdView_onAdLoaded
InterstitialAd_onAdLoaded
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (interstitialad_onadopened)
sending message to waiting queue (interstitialad_onadclosed)
running waiting messages (2)
InterstitialAd_onAdOpened
InterstitialAd_onAdClosed
** Activity (main) Resume **
AdView_onAdLoaded
AppOpenAds_onAppOpenAdLoaded
java.lang.NoSuchMethodError: No virtual method show(Landroid/app/Activity;Lcom/google/android/gms/ads/FullScreenContentCallback;)V in class Lcom/google/android/gms/ads/appopen/AppOpenAd; or its super classes (declaration of 'com.google.android.gms.ads.appopen.AppOpenAd' appears in /data/app/com.domain.testad-1/base.apk)
    at com.b4a.admobwrapper.AppOpenAdsWrapper.Show(AppOpenAdsWrapper.java:117)
    at com.domain.testad.main._appopenads_onappopenadloaded(main.java:628)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5307)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
As i said in first post, you should update your sdk to 19.6.0
 

ArminKh1993

Active Member
Yes, I have updated to SDK 19.6.0 and i get this error


B4X:
main_appopenads_onappopenadloaded (java line: 484)
java.lang.NoSuchMethodError: No virtual method show(Landroid/app/Activity;Lcom/google/android/gms/ads/FullScreenContentCallback;)V in class Lcom/google/android/gms/ads/appopen/AppOpenAd; or its super classes (declaration of 'com.google.android.gms.ads.appopen.AppOpenAd' appears in /data/app/com.domain.testad-8QfUN8N17FRXdvXhqnDabw==/base.apk)
are you tried it in attached example?
 

ArminKh1993

Active Member
I have deleted the whole sdk folder and redownloaded new zip files, and only updated the play services ads and am getting this error. Hm I think something is wrong somewhere

B4X:
B4A Version: 10.50
Parsing code.    (0.01s)
    Java Version: 14
Building folders structure.    (0.03s)
Compiling code.    (0.02s)
Compiling layouts code.    (0.00s)
Organizing libraries.    (0.00s)
    (AndroidX SDK)
Generating R file.    (0.00s)
Compiling generated Java code.    (0.03s)
Convert byte code - optimized dex.    Error
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/ads/internal/overlay/zzv;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/ads/zzb;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/internal/ads/zzaar;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/internal/ads/zzaas;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/internal/ads/zzaat;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/internal/ads/zzaau;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/internal/ads/zzabl;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/internal/ads/zzabn;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/internal/ads/zzabo;
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Translation has been interrupted
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:692)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:315)
    at com.android.dx.command.dexer.Main.runDx(Main.java:293)
    at com.android.dx.command.dexer.Main.main(Main.java:249)
    at com.android.dx.command.Main.main(Main.java:94)
Caused by: java.lang.InterruptedException: Too many errors
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:684)
    ... 4 more
as erel said at https://www.b4x.com/b4a.html you should use :
Oracle Java 8 or OpenJDK 11
and i see you are using java 14 !!
 
Last edited:

asales

Expert
Licensed User
Longtime User
A new format ad has coming (in beta for now, but I can see the format in the Admob console):
Rewarded Interstitial Ad (different of Rewarded Video)

 

ArminKh1993

Active Member
A new format ad has coming (in beta for now, but I can see the format in the Admob console):
Rewarded Interstitial Ad (different of Rewarded Video)

I will take a look at it, but I will not update the library before releasing version 20 of google ads sdk
 

SMOOTSARA

Active Member
Licensed User
Longtime User
Hi
This is a full wrapper over goolgle ads sdk.
https://developers.google.com/admob/android/quick-start

It supports :
- Banner (Also Adaptive Banner) | https://developers.google.com/admob/android/banner
- Interstitial | https://developers.google.com/admob/android/interstitial
- Rewerded | https://developers.google.com/admob/android/rewarded-ads
- AppOpenAds | https://developers.google.com/admob/android/app-open-ads
- UnifiedNative | https://developers.google.com/android/reference/com/google/android/gms/ads/formats/UnifiedNativeAd

First you need to follow the Firebase integration tutorial: https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/
Make sure to include the Ads manifest snippet.

Note:
- This library is compatible with Admob version 19.6 or higher, So if you see any errors, please update your SDK first.
View attachment 105571

- This library will be updated as soon as Google launches the new Google Mobile Ads SDK for Android version 20.0.0 In early 2021. So several APIs currently available will be changed or retired. See this https://developers.google.com/admob/android/migration

- You should initialialize MobileSdk at first once and wait for MobileAds_onInitializationComplete
then you can add your ads.
like this:
B4X:
    Dim MobileAds As MobileAds
        MobileAds.Initialize
    Wait For MobileAds_onInitializationComplete
        ' Here You Can Load Your Ads

- For video ads to show successfully in your banner ad views, hardware acceleration must be enabled.(Add to manifest)
B4X:
SetApplicationAttribute(android:hardwareAccelerated, "true")

- 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"/>
)

HI ArminKh1993 ?
Thank you for a good library?

There is a small bug in the "IsLoaded" control time
Returns the value "null" but the default should be "false"

Please test the following codes

B4X:
 InterstitialAd.Initialize("InterstitialAd")

    Log(InterstitialAd.IsLoaded)

    If InterstitialAd.IsLoaded = True Then

        Log("InterstitialAd  Is Loaded and go to show ad")

    Else

        InterstitialAd.LoadAdWithBuilder(xxx,builder)

    End If


B4X:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.android.gms.ads.InterstitialAd.isLoaded()' on a null object reference
 

ArminKh1993

Active Member
HI ArminKh1993 ?
Thank you for a good library?

There is a small bug in the "IsLoaded" control time
Returns the value "null" but the default should be "false"

Please test the following codes

B4X:
 InterstitialAd.Initialize("InterstitialAd")

    Log(InterstitialAd.IsLoaded)

    If InterstitialAd.IsLoaded = True Then

        Log("InterstitialAd  Is Loaded and go to show ad")

    Else

        InterstitialAd.LoadAdWithBuilder(xxx,builder)

    End If


B4X:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.android.gms.ads.InterstitialAd.isLoaded()' on a null object reference
i will take a look at it tomorrow
 
Top