Libgdx Demo Application With Account Kit Huawei ID Authentication Part 1

Erdal Kaymak
Huawei Developers
Published in
6 min readJan 4, 2022

Demo Application Introduction

In this demo application, we learn what is Libgdx and how to use the basic functions of Libgdx with Account Kit authentication. First, we should create our Game with the gdx_setup.jar file. We can download this jar file on this website after that I explain the Libgdx and Libgdx features and how to use this framework. Finally, we start to create our screens and implement the Account kit for Huawei Id authentication.

Integrating Applications to HMS Core

To start developing an app with Huawei mobile services, you need to integrate your application to the HMS core. Check the link below to integrate your application, also don’t forget to enable the Account Kit from AppGallery Connect.

Libgdx Framework

LibGDX is a cross-platform Java game development framework based on OpenGL that works on Windows, Linux, macOS, Android, your browser, and iOS. LibGDX is a well-proven and reliable framework with a sound base and documentation. Furthermore, there are plenty of games built on top of LibGDX, many of which are open source. It can be developed in Android Studio, Eclipse, Netbeans, IntelliJ IDEA. We use theAndroid Studio for our demo project.

Libgdx Framework Features

Libgdx has lots of features I explain these features below

Audio: Streaming music and sound effect playback for WAV, MP3, and OGG

Input Handling: Abstractions for mouse, keyboard, touchscreen, controllers, accelerometer, gyroscope, and compass. Gesture detection (recognizing taps, panning, flinging, and pinch-zooming)

Math & Physics: 2D physics JNI wrapper for the popular Box2D physics (see also Box2DLights).

Integration of Services: Easy integration of game services, such as Google Play Games, Apple Game Center, and more. Cross-platform API for in-app purchases. Third-party support for Google’s Firebase, the Steamworks API, gameanalytics.com, and Facebook’s Graph API. Easy integration of AdMob

Graphics: Rendering through OpenGL ES 2.0/3.0 on all platforms

Low-Level OpenGL helpers: Meshes, Textures, Shaders, Simple shape rendering

High-level 2D APIs: Orthographic camera, High-performance sprite batching, and caching. Texture atlases, with whitespace stripping support. Either generated offline or at runtime. Bitmap fonts are Either generated offline or loaded from TTF files,2D Particle system TMX tilemap support, and 2D scene-graph API.

A Powerful UI Solution: 2D UI library, based on scene-graph API (Scene2D). We will use this library to create our stage and tables.

Networking: Gdx.net for simple networking (TCP sockets and HTTP requests)

How to use Libgdx Jar File?

Libgdx Project Generator

After downloading the gdx_setup.jar file. And we open it to create our Libgdx project with Libgdx Project Generator. In this section, I explain the Libgdx Project Generator sections below.

Name: The name of the application; lower case with minuses is usually a good idea, e.g. my-game

Package: The Java package under which your code will reside, e.g. com.badlogic.mygame

Game Class: The name of the main game Java class of your app, e.g. MyGame

Destination: The folder where your app will be created

Android SDK: The location of your Android SDK. With Android Studio, to find out where it is, start Android Studio and click “Configure” -> “SDK Manager”. By default it is in /Users/username/Library/Android/SDK

Sub Projects: LibGDX is cross-platform. By default, all the target platforms are included (Desktop; Android; iOS; HTML). There is no need to change the default value unless you are sure you will never compile for a specific target.

Note: iOS projects can only be compiled on macOS.

Extensions

Bullet: 3D Collision Detection and Rigid Body Dynamics Library.

FreeType: Scalable font. Great to manipulate font size dynamically. However, be aware that it does not work with HTML target if you cross-compile for that target.

Tools: Set of tools including particle editor (2d/3d), bitmap font, and image texture packers.

Controller: Library to handle controllers (e.g.: XBox 360 controller).

Box2d: Box2D is a 2D physics library. We check that because I use it in my demo project

Box2dlights: 2D lighting framework that uses box2d for raycasting and OpenGL ES 2.0 for rendering.

Ashley: A tiny entity framework. We check that because I use Ashley in my demo project.

AI: An artificial intelligence framework.

After clicking the generate button we can use Android Studio to open our project with File/Open project and choose the destination of our Libgdx Project.

Libgdx Demo Project Folders

We have three main folders building libgdx application for desktop and android. These folders are android, core, and desktop. In the core folder; we create our game class, game design, all screens, and call all libgdx functions. The game class is called and used by launchers(Android, desktop, etc.) for starting the application. In the android folder, we create our specific functions for the android application. These are Custom Views, Fragments, Activities, Huawei App Galery Services, Google Play Services and etc. In the desktop folder; we can create our libgdx application for Desktop. We can use swing functions and designs in that folder. We have two launchers to open our libgdx application. These are AndroidLauncher for android and DesktopLaucher for desktop. Now we should implement the Account Kit first then we can continue with creating our libgdx menu screens.

Account Kit Introduction

Account Kit provides us with simple, secure, and quick sign-in and authorization functions. Instead of entering accounts and passwords and waiting for authentication, users can just tap the Sign in with HUAWEI ID button to quickly and securely sign in to our app with their HUAWEI IDs.

Assigning Permissions in the Manifest File

The Account SDK requires permissions to obtain the network status and Wi-Fi status. Declare the permissions in the AndroidManifest.xml file as follows:

Preparations for the Code

After adding permission to AndroidManifest.xml we need to add dependency of Account Kit to Project/build.gradle file.

We should define our android specific services under Project/ build.gradle in the android header for Libgdx Application.

We can define the Account Kit dependency in the project(“: android”) dependencies. I use the Dagger Hilt for dependency injections.

Account Kit Class

We should create that class under the android folder.

We create that class for account kit functions. These are ; prepare(), getSignInIntent, loginWithHuaweiId, silentSignIn and signOut

Login Screen has a button as huaweiSignInButton for Login with Huawei Id. When a user clicks it's calling the accountKit.prepare() method and AccountKit class prepares AccountAuthService object via AccountAuthParams object. Also, AccountAuthParams is getting created with DEFAULT_AUTH_REQUEST_PARAM_GAME.

We are calling accountKit.getSignInIntent(), this method returns Huawei AccountAuthService’s signInIntent object. And then we are calling startActivityForResult via that intent object.

There is a default loginWithHuaweiId method. First it is checking requestCode==8888 and then authAccountTask.isSuccessful if both of the results are true, We can say that Login With Huawei Id is completed. For more detail about this feature click here.

We can call accountKit.signOut function to log out our game.

Kit Module Object

We create that module object for the Dagger Hilt dependency injection using our AccountKit class.

We should create an interface for calling our function from the libgdx screens

Account Kit Interface

In this interface, we define the Account kit functions call from Android/ AndroidLauncher and Libgdx screens in the Core folder.

Android Launcher Class

We implement our interface for using account kit functions. That way when we call the interface dot method name(for example interface.onSignOutButtonClicked ) we can trigger the Launcher methods by passing our interface from the constructor.

We will continue the demo application in the next part of this article by creating our Libgdx screens, base screens, and understanding libgdx special terms like the stage, AssetManager, table, Ui skin, Scene 2d, Camera, Sprite Batch, Viewport, etc.

Take Care until next time …

References:

--

--