Android Question Using Native ads in Firebase admob2

Fabio vega

Member
Licensed User
Longtime User
Hello, I use the guie: https://www.b4x.com/android/forum/threads/firebaseadmob2-google-mobile-ads-sdk-v20.129609/

to migrate mi app to new Firebase admob2 and sing V3.

https://developers.google.com/admob/android/migration

but when I use native ads, I get one error : java.lang.ClassNotFoundException: com.google.android.gms$ads$formats$UnifiedNativeAdView

in module code:
line error:
    NativeUnifiedAdView.InitializeNewInstance("com/google/android/gms/ads/formats/UnifiedNativeAdView".Replace("/", "."), _
       Array(ctxt)) '

file name: mwFirebaseAdmobNative

Work code with B4A 10.6 SDK 28:
Sub LoadUnifiedNativeAd
  
    If isTesting Then LogColor("Trying to load unified native ad",Colors.Yellow)
    Dim AdUnitId As String = AdID
    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/formats/UnifiedNativeAd.OnUnifiedNativeAdLoadedListener".Replace("/", "."), _
       "UnifiedNativeAdLoaded", Null)
    builder.RunMethod("forUnifiedNativeAd", Array(onUnifiedAdLoadedListener))

    Dim AdLoader As JavaObject = builder.RunMethod("build", Null)
    Dim AdrequestBuilder As JavaObject
    AdrequestBuilder.InitializeNewInstance("com/google/android/gms/ads/AdRequest.Builder".Replace("/", "."), Null)
    Dim AdRequestBuilder1 As AdRequestBuilder = AdrequestBuilder 'JavaObject builder
'    Try
'        If Starter.consent.IsRequestLocationInEeaOrUnknown Then
'            LogColor("EU or unknown",Colors.Magenta)
'            Starter.firebaseanalytics.SendEvent("ads_non_personalized",Null)
'            AdRequestBuilder1.NonPersonalizedAds
'        Else
'            LogColor("non-EU",Colors.Magenta)
'            Starter.firebaseanalytics.SendEvent("ads_personalized",Null)
'        End If
'        LogColor("trying to load unified ad",Colors.Red)
'    Catch
'        Log(LastException.Message)
'    End Try
  
    AdLoader.RunMethod("loadAd", Array(AdrequestBuilder.RunMethod("build", Null)))
  
    Wait For (builder) UnifiedNativeAdLoaded_Event (MethodName As String, Args() As Object)
    If SubExists(ParentModule,"nativead_ReceiveAd") Then CallSubDelayed(ParentModule,"nativead_ReceiveAd")
    If isTesting Then Log("UnifiedNativeAdLoaded_Event")
    Try
        Dim NativeUnifiedAd As JavaObject = Args(0)
        If isTesting Then Log(NativeUnifiedAd.RunMethod("getHeadline", Null))
        Dim NativeUnifiedAdView As JavaObject
'    LogColor("unified ad loaded 2",Colors.Yellow)
        NativeUnifiedAdView.InitializeNewInstance("com/google/android/gms/ads/formats/UnifiedNativeAdView".Replace("/", "."), _
       Array(ctxt)) '
        'com.google.android.gms.ads.internal.formats.client.IUnifiedNativeAd
        Dim pNativeAdView As Panel =NativeUnifiedAdView
        AdPanelHolder=pNativeAdView
        Dim content As Panel
        content.Initialize("")
        pNativeAdView.AddView(content, 0, 0, pWidth, pHeight)
        content.LoadLayout("NativeUnifiedAd")
    
        GetAndSetText(NativeUnifiedAd, NativeUnifiedAdView,"Headline",Headline,22)
      
        GetAndSetText(NativeUnifiedAd,NativeUnifiedAdView,"Body",Body,22)
      
        GetAndSetText(NativeUnifiedAd,NativeUnifiedAdView,"CallToAction",CallToActionLabel,22)
      
        GetAndSetImage(NativeUnifiedAd,NativeUnifiedAdView,"Logo",ImageLogo, content, ctxt)
      
        images = NativeUnifiedAd.RunMethod("getImages", Null)
'        SetExtraImage(NativeUnifiedAdView,ImageMain,0,True)
      
        NativeUnifiedAdView.RunMethod("setNativeAd", Array(NativeUnifiedAd))     

      
        CallingPanel.AddView(pNativeAdView, pLeft, pTop, pWidth, pHeight)
      
    Catch
        Log(LastException.Message)
    End Try

End Sub


any solution for B4A 10.9 or b4A 11 .. thanks
 
Last edited:

asales

Expert
Licensed User
Longtime User
This is an old code and it don't works with the new versions.

Check this update:
 
Upvote 0
Top