[Tutorial][Android] Building Irrlicht Example 01 with Gradle

A forum to store posts deemed exceptionally wise and useful

[Tutorial][Android] Building Irrlicht Example 01 with Gradle

Postby LunaRebirth » Mon Jan 29, 2018 10:14 pm

Building Irrlicht Example 01 with Gradle

Irrlicht (OGL-ES): https://sourceforge.net/p/irrlicht/code/HEAD/tree/branches/ogl-es/
Cygwin: https://www.cygwin.com/
Android Studio: https://developer.android.com/studio/index.html
Android NDK & SDK: (available for download in Android Studio)

1. Compile Irrlicht
*Before this step, make sure you have already downloaded Android Studio, the NDK and SDK.

Download the OGL-ES snapshot from the link given above.
Extract it to any location.
Open Cygwin, and cd to your Irrlicht directory.
"cd IrrlichtPath/source/Irrlicht/Android"
In Cygwin, do "ndk-build"
This may take a while to compile Irrlicht for different targets. In the end, you should see these folders in location "IrrlichtPath/source/Irrlicht/Android/obj/local/":
"arm64-v8a", "armeabi", "armeabi-v7a", "mips64", "x86", "x86_64".

*I mention the required targets because the OGL-ES branch I have to try compiling twice before I get the "x86" folder. I'm unsure why.

2. Create an Android Studio project
In Android Studio, press File > New > New Project.
Give your project any name and location you want. Select "Include C++ Support" and a minimum SDK (I'm using the default value -- currently 16).
Press Next, and choose the "Empty Activity". UNCHECK "Generate Layout File" and "Backwards Compatibility (AppCompat)". Press "Finish" to create the project.

3. Android Studio Project Files
Firstly, in Android Studio, go to app > cpp. Right-click "native-lib.cpp" and delete it from the project.

In Android Studio's project browser, open app > java > (YOUR PROJECT) > MainActivity. Edit it to look like this:
cpp Code: Select all
import android.app.NativeActivity;
import android.os.Bundle;
public class MainActivity extends NativeActivity {
    public void onCreate(Bundle savedInstanceState) {

(Don't forget to change the package name to your project's package).

Now go to app > manifests. Edit AndroidManifest.xml to look like this:
cpp Code: Select all
<?xml version="1.0" encoding="utf-8"?>
                android:value="native-lib" />
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

(You may need to change android:name=".MainActivity" to android:name="com.yourpackage.MainActivity", though you probably won't have to).

Press the drop-down for "External Build Files" in Android Studio. Edit CMakeLists.txt to look like this:
cpp Code: Select all
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
             # Sets the library as a shared library.
             # Provides a relative path to your source file(s).
             # Associated headers in the same location as their source
             # file are automatically included.
             ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )
add_library( # Sets the name of the library.
             # Sets the library as a shared library.
             # Provides a relative path to your source file(s).
             # Associated headers in the same location as their source
             # file are automatically included.
             src/main/cpp/android_tools.cpp )
# Specifies a path to native header files.
#>>>    NOTE: The above should be YOUR Irrlicht include directory. This is mine.
#>>>          The path starts in the "app" directory of your project.
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( native-lib
#>>>    NOTE: Just like above, this should be your path. This one is mine.
#>>>          Links to the appropriate ABI. CAREFUL: You must have >ALL< Irrlicht ABIs built ("arm64-v8a", "armeabi", "armeabi-v7a", "mips64", "x86", "x86_64") otherwise you may run into installation errors on your virtual device.

(NOTE: I placed important messages in comments that start with "#>>>". Make sure to read them)

Now we need to make sure that we get the Irrlicht media packaged into the program for the HelloWorld example.
Open app > build.gradle and add this to the end of the file. Do not replace anything in this file.
cpp Code: Select all
copy {
    from "../../irrlicht"
    into "src/main/assets"
    include 'media/**'

(Note that you may need to change the irrlicht directory. This is where I placed mine.
In the end, this should create an assets folder with Irrlicht's media files into your Android Studio project path "app/src/main")

4. Use the Irrlicht example code
Inside of your Irrlicht OGL-ES snapshot download, there is a folder "IrrlichtPath/examples/01.HelloWorld_Android".
In this folder, copy these files:
"android_tools.cpp", "android_tools.h", and "main.cpp".
Paste these into your Android Studio project path under directory "app/src/main/cpp"

5. Run your project
At this point, if you haven't synced your project, your project may say it's missing some files (like android_native_app_glue.h). This is fine, Android Studio should fine it once it builds your project.

Open the AVD Manager (press the icon in Android Studio at the top that looks like a phone with the Android symbol on it).
If you don't have any virtual devices, create one and run it.
If you do have a virtual device: in the right-hand side of the AVD Manager window is an "Actions" column with a drop-down arrow. Select it, and press "Cold Boot Now"

*Important NOTE: "Cold Boot Now" is not required, but I've been having a lot of issues restarting Android Studio emulators. While looking around on Google, it appears this is quite common. I was getting error "Client couldn't connect within 7 seconds..." without doing a cold boot.

Once your device starts up and is functional, go to Android Studio and select Run > Run 'app'.
After your project builds, it should automatically open and run the Irrlicht program on the virtual device.


If you get error "undefined symbol: ANativeActivity_onCreate", it's likely that you removed "app_dummy()" from the Irrlicht example. This error is wierd, because Android Studio puts a strike through this function and lists it as "deprecated," but it's still actually needed by Irrlicht.
Posts: 316
Joined: Sun May 11, 2014 12:13 am

Return to FAQs, Tutorials, Howtos, and external tool lists

Who is online

Users browsing this forum: No registered users and 1 guest