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

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1717
    • 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 you need a newer version of Android Studio glbasic build, that is now on steam as a beta:
https://www.glbasic.com/forum/index.php?topic=11391.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

Space Fractal: 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 is controllers that works its self on the system navigation as well ouya. Im have tested successful with PS4 and Microsoft One wired controllers on a Android 9 based Android TV. Im do of course cannot guarantee any joypads works of course.

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 with wired controllers on a Android TV might have ID as Player 3, not as Player 1. Be aware of that!

Currectly back, menu and start buttons is NOT supported, howover they do returns a KEY() value throught.

when using Gamecontrollers with KEY():

You can also uses KEY() command for checking values as well. You can respons to controller by KEY() values using the image seens here. Also if you do a remapping/rebind feature, then you could support even more controllers. But this is for single player only as its will been checked by all game controllers by once!

The values a game controller would return, would have those values seens on the image. Sticks can also return a float to the KEY() commands as well. Been beware of that. Howover this is disabled by default, and can been enabled by editing the strings.xml.

You can also uses Tile (using the accelerator sensor), which is routed to the GETJOYX(0) and GETJOYY(0) commands, while Orientation is routed to GETJOYRX(0) and GETJOYRY(0). The later one is the orientation (360 degree) converted to axis.
« Last Edit: 2020-Mar-30 by spacefractal »
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3918
    • View Profile
    • Space Fractal
Im have choosen to clean up this thread as there was too much offypic (that include me, so its generally). This thread is now only about Controller and Admob support. This post might can been edited both by me and MR Plow.

This is due im have been heavy code to get the JOY commands works much better than its was before.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation and The beagle Jam.

Offline SnooPI

  • Dr. Type
  • ****
  • Posts: 343
    • View Profile
Yeah ! It's cool guys!

I think it will help a lot of people  :) ...including me when I get back to Android.

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1717
    • View Profile
SF, the new zip file with android studio has a lot of other files in there...were they really necessary  - did they have vital changes?

Is it a full empty project for copying to the template folder?

Previously what i did was compile then copy in the newer admob files in the new apps 'android studio' folder
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3918
    • View Profile
    • Space Fractal
Use the steam version and enable beta. Its download and update the files soon you enable it. Its pretty much same files as its was with the download. I'm was in lastest version just zipped the whole empty android studio files as I'm could not keep the track of the changed anymore. Steam track that and only download the changes files. Its a 1mb or two and about 8 to 10 files or such.

Also I'm don't support older versions now.

You should uses the newest version of the admob file and the java files. I'm don't gonna to update in a while right now.

You can still use KEY() for control. Howover Joy Is 0 can been a keyboard or a TV remote (you can check that with its joyname). Newer a gamepad. KEY() 0 to 255 do scan through all gamepads at once, due the added multiple gamepad support. This was to not break combatible. Keyscanes is not changed, but be aware for the tilt/orentation (is on id0 you want it or not).

Admob was required to been init a bit later as its might could confuse the resolution surface detection. Also remember it's take a while to init it and game don't wait for it. But should no crash.

Checkout Spot Race. Its use the newest version. Its should also detect chromebook currently as well. My chromebook did not have a touchscreen. So I'm updated the code to support keyboard and mouse, include the second button.
« Last Edit: 2020-Apr-08 by spacefractal »
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation and The beagle Jam.

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1717
    • View Profile
Am I missing something - i updated but do not see anything mentioning a "beta"??

ver 16.252
« Last Edit: 2020-Apr-09 by MrPlow »
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs