Skip to content

Working for Android

Le Juez Victor edited this page Jun 21, 2023 · 25 revisions

NOTE: This guide is intended for Android development on Windows platform, for alternative platforms, check:

Installing required tools

Android requires a set of tools to do all the build process to generate a .APK game.

1. Open JDK

You can just download the JDK from here and decompress it in a directory, raylib Makefile.Android scripts looks for it on C:\open-jdk.

2. Android SDK

Actually, Android SDK is composed by a series of tools, you can install everything in a go just downloading Android Studio and installing the full package.

Alternatively, you can just install the required files manually. To do that, start downloading Command line tools package, found at the end of this page.

  • Decompress downloaded sdk-tools-... file into a folder named android-sdk. One of the included tools is the sdkmanager, a command line utility to install required packages.
  • From command line, navigate to android-sdk/tools/bin and execute the following commands:
sdkmanager --sdk_root=<your_path_sdk> --update
sdkmanager --sdk_root=<your_path_sdk> --list
sdkmanager --sdk_root=<your_path_sdk> --install build-tools;29.0.3
sdkmanager --sdk_root=<your_path_sdk> --install platform-tools
sdkmanager --sdk_root=<your_path_sdk> --install platforms;android-28
sdkmanager --sdk_root=<your_path_sdk> --install extras;google;usb_driver

With those commands you're installing all required tools. It includes: tools, build-tools, platform-tools, platforms\android-28 api level and also extras\google\usb_driver (that you need to install to connect to your device).

3. Android NDK

To develop with raylib in C, we need to install the Android Native Development Kit. You can download it from here. Android NDK includes support files for multiple Android APIs and multiple architectures. raylib scripts expect to use NDK r21 and found it available on C:\android-ndk.

Compiling raylib source code

To compile raylib sources, just navigate from command line to directory raylib/src/ and execute Makefile with:

mingw32-make PLATFORM=PLATFORM_ANDROID

NOTE: libraylib.a will be generated in raylib/src directory by default.

WARNING: Maybe your Android device uses a 64bit CPU but be careful, installed Android OS could still be 32bit, not allowing 64bit apps.

Implementation for Android Studio

The Raymob repository offers an implementation of raylib specifically designed for Android Studio. This template provides a ready-to-use configuration for developing applications using raylib.

Implementation Contents

This implementation for Android Studio includes the following:

  • The source code of the latest stable version of raylib directly integrated.
  • A preconfigured project structure to facilitate raylib application development.
  • Single gradle.properties file to define all configuration variables of your project.

Prerequisites

  • SDK API 33.0.0 and NDK r21 (or higher, compatible with CMake 3.22.1)

Usage

To use the Raymob template:

  1. Clone the GitHub repository to your local machine.
  2. Open Android Studio and select "Import Project".
  3. Navigate to the directory where the cloned repository is located and select it for opening.
  4. Wait for Android Studio to import the project and configure the necessary dependencies.
  5. Once the import is complete, you can start developing your raylib application in the app/cpp directory.

If you already have the NDK and SDK API 33 without Android Studio, you should still be able to compile using the gradlew or gradlew.bat files.

Customization

The Raymob template can be customized to fit the specific needs of your project. You can add additional dependencies or libraries to the project by modifying gradle.build or CMakeLists.txt.

Compatibility

By default, the Raymob template targets APIs 24 to 33, which corresponds to Android 7.0 to Android 13 (so 96.2% of devices according to apilevels.com). You can also adapt it to target other API versions if necessary.

Support

If you have any questions or encounter issues while using this implementation, you can seek help by submitting an issue on the Raymob repository.

Compiling a raylib game for Android (using a project template)

Step 1. To build an APK, navigate to directory raylib-game-template/src/ and edit Makefile.Android. Replace these settings with yours where apropriate:

ANDROID_ARCH           ?= ARM
ANDROID_API_VERSION     = 28

JAVA_HOME              ?= C:/open-jdk
ANDROID_HOME           ?= C:/android-sdk
ANDROID_NDK            ?= C:/android-ndk
ANDROID_TOOLCHAIN      ?= $(ANDROID_NDK)/toolchains/llvm/prebuilt/windows-x86_64
ANDROID_BUILD_TOOLS    ?= $(ANDROID_HOME)/build-tools/29.0.3
ANDROID_PLATFORM_TOOLS  = $(ANDROID_HOME)/platform-tools

Then build the apk with:

mingw32-make PLATFORM=PLATFORM_ANDROID

WARNING: Make sure the MAKE tool to use is properly configured in the Makefile:

# Define default make program
MAKE = make

ifeq ($(PLATFORM),PLATFORM_DESKTOP)
    ifeq ($(PLATFORM_OS),WINDOWS)
        MAKE = mingw32-make
    endif
endif
ifeq ($(PLATFORM),PLATFORM_ANDROID)
    MAKE = mingw32-make
endif

WARNING: If compiled project is developed in C++ instead of C, there are additional considerations:

To compile the game into a shared library (libmain.so) the following flags are required:

  • Compile C with clang: -std=c99 -lc
  • Compile C++ with clang++: -std=c++11 -lc++

Add required shared libraries to the generated APK:

  • Compiling in C:
  $BUILD_TOOLS/aapt add $__OUTP/$NAME.apk lib/$ABI/libmain.so
  • Compiling in C++:
  cp $TOOLCHAIN/../../../../sources/cxx-stl/llvm-libc++/libs/$ABI/libc++_shared.so lib/$ABI/libc++_shared.so
  $BUILD_TOOLS/aapt add $__OUTP/$NAME.apk lib/$ABI/libc++_shared.so
  $BUILD_TOOLS/aapt add $__OUTP/$NAME.apk lib/$ABI/libmain.so

Step 2: To install the APK into connected device (previously intalled drivers and activated USB debug mode on device):

%ANDROID_SDK_TOOLS%\adb install simple_game.apk

Step 4: To view log output from device:

%ANDROID_SDK_TOOLS%\adb logcat -c
%ANDROID_SDK_TOOLS%\adb -d  logcat raylib:V *:S

Generate APK using raylib-game-template. (edited on 2022.07.30)

My pc os is win8.1 x64

_Step 1.need to download these (需要下载这些)

    msys2-x86-64 https://www.msys2.org/
        run command in msys:
        pacman -S mingw-w64-i686-toolchain (select default 选择默认的)
        pacman -S mingw-w64-x86_64-make
  
    android-studio https://developer.android.com/studio
        use SDK Manager download these:
        sdk with android-29
        NDK 25.0.8775105
        build tools 29.0.3
        platform-tools

    openjdk-8u332-b09-windows-64 https://www.openlogic.com/openjdk-downloads

    raylib https://github.com/raysan5/raylib

    raylib-game-template https://github.com/raysan5/raylib-game-template

_Step 2.confirm the installation path (确认安装路径)

    mingw-w64 C:\msys64\mingw64\bin
    NDK D:\Program_Files\android_sdk\ndk\25.0.8775105
    openjdk D:\Program_Files\openjdk
    android sdk D:\Program_Files\android_sdk
    raylib C:\raylib
    raylib-game-template D:\raylib-game-template-main

_Step 3.set environment variables (设置环境变量)

    JAVA_HOME D:\Program_Files\openjdk
    Path C:\msys64\mingw64\bin

_Step 4.edit makefile and makefile.android (编辑makefle和makefile.android)

    c:\raylib\src\Makefile
        line 203 ANDROID_NDK ?= D:/Program_Files/android_sdk/ndk/25.0.8775105
        line 197 ANDROID_ARCH ?= arm64
        line 198 ANDROID_API_VERSION ?= 29

    d:\raylib-game-template-main\src\Makefile.Android
        line 28 RAYLIB_PATH            ?= C:\raylib
        line 33 ANDROID_ARCH           ?= ARM64
        line 34 ANDROID_API_VERSION     = 29
        line 51 JAVA_HOME              ?= D:/Program_Files/openjdk
        line 52 ANDROID_HOME           ?= D:/Program_Files/android_sdk
        line 53 ANDROID_NDK            ?= D:/Program_Files/android_sdk/ndk/25.0.8775105
        line 55 ANDROID_BUILD_TOOLS    ?= $(ANDROID_HOME)/build-tools/29.0.3

_Step 5.generate libraylib.a for android (生成安卓平台的libraylib.a)

    in c:\raylib\src
    run command: 
        mingw32-make PLATFORM=PLATFORM_ANDROID

_Step 6.generate APK (生成APK)

    in d:\raylib-game-template-main\src
    run command: 
        mingw32-make PLATFORM=PLATFORM_ANDROID

when compiling,if report "fatal error: 'asm/types.h' file not found ", you need copy "asm-generic" and renamed as "asm". (the path "D:\Program_Files\android_sdk\ndk\25.0.8775105\toolchains\llvm\prebuilt\windows-x86_64\sysroot\usr\include\asm-generic")

If you have any doubt, just let me know.

Clone this wiki locally