Author Topic: Android: Adding Admob & Basic Game Controller Support  (Read 973 times)

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1638
    • View Profile
Hi All,
For GLB users that are interested in Android Studio development and want controller and in-app Ads.

ANDROID ADMOB with Android Studio

Requires the *New* versions of SDLGlbasic.java and Interstitial.java AStudio files. The new update from SF (Im also might edit this post) works perfectly for instant placement of Interstitial Ads in your Apps. This update is still not pushed to steam.

First at all here is the download file for the fixes and extras for Android Studio:
https://www.dropbox.com/s/v5tr0evzehka1v4/Admob.zip?dl=0

Step 1

AndroidManifest.xml - add meta info

<application
        android:isGame="true"
        android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:allowBackup="true"
        android:banner="@mipmap/banner"
        android:hardwareAccelerated="true">
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713" />
<!-- replace testid with own id -->

....
  <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
    </application>

</manifest>


Step 2

Open string.xml in the res > values folder
Add the following:

<string name="MobileAds_setAdUnitId">ca-app-pub-3940256099942544/1033173712</string>

Note: When live you can replace testid with your own app advert id provided by Admob console



Step 3
open build.gradle file within the "app" folder in Android Studio
add the bolded item to dependancies, so its look like this (its near the bottom of it and the old one can been replaced):

Code: (glbasic) [Select]
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.gms:play-services-ads:18.3.0'
}

in the soon steam update, then the lines has allready been added for new projects, but admob just commented out.

Step 4

Open Android Studio, right click on SDLGlbasic.java and then let it Open in Explorer from the file you have downloaded from the top of the post.

Those 3 files should been placed and eventuelt replace the Java files in that folder.

Open SDLGlbasic.java in AStudio

 ** New version of SDLGlbasic.java is being pushed to Steam soon, so you can skip this step over **

Step 5

In GLBASIC when invoking the ads there is no need to re-load ads - the admob listener takes care of that.

Code: (glbasic) [Select]
?IFDEF ANDROID
IMPORT "C" const char* android_JAVACALL(const char* string)
?ENDIF

FUNCTION JavaCall$: callStr$
?IFDEF ANDROID
STATIC res$
res$ = android_JAVACALL(callStr$)
RETURN res$
?ENDIF
ENDFUNCTION

The above function let you call Java function from Glbasic.

// Run after a command press or touchscreen to start a gameloop / next level / or jump back to main menu
// This call the ads its self and also load a new one.
// Also if a ads has been failed, no ads will been shown, but will try again next time you using it.

JavaCall$("ads_show")

// This returns the current status of ads, if something is happens, fails or succes.
Statust$=JavaCall$("ads_status")

Good luck!

Thanks to SF for sticking it out and getting it sorted in the end!

------

SPACE FRACTAL: This works DOES work now, and im ends with to use tilt, keys and touch events to pickup the JavaCalls from a Activity.



ADDING GAMECONTROLLER for Android Studio

This section is heavy edited as its now much better implemeted and now can use all the JOY() commands for all officiel Google supported JoyPads, those which navigation on system its self works, as well one another, which is the Ouya Controller. A Microsoft Xbox One controller might still not work as im have not tested it.

Now you can use GETJOYNAME$(ID) to get a kind of joystick, if there is any of them. GETJOYNAME$(0) is allways to been used by like a TV Remote as well for TILT. GETJOYNAME$(1) when connected is used by the first connected gamepad controller.

