How to set up Allegro 5 Library for Android Development

Okay, so in this post, I'm going to be showing you how to set up the Allegro 5 Library for game development on the Android platform. This will be particularly useful for those who are having difficulty setting up the environment for deploying your games made in Allegro 5. Later, I'll be making a couple of tutorial series that will show how to make Allegro 5 games from scratch for those who are just starting out.

I already made a video on this topic, and if you prefer to follow a video tutorial instead of reading this stuff, then you can watch the video below.


Without further ado, let's get started with the requirements.
Requirements
  • Android Studio
  • Android SDK with the following installed packages:
    • Android NDK (make sure you take the latest version)
    • CMake
    • LLDB
Once you have all these ready, we can start.
First, we need to download the Allegro 5 libraries for android. So navigate to the maven repository as shown in the screenshot below.
Click to enlarge.
Click either allegro5-debug or allegro5-release (whichever one you want, but you can just use the release version) and then click on the Files tab.
Scroll to the end and click on the latest version of Allegro 5 library.
Click to enlarge.
Select the file that ends with the ".aar" file extension to download it.
Click to enlarge.
Once the file is downloaded, you need to open the archive with a zip utility (7-Zip should do), or you can just rename the file extension from .aar to .zip and Windows Explorer will treat the file as a zip file and will let you view and extract its content. Once you open up the archive, you'll find the following files and directories:
Click to enlarge
Leave this open and let's go back to the browser for a bit.
Click on this link and enter whichever directory of the version of Allegro 5 you downloaded earlier. Click on the allegro_jni_includes.zip file to download it (or download the debug version if that's what you want).
Click to enlarge
Once the file is finished downloading, open it up with Windows Explorer or any program that can open zip files and navigate inside the directory structure of the zip file until you find two folders and a file as shown in the screenshot below:
Click to enlarge
Now, leave these as they are and launch Android studio (if you haven't already).
Create a new project as shown below:
Click to enlarge
Select Native C++ and Click Next.
Click to enlarge
Give your project a name, set the package name, Save Location and Minimum API Level, then click Next.

Click Next again, then Finish.

Once the project is done loading and gradle has finished doing its stuff, go back to the aar file we downloaded earlier. Click on the classes.jar file and extract it to YourProjectFolder/app/libs/ folder. Switch to the Project View on the project pane and make sure the classes.jar is inside the directory structure as shown in the image below:
Click to enlarge
Right-click on the classes.jar file and select Add As Library in the pop-up menu. Another dialog will be displayed asking for the module to add the library to. The app module will be selected by default, so just click OK. Then let Gradle build it's stuff.
Once Gradle is done, go to the second file we downloaded (the allegro_jni_includes.zip file) which we opened up. First, take the allegro.cmake file and the jniIncludes folder and extract them into YourProjectFolder/app/src/main/cpp as shown in the screenshot below.
Click to enlarge
Open up the allegro.cmake file. Go to where you find: include_directories(${JNI_FOLDER}/jniIncludes) and change it to include_directories(${CMAKE_CURRENT_SOURCE_DIR}/jniIncludes).

Now, open up CMakeLists.txt and delete the everything inside the file. Then copy and paste the code below inside the CMakeLists.txt file:
Now go into the zip file we downloaded (the allegro_jni_includes.zip file) and extract the jniLibs folder into YourProjectFolder/app/src/main folder.
Once the extraction is complete, close the zip file, we're done with it.

Now, open up MainActivity.java and replace the contents with the following file (except line 1). Paste the following inside the file:

Now, open up native-lib.cpp inside the cpp folder, delete its contents and paste the following:

Build the project by going to Build --> Make Project to make sure everything works fine. If everything goes well, no errors should be reported. Also try to build an apk to make sure that you're set up correctly.

Lastly, open up AndroidManifest.xml and add an attribute into the application tag. It looks like this:
android:configChanges="orientation|keyboardHidden|screenLayout|uiMode|screenSize".
Absence of this might make your game behave in weird ways when running your game, especially when you minimize it, or flip the screen.

That's all for now, and I'm going to be making a couple of tutorials and (maybe) start a couple of series to help beginners start working on making games with C++ and Allegro 5.
If you've got any questions, post it in the comments section below and I'll be sure to reply.

Comments

  1. This is a very great tutorial. The combination video + text tutorials give the result of which anyone can understand. CONGRATULATIONS ! Now, I would like that you explain me how to draw an image and how to play a sound using Allegro 5 in Android Studio. The way I used to do for desktop game doesn't work for Android game. Where to place the images and sounds in the project ? How I can specify the place of these files when I call a function to draw an image or to play a sound ? Thanks in advance for your precious help that I am waiting for with impatience.

    ReplyDelete

Post a Comment

Popular posts from this blog

Assembly Language Tutorial For Beginners Part 1

How to Export MySQL Database (Command Line) to a File