B4A Class AdsHelper Extension for Traditional B4A apps & more

I wanted to share an update I made to the AdsHelper class and example.

Added:
  • support for rewarded video ads
  • events for rewarded video
  • events for rewarded interstitial
  • events for open ads
  • added parameter for open ads for setting the background delay before allowing the ad to show
  • incorporated native ad example and moved the code into a separate class
  • example of a fixed size banner ad (300x250)
  • support for traditional B4A apps

Modified:
  • code reorganized to make it more readable and easier to follow
 

Attachments

  • ActivityAdExample.zip
    19.4 KB · Views: 774
  • B4XPagesMobileAds.zip
    18.8 KB · Views: 693

amidgeha

Active Member
Licensed User
Longtime User
After MobileAds_Ready
onConsentInfoUpdateFailure: Invalid response from server: Failed to read publisher's account configuration; try again later., code: 1
Consent: UNKNOWN
 

kisoft

Well-Known Member
Licensed User
Longtime User
I am trying to compile an example but I get this error:

B4X:
Maven artifact not found: com.google.android.ump / user-messaging-platform
 

angel_

Well-Known Member
Licensed User
Longtime User
I am trying to compile an example but I get this error:
B4X:
Maven artifact not found: com.google.android.ump / user-messaging-platform
Add this
B4X:
#AdditionalJar: com.google.android.ump:user-messaging-platform
 

kisoft

Well-Known Member
Licensed User
Longtime User
@angel ...
This is already added in the example ... I think I need to remove the SDKs and get new ones ...
 

SMOOTSARA

Active Member
Licensed User
Longtime User

Hi
How can I get the following methods for AppOpens?



b4a:
'Sub OpenAd_onAppOpenAdLoaded
'Sub OpenAd_onAppOpenAdFailedToLoad(ErrorCode As Int)
'Sub OpenAd_onAdShowedFullScreenContent
'Sub OpenAd_onAdDismissedFullScreenContent
 

asales

Expert
Licensed User
Longtime User
The two firsts events has code in the class.
To the anothers you need to change the class with the respective java code.
B4X:
Sub OpenAd_ReceiveAd

Sub OpenAd_FailedToReceiveAd (ErrorCode As String)
 

SMOOTSARA

Active Member
Licensed User
Longtime User

Hi, this is exactly the problem I do not know about "Java code"
 

asales

Expert
Licensed User
Longtime User
... I do not know about "Java code"
Time to learn.
Check the java code in the class and the documentation to the AppOpenAd ads.
The java code to ReceiveAd/AdLoaded has 2 lines and I think that will be simple to make the changes.
And you could create a thread with your doubts about the code.
 

sorex

Expert
Licensed User
Longtime User
Hello Jack,

I'm trying your example but it won't compile and gives the error below.

Any idea?

B4X:
Compiling generated Java code.    Error
src\anywheresoftware\b4a\samples\smiley\adshelper.java:859: error: incompatible types: <anonymous OnUserEarnedRewardListener> cannot be converted to RewardedAdCallback
          mRewardedAd.show(context, new OnUserEarnedRewardListener() {
                                    ^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

javac 11.0.1
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
I downloaded both examples and they both compile without error on my system. Did you update libraries using the SDK manager? I usually find that to cause problems. You must be very careful about what you update.
 

sorex

Expert
Licensed User
Longtime User
no, it's the install as explained on the B4A instruction page as I had some issues with upgrading things before.

the only thing I did was install that user messaging platform thing.

do you use the 1.8.xxx java or the open one? I use the open 11.0.1 as you can see above.
 

sorex

Expert
Licensed User
Longtime User
switching from 11.0.1 to the latest 1.8.321 as javac compiler didn't change a thing tho.

other downloads (jdk11.0.1 & the resources.zip) are equal to what I downloaded before aswell.
 

sorex

Expert
Licensed User
Longtime User
Got it working now.

I renamed the c:\Android folder and created a new one and placed the content of the 2 zips inthere.

Something got corrupted somehow but it's still strange that all my other projects still compiled without problems.
 

SMOOTSARA

Active Member
Licensed User
Longtime User
Hey guys

I used this example in my program
I have updated the B4َA version to 11.5
The program exits at this line by error

B4X:
    ''' Error =========================================================================
    ''' Error =========================================================================
    AdLoader.RunMethod("loadAd", Array(AdRequestBuilder.RunMethod("build", Null)))
    ''' Error =========================================================================
    ''' Error =========================================================================

B4X:
Sub LoadNativeAd(AdName As String, IdAd As String)
    LogColor("LoadNativeAd: " & AdName, Colors.Green)

    Dim AdUnitId As String = IdAd
    
    Dim ctxt As JavaObject
    ctxt.InitializeContext
  
    Dim builder As JavaObject
    builder.InitializeNewInstance("com.google.android.gms.ads.AdLoader.Builder", Array(ctxt, AdUnitId))
  
    Dim onUnifiedAdLoadedListener As Object = builder.CreateEventFromUI("com/google/android/gms/ads/nativead/NativeAd.OnNativeAdLoadedListener".Replace("/", "."), _
       "UnifiedAdLoaded", Null)
    builder.RunMethod("forNativeAd", Array(onUnifiedAdLoadedListener))
  
    Dim Listener As JavaObject
'    Listener.InitializeNewInstance(Application.PackageName & ".main$MyAdListener", Array(AdName))  'change 'main' with the current activity module name
    Listener.InitializeNewInstance(Application.PackageName & ".mwnative$MyAdListener", Array(AdName, Me))
    builder.RunMethod("withAdListener", Array(Listener))
  
'    Sends a request To AdMob, requesting an ad.
    Dim AdLoader As JavaObject = builder.RunMethod("build", Null)
    
    Dim AdRequestBuilder As JavaObject
    AdRequestBuilder.InitializeNewInstance("com/google/android/gms/ads/AdRequest.Builder".Replace("/", "."), Null)
    
    ''' Error =========================================================================
    ''' Error =========================================================================
    AdLoader.RunMethod("loadAd", Array(AdRequestBuilder.RunMethod("build", Null)))
    ''' Error =========================================================================
    ''' Error =========================================================================

 
    Wait For (builder) UnifiedAdLoaded_Event (MethodName As String, Args() As Object)
    
    Dim NativeAd As JavaObject = Args(0)
    
'    Log(GetType(NativeAd))
'    Log("getHeadline: " & NativeAd.RunMethod("getHeadline", Null))
'    Log("getBody: " & NativeAd.RunMethod("getBody", Null))
'    Log("getAdvertiser: " & NativeAd.RunMethod("getAdvertiser", Null))
'    Log("getCallToAction: " & NativeAd.RunMethod("getCallToAction", Null))
'    Log("getPrice: " & NativeAd.RunMethod("getPrice", Null))
'    Log("getStarRating: " & NativeAd.RunMethod("getStarRating", Null))
'    Log("getStore: " & NativeAd.RunMethod("getStore", Null))
'    Log("getExtras: " & NativeAd.RunMethod("getExtras", Null))
  
    Dim NativeAdView As JavaObject
      
    NativeAdView.InitializeNewInstance("com/google/android/gms/ads/nativead/NativeAdView".Replace("/", "."), _
       Array(ctxt))   
                  
    Dim pNativeAdView As Panel = NativeAdView
  
    Dim content As Panel
    content.Initialize("")
    pNativeAdView.AddView(content, 0, 0, 100%x, 70%y)
    
    Dim lblAd As Label
    lblAd.Initialize("")
    lblAd.TextSize = 14
    lblAd.TextColor = xui.Color_White
    lblAd.Color = xui.Color_RGB(255,140,0) 'Dark Orange
    lblAd.Text = "AD"
    lblAd.Padding = Array As Int (0,0,0,0)
    lblAd.Gravity = Gravity.CENTER
    content.AddView(lblAd, 10dip, 1dip, 25dip, 16dip)
  
    Dim lbl As Label
    lbl.Initialize("")
    lbl.TextSize = 20
    lbl.TextColor = xui.Color_Black
    lbl.Text = NativeAd.RunMethod("getHeadline", Null)
    content.AddView(lbl, 10dip, 15dip, 300dip, 50dip)
    NativeAdView.RunMethod("setHeadlineView", Array(lbl))
  
    Dim lbl2 As Label
    lbl2.Initialize("")
    lbl2.TextColor = xui.Color_Black
    lbl2.Text = NativeAd.RunMethod("getBody", Null)
    content.AddView(lbl2, 10dip, 65dip, 300dip, 50dip)
    NativeAdView.RunMethod("setBodyView", Array(lbl2))
  
    Dim logo As JavaObject = NativeAd.RunMethod("getIcon", Null)
    If logo.IsInitialized Then
        Dim logoView As Panel
        logoView.Initialize("")
        logoView.Background = logo.RunMethod("getDrawable", Null)
        content.AddView(logoView, 250dip, 10dip, 80dip, 80dip)
        NativeAdView.RunMethod("setIconView", Array(logoView))
    End If
    
    Dim MediaContent As JavaObject = NativeAd.RunMethod("getMediaContent", Null)
    If MediaContent.IsInitialized Then
        Dim vid As JavaObject = MediaContent.RunMethod("getVideoController", Null)
    End If
        
    Log("vid.IsInitialized: " & vid.IsInitialized)
    'check if ad has a video
    If vid.IsInitialized Then
        Dim MediaView As JavaObject
        MediaView.InitializeNewInstance("com/google/android/gms/ads/nativead/MediaView".Replace("/", "."), Array(ctxt))
        content.AddView(MediaView, 10dip, 100dip, 280dip, 180dip)
        NativeAdView.RunMethod("setMediaView", Array(MediaView))
    Else
        'If don't has video, check if is there are images
        Dim images As List = NativeAd.RunMethod("getImages", Null)
        If images.IsInitialized And images.Size > 0 Then
            Dim MediaView As JavaObject
            MediaView.InitializeNewInstance("com/google/android/gms/ads/nativead/MediaView".Replace("/", "."), Array(ctxt))
            content.AddView(MediaView, 10dip, 100dip, 200dip, 200dip)
            NativeAdView.RunMethod("setImageView", Array(MediaView))
        End If
    End If
      
    '*** new button to action
    Dim btAction As Button
    btAction.Initialize("")
    Dim cd As ColorDrawable
    cd.Initialize(Colors.RGB(65,105,225),3dip)
    btAction.Background = cd
    btAction.Text = NativeAd.RunMethod("getCallToAction", Null)
    content.AddView(btAction, 10dip, 275dip, 100dip, 50dip)
    NativeAdView.RunMethod("setCallToActionView", Array(btAction))
  
    NativeAdView.RunMethod("setNativeAd", Array(NativeAd))
  
    TheParentPanel.AddView(pNativeAdView, 0, 0, 100%x, 70%y)
End Sub


B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
After MobileAds_Ready
Consent: OBTAINED
b4xmainpage_loadads (java line: 356)
java.lang.IncompatibleClassChangeError: The method 'java.lang.Class java.lang.Object.getClass()' was expected to be of type interface but instead was found to be of type virtual (declaration of 'com.google.android.gms.dynamic.ObjectWrapper' appears in /data/app/anywheresoftware.b4a.samples.smiley-2/base.apk)
    at com.google.android.gms.dynamic.ObjectWrapper.unwrap(com.google.android.gms:play-services-basement@@18.0.2:4)
    at com.google.android.gms.internal.ads.zzbji.zzl(com.google.android.gms:play-services-ads-lite@@20.6.0:20)
    at com.google.android.gms.ads.BaseAdView.loadAd(com.google.android.gms:play-services-ads-lite@@20.6.0:1)
    at anywheresoftware.b4a.admobwrapper.AdViewWrapper.LoadAd(AdViewWrapper.java:141)
    at anywheresoftware.b4a.samples.smiley.b4xmainpage._loadads(b4xmainpage.java:356)
    at anywheresoftware.b4a.samples.smiley.b4xmainpage$ResumableSub_CheckConsentAndAddAds.resume(b4xmainpage.java:276)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
    at anywheresoftware.b4a.BA$1.run(BA.java:352)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6138)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
b4xmainpage_loadads (java line: 356)
java.lang.IncompatibleClassChangeError: The method 'java.lang.Class java.lang.Object.getClass()' was expected to be of type interface but instead was found to be of type virtual (declaration of 'com.google.android.gms.dynamic.ObjectWrapper' appears in /data/app/anywheresoftware.b4a.samples.smiley-2/base.apk)
    at com.google.android.gms.dynamic.ObjectWrapper.unwrap(com.google.android.gms:play-services-basement@@18.0.2:4)
    at com.google.android.gms.internal.ads.zzbji.zzl(com.google.android.gms:play-services-ads-lite@@20.6.0:20)
    at com.google.android.gms.ads.BaseAdView.loadAd(com.google.android.gms:play-services-ads-lite@@20.6.0:1)
    at anywheresoftware.b4a.admobwrapper.AdViewWrapper.LoadAd(AdViewWrapper.java:141)
    at anywheresoftware.b4a.samples.smiley.b4xmainpage._loadads(b4xmainpage.java:356)
    at anywheresoftware.b4a.samples.smiley.b4xmainpage$ResumableSub_CheckConsentAndAddAds.resume(b4xmainpage.java:276)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
    at anywheresoftware.b4a.BA$1.run(BA.java:352)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6138)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
 
Last edited:

SMOOTSARA

Active Member
Licensed User
Longtime User
Sorry, I just now saw your post. Did you ever get it to work?
yes

The problem was solved

First, I deleted the tools in android SDK folder and replaced it with the new version from the link below

Then in B4A ->Tools -> Paths Configuration I changed the address from "android.jar ->28" to "android.jar ->32"

All known old libraries and applications work
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…