Howover as Mr Plov as pointed out, you can still use KEY() values as this has not been touched as such (except tilt). Howover KEY() now expand more than 256 values: One for each gamepad connected (GETJOYNAME$(0) uses 0-255 and GETJOYNAME$(1) uses KEY(256) -> KEY(511) and so on. This is required to make possible to do multiply gamepad support.

So you can still uses the old KEY() method, if you dont want to use the JOY() commands, but you should newer uses KEY() directly throught, but do its from a function instead. Something like this:

Code: (glbasic) [Select]
FUNCTION getKeyCode: ScanKey
IF ScanKey<0 OR ScanKey>255 THEN RETURN
?IFDEF ANDROID
LOCAL result=0
FOR i=0 TO 4
IF GETJOYNAME$(i)="" THEN RETURN result
result=result+KEY(i)*256
NEXT
RETURN result
?ELSE
RETURN KEY(ScanKey)
?ENDIF
ENDFUNCTION

This make sure all gamepads is got scanned and returned a value, and then you can use a code like this for a button press:

IF getKeyCode(96)=TRUE THEN A_BUTTON=PRESSED

This can works with any one player games and you add a remapping feature if you want. This would do effects for all joypads of course. So been warned, do NOT use KEY() command directly!

A bit of warning: Im do still need to implemented a disconnect features when a gamepads etc can got removed while ingame.

PS. This part is still only hearvy working and needs feedback in this one. Android is a beast when coming to gamecontrollers.
« Last Edit: Yesterday at 06:48 PM by spacefractal »
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2651
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #1 on: 2019-Nov-18 »
Well done Mr Plow, I know what a PITA this stuff can be!  :good:
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3792
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #2 on: 2019-Nov-20 »
please note, the stick order in x-box controllers does NOT follow the google standard, more precision the triggers and the right stick. Im thinks you can add this is line around 557 to perform a auto remapping, im should have do that long time ago:

Code: (glbasic) [Select]
        if (SDLGlbasic.LastJoyname.contains("ouya")) { remapping = "0:1:4:2:3:5:9:9:9:9:"; }
        if (SDLGlbasic.JoyMicrosoftProfile.contains("xbox")) { remapping = "0:1:4:2:3:5:9:9:9:9:"; }
        if (SDLGlbasic.JoyMicrosoftProfile.contains("x-box")) { remapping = "0:1:4:2:3:5:9:9:9:9:"; }

Etc seach after the ouya one as its used the same order as the x-box one as you can see.

btw give me the java file, and im is happy to include and send it to Gernot!

The above is why you should include a remapping, mostly if you want to use theright stick and a triggers. if you only use left stick/dpad and the face buttons, then there should not been in any conflict at all! They does follow  the Google standard quite well, but not allways throught (Ouya example). But most of time they do.

just to been noticed, you are risk to get your account banned to show ads in app enter and app exit. This is not a good user experience anyway at all. Use a Java call. etc do something like this (this is not a real code, but give you idea as not checked my self, all java calls from glbasic is called to glb_JAVACALL in SDLGlbasic.java):

Code: (glbasic) [Select]
        // === SHOW A ADS ===
        if (tokens[0].equals("Interstitial")) {
mInterstitialAd.show();
            return result;
        }
« Last Edit: 2019-Nov-20 by spacefractal »
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1638
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #3 on: 2019-Nov-20 »
Thanks SF

The only changes I made to SDLGlbasic was the Admob stuff - but some user might only want to include it optionally (for paid or educational apps)

I spent over a week on the SDLGlbasic trying to get mInterstitialAd.show() to work, but the Admob would not launch within the SDL process for some reason. The Javacall worked - as I logged it - just didn't have any affect, maybe you know why?

The best way I got it to work was to add a show at the OnResume {} - which is very good and acceptable for Admob policies.

I have removed the onPause instance already...just to be safe.

The loadrequest happens automatically and you can control capping via Admob console.


Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1638
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #4 on: 2019-Nov-20 »
Also SF,

That remapping is already in my file

Code: (glbasic) [Select]
        // **** STICK REMAP ****
// Im guess there is two different major remapping. One is the Google Layout, which
// does not require a remapping, and that one Microsoft using with thier xbox. The
// right stick and triggers conflict with each other. Ouya seens follow xbox.
        if (SDLGlbasic.LastJoyname.contains("ouya")) { remapping = "0:1:4:2:3:5:9:9:9:9:"; }
        if (SDLGlbasic.LastJoyname.contains("xbox")) { remapping = "0:1:4:2:3:5:9:9:9:9:"; }
        if (SDLGlbasic.LastJoyname.contains("x-box")) { remapping = "0:1:4:2:3:5:9:9:9:9:";
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1638
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #5 on: 2020-Feb-10 »
Sorry SF,

I was just Google-ing and looking a possible *lightbulb* solution :)
I spent a couple of weeks at it myself without any joy - so I know how you feel.

I will keep trying  - if I find a workaround I will post it here...


Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3792
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #6 on: 2020-Feb-10 »
the main issue is Activity is gone when trying to call it outside, dispite im passing it. So its look like its a bug found in admob. its just even more annoying as its dont crash correctly or return with any errors at all.
« Last Edit: 2020-Feb-10 by spacefractal »
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3792
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #7 on: 2020-Feb-10 »
actuelly got it working, but want to do this thread clean until mrplow have updated the thread. So all comments should been in the other thread about failed attempt.

Tilt Sensor function have its uses here.
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1638
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #8 on: 2020-Feb-11 »
Updated the notes above  =D

If I need to re-edit to clarify let me know...
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3792
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #9 on: 2020-Feb-11 »
im have edited your post a bit with the downloads while we waiting for the steam update (Step 4). Now Admob is FINALLY working fine. Im will been pushing this to steam soon. But im want this thread to been a bit clean, so im do not regreet im splitted this thread. As least this version working and both me and mrplow got success with it (as lest with the test ads IDS we are using here).

Please Note: The steam version does NOT force Admob if you dont want to use it. Its being checking the grandle file, you using it or not, then its use one of the two java file, where admob is implemeted, while the other one is just using empty classes.
« Last Edit: 2020-Feb-11 by spacefractal »
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3792
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #10 on: 2020-Feb-12 »
there is slight issue on the gamemapping issue. top left and top right has same value :-D. They cant been that.
« Last Edit: 2020-Feb-12 by spacefractal »
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1638
    • View Profile
Re: Adding Admob & Basic Game Controller Support
« Reply #11 on: 2020-Feb-12 »
Oops yes, will change that.
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3792
    • View Profile
    • Space Fractal
Re: Adding Admob & Basic Game Controller Support
« Reply #12 on: 2020-Feb-12 »
Also there was a very slight bug, so the right trigger used wrong constant. That is just fixed. KEYCODE_BUTTON_L2 should have been KEYCODE_BUTTON_R1 in SDLGlbasic.java. Howover this is only happens with Ouya, and joypads that require remapping. Defaults values should works out of the box throught.

Ouya is the only jopad im could auto remap. Im dont own all joypads. If you have such a joypad that dont follow the standard, let me now. All you do is let me know the name of it and all KEY() values its uses. Im should do a simple test app for that.

But howover as long you add a remap feature to your game, all joypads would been useable, regardless which mapping they have. This is IMPORTANT on Android.
« Last Edit: 2020-Feb-12 by spacefractal »
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3792
    • View Profile
    • Space Fractal
im updated the joypad image a little bit as there was one wrong a value and the tilt function. Mr Plow, fell free to clear it up with right font.
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1638
    • View Profile
Will do - once I locate my copy of the image  :D

BTW - I noticed a lot bigger Admob file - you have included other .java files - have they all had modifications in some way?

And there is a jni file update too?

Can you let us know what has changed if it's important...just worried about apps breaking with 10,000+ users etc.
« Last Edit: 2020-Feb-19 by MrPlow »
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs