B4A Library AdsHelper extends FirebaseAdMob2 / Google Mobile Ads v20.0+

Status
Not open for further replies.
AdsHelper is a class that adds the following features:

- Managing the user consent with Google's User Messaging Platform: https://developers.google.com/admob/ump/android/quick-start
- App Open Ads: https://developers.google.com/admob/android/app-open-ads

It can be extended with more features.

Instructions:
1. It is important to read Google's documentation and understand the main concepts.
2 Add to manifest editor:
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
CreateResourceFromFile (Macro, FirebaseAdMob.FirebaseAds)
AddReplacement($ADMOB_APP_ID$, ca-app-pub-1267570xxxxx0~676xxxxx) 'your app id here

3. This class should be used in B4XPages projects. With some work you can use it in non-B4XPages but you will need to handle the much more complicated life cycle and split the tasks between the activity and the starter service.
4. Add to Main:
B4X:
#AdditionalJar: com.google.android.ump:user-messaging-platform


Consent

Sign up to Funding Choices and create an EU consent message: https://support.google.com/fundingchoices/answer/9180084
The logic itself is simple:
B4X:
If Ads.GetConsentStatus = "UNKNOWN" Or Ads.GetConsentStatus = "REQUIRED" Then
    Wait For (Ads.RequestConsentInformation(False)) Complete (Success As Boolean)
End If
If Ads.GetConsentStatus = "REQUIRED" And Ads.GetConsentFormAvailable Then
    Wait For (Ads.ShowConsentForm) Complete (Success As Boolean)
End If
We check the consent status. If it is unknown we request new information.
If the consent status is REQUIRED and a consent form is available then we show it.
The consent status is managed by Google and is cached locally.

In order to debug it we can set a test device and set the location to be in the EU (True) or outside (False):
B4X:
Ads.ResetConsentStatus
Ads.SetConsentDebugParameters("77A04EE40B2AFED2AFC67701365187EC", True)



1618324755085.png


App Open Ads

These are ads that appear when the app has moved to the foreground. It is configured to appear if the app was more than 2 minutes in the background and then resumed.

Instructions:
1. Call Ads.FetchOpenAd once.
2.
B4X:
Private Sub B4XPage_Foreground
    Ads.ShowOpenAdIfAvailable
End Sub

Private Sub B4XPage_Background
    Ads.Background
End Sub

AdsHelper class is included in the attached project.
The project will fail to run unless you set the correct app id in the manifest editor.

Updated version by @Jack Cole, including an example of using AdHelper in non-B4XPages project: https://www.b4x.com/android/forum/threads/adshelper-extension-for-traditional-b4a-apps-more.131798/
 

Attachments

  • AdsHelper.zip
    12.7 KB · Views: 233
Last edited:

SMOOTSARA

Active Member
Licensed User
AdsHelper is a class that adds the following features:

- Managing the user consent with Google's User Messaging Platform: https://developers.google.com/admob/ump/android/quick-start
- App Open Ads: https://developers.google.com/admob/android/app-open-ads

It can be extended with more features.

Instructions:
1. It is important to read Google's documentation and understand the main concepts.
2 Add to manifest editor:
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
CreateResourceFromFile (Macro, FirebaseAdMob.FirebaseAds)
AddReplacement($ADMOB_APP_ID$, ca-app-pub-1267570xxxxx0~676xxxxx) 'your app id here

3. This class should be used in B4XPages projects. With some work you can use it in non-B4XPages but you will need to handle the much more complicated life cycle and split the tasks between the activity and the starter service.
4. Add to Main:
B4X:
#AdditionalJar: com.google.android.ump:user-messaging-platform


Consent

Sign up to Funding Choices and create an EU consent message: https://support.google.com/fundingchoices/answer/9180084
The logic itself is simple:
B4X:
If Ads.GetConsentStatus = "UNKNOWN" Or Ads.GetConsentStatus = "REQUIRED" Then
    Wait For (Ads.RequestConsentInformation(False)) Complete (Success As Boolean)
End If
If Ads.GetConsentStatus = "REQUIRED" And Ads.GetConsentFormAvailable Then
    Wait For (Ads.ShowConsentForm) Complete (Success As Boolean)
End If
We check the consent status. If it is unknown we request new information.
If the consent status is REQUIRED and a consent form is available then we show it.
The consent status is managed by Google and is cached locally.

In order to debug it we can set a test device and set the location to be in the EU (True) or outside (False):
B4X:
Ads.ResetConsentStatus
Ads.SetConsentDebugParameters("77A04EE40B2AFED2AFC67701365187EC", True)



View attachment 111603

App Open Ads

These are ads that appear when the app has moved to the foreground. It is configured to appear if the app was more than 2 minutes in the background and then resumed.

Instructions:
1. Call Ads.FetchOpenAd once.
2.
B4X:
Private Sub B4XPage_Foreground
    Ads.ShowOpenAdIfAvailable
End Sub

Private Sub B4XPage_Background
    Ads.Background
End Sub

AdsHelper class is included in the attached project.
The project will fail to run unless you set the correct app id in the manifest editor.

Updated version by @Jack Cole, including an example of using AdHelper in non-B4XPages project: https://www.b4x.com/android/forum/t...b2-google-mobile-ads-v20-0.129696/post-828193


Hi
I get an error when I run the example.
What is the cause of this error message?
Thanks

B4X:
B4A Version: 10.90
Parsing code.    (0.04s)
    Java Version: 11
Building folders structure.    (0.01s)
Running custom action.    (0.07s)
Compiling code.    (0.10s)
Compiling layouts code.    (0.02s)
Organizing libraries.    (0.04s)
    (AndroidX SDK)
Compiling resources    (1.56s)
Linking resources    (0.59s)
Compiling debugger engine code.    (2.49s)
Compiling generated Java code.    (3.46s)
Convert byte code - optimized dex.    Error
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
    at com.android.dx.merge.DexMerger$8.updateIndex(DexMerger.java:565)
    at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:276)
    at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:574)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:166)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:198)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:336)
    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)
 

Rubsanpe

Active Member
Licensed User
Hi
I get an error when I run the example.
What is the cause of this error message?
Thanks

B4X:
B4A Version: 10.90
Parsing code.    (0.04s)
    Java Version: 11
Building folders structure.    (0.01s)
Running custom action.    (0.07s)
Compiling code.    (0.10s)
Compiling layouts code.    (0.02s)
Organizing libraries.    (0.04s)
    (AndroidX SDK)
Compiling resources    (1.56s)
Linking resources    (0.59s)
Compiling debugger engine code.    (2.49s)
Compiling generated Java code.    (3.46s)
Convert byte code - optimized dex.    Error
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
    at com.android.dx.merge.DexMerger$8.updateIndex(DexMerger.java:565)
    at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:276)
    at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:574)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:166)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:198)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:336)
    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)
Hi you need add #MultiDex: true to Project attributes.

Rubén
 

SMOOTSARA

Active Member
Licensed User
Hi you need add #MultiDex: true to Project attributes.

Rubén

Hi
add #MultiDex: but I get an error
B4X:
B4A Version: 10.90
Parsing code.    (0.03s)
    Java Version: 11
Building folders structure.    (0.15s)
Running custom action.    (0.07s)
Compiling code.    (0.10s)
Compiling layouts code.    (0.01s)
Organizing libraries.    (0.03s)
    (AndroidX SDK)
Compiling resources    (1.52s)
Linking resources    (0.58s)
Compiling debugger engine code.    (2.37s)
Compiling generated Java code.    (3.05s)
Convert byte code - optimized dex.    Error
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Library dex files are not supported in multi-dex mode
    at com.android.dx.command.dexer.Main.runMultiDex(Main.java:371)
    at com.android.dx.command.dexer.Main.runDx(Main.java:291)
    at com.android.dx.command.dexer.Main.main(Main.java:249)
    at com.android.dx.command.Main.main(Main.java:94)
 

Rubsanpe

Active Member
Licensed User
If I remember correctly, this error occurred when the Android SDK had some libraries that were not needed. I think I solved it by reinstalling the SDK and selecting only the necessary components.

Rubén
 
Status
Not open for further replies.
Top