Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Working for Android
Note
This guide is intended for Android development on Windows platform, for alternative platforms, check:
Android requires a set of tools to do all the build process to generate an APK:
You can download the JDK fromhere and extract it to a directory. The raylibMakefile.Android scripts expect it to be located atC:\open-jdk.
Actually, the Android SDK consists of several tools, but you can install everything at once by downloadingAndroid Studio and installing the full package.
Alternatively, you can install only the required tools manually. To do this, download the Command line tools package, located at the bottom of thispage.
Decompress the downloaded
sdk-tools-...file into a folder namedandroid-sdk. One of the included tools issdkmanager, a command-line utility for installing the required packages.Open a command prompt, navigate to
android-sdk/tools/bin, and run the following commands:sdkmanager --sdk_root=<path_to_sdk> --updatesdkmanager --sdk_root=<path_to_sdk> --install build-tools;<version>sdkmanager --sdk_root=<path_to_sdk> --install platform-toolssdkmanager --sdk_root=<path_to_sdk> --install platforms;android-<api_level>sdkmanager --sdk_root=<path_to_sdk> --install extras;google;usb_driver
Note
To find available versions and API levels:
sdkmanager --sdk_root=<path_to_sdk> --listTo develop with Raylib in C, you need to install the Android Native Development Kit (NDK).
- You can download it directly fromhere.
- Alternatively, you can install it using
sdkmanager:sdkmanager --sdk_root=<your_path_sdk> --install ndk;<version>
The Android NDK supports multiple Android APIs and architectures (ABIs). The supported ABIs are:armeabi-v7a,arm64-v8a,x86, andx86_64. Support forriscv64 is experimental in the latest NDK versions (seethis for details).
By default, Raylib’s scripts use NDK r21 and expect it to be located atC:\android-ndk.
You can build raylib using eitherCMake orMake. Follow the steps below depending on your preferred method.
- Using CMake
Clone the repository:
git clone https://github.com/raysan5/raylib.gitcd raylibBuild raylib as a static library:
cmake -B Build \ -G "<build_system>" \ -DCMAKE_BUILD_TYPE=MinSizeRel \ -DCMAKE_TOOLCHAIN_FILE=<path_to_ndk>\build\cmake\android.toolchain.cmake \ -DPLATFORM=Android \ -DANDROID_ABI=<abi> \ -DANDROID_PLATFORM=<minimum-api-level> \ -DBUILD_EXAMPLES=OFF # or ON if you want examplescmake --build BuildExplanation of placeholders:
<build_system>→ Your build system generator.Ninjais recommended.<path_to_ndk>→ Full path to your installed Android NDK directory.<abi>→ Target CPU architecture:armeabi-v7a,arm64-v8a,x86, orx86_64.<minimum-api-level>→ Minimum Android API level to support (Seethis for details).
After building, the static library
libraylib.awill be located inBuild/raylib/
Important
When using the NDK toolchain file, the output library always includes debug symbols, even in release mode. This is not an issue if you use the Android Gradle Plugin to package your APK. However, if you are packaging the APK manually, consider stripping unused symbols with NDK'sllvm-strip before including the libraries in your release APK.
Using Make
- Navigate to the source folder:
cd raylib\src - Build the library:
make PLATFORM=PLATFORM_ANDROID \ ANDROID_NDK=<path_to_ndk> \ ANDROID_API_VERSION=<minimum-api-level> \ ANDROID_ARCH=<abi> - By default, the static library
libraylib.awill be generated inraylib/src/
- Navigate to the source folder:
Build raylib as a shared library:
You can build raylib as a shared library by passing the appropriate option for your build system:
- CMake:
-DBUILD_SHARED_LIBS=ON - Make:
RAYLIB_LIBTYPE=SHARED
You might also findthis discussion helpful.
- CMake:
Warning
Your Android device may have a 64-bit CPU, but the installed OS could still be 32-bit. Make sure the selected ABI matches your device architecture.
TheRaymob repository offers an implementation of raylib specifically designed for Android Studio. This template provides a ready-to-use configuration for developing applications using raylib.
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.propertiesfile to define all configuration variables of your project.
- SDK API 33.0.0 and NDK r21 (or higher, compatible with CMake 3.22.1)
To use the Raymob template:
- Clone the GitHub repository to your local machine.
- Open Android Studio and select "Import Project".
- Navigate to the directory where the cloned repository is located and select it for opening.
- Wait for Android Studio to import the project and configure the necessary dependencies.
- Once the import is complete, you can start developing your raylib application in the
app/cppdirectory.
If you already have the NDK and SDK API 33 without Android Studio, you should still be able to compile using thegradlew orgradlew.bat files.
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 modifyinggradle.build orCMakeLists.txt.
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 toapilevels.com). You can also adapt it to target other API versions if necessary.
If you have any questions or encounter issues while using this implementation, you can seek help by submitting an issue on theRaymob repository.
Compiling a raylib game for Android (using aproject template)
Step 1. To build an APK, navigate to directoryraylib-game-template/src/ and editMakefile.Android. Replace thesesettings with yours where apropriate:
ANDROID_ARCH ?= ARMANDROID_API_VERSION = 28JAVA_HOME ?= C:/open-jdkANDROID_HOME ?= C:/android-sdkANDROID_NDK ?= C:/android-ndkANDROID_TOOLCHAIN ?= $(ANDROID_NDK)/toolchains/llvm/prebuilt/windows-x86_64ANDROID_BUILD_TOOLS ?= $(ANDROID_HOME)/build-tools/29.0.3ANDROID_PLATFORM_TOOLS = $(ANDROID_HOME)/platform-toolsThen build the apk with:
mingw32-make PLATFORM=PLATFORM_ANDROIDWARNING: Make sure the MAKE tool to use is properly configured in the Makefile:
# Define default make programMAKE = makeifeq ($(PLATFORM),PLATFORM_DESKTOP) ifeq ($(PLATFORM_OS),WINDOWS)MAKE = mingw32-make endifendififeq ($(PLATFORM),PLATFORM_ANDROID)MAKE = mingw32-makeendif
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.soStep 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.apkStep 4: To view log output from device:
%ANDROID_SDK_TOOLS%\adb logcat -c%ANDROID_SDK_TOOLS%\adb -d logcat raylib:V *:SMy 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_ANDROIDwhen 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.
www.raylib.com | itch.io | GitHub | Discord | YouTube
- Architecture
- Syntax analysis
- Data structures
- Enumerated types
- External dependencies
- GLFW dependency
- libc dependency
- Platforms and graphics
- Input system
- Default shader
- Custom shaders
- Coding conventions
- Integration with other libs
- Working on Windows
- Working on macOS
- Working on GNU Linux
- Working on Chrome OS
- Working on FreeBSD
- Working on Raspberry Pi
- Working for Android
- Working for Web (HTML5)
- Working on exaequOS Web Computer
- Creating Discord Activities
- Working anywhere with CMake
- CMake Build Options
- raylib templates: Get started easily
- How To: Quick C/C++ Setup in Visual Studio 2022, GCC or MinGW
- How To: C# Visual Studio Setup
- How To: VSCode
- How To: Eclipse
- How To: Sublime Text
- How To: Code::Blocks