Support for the Android target is present starting from FPC 3.0.
- ARM (
- x86 (
- MIPS (
- JVM (not discussed on this page)
You need to download and install Android NDK in order to get the cross binutils that compile programs for the android target on your current machine.
Download Android NDK using this link: https://developer.android.com/ndk/downloads/index.html
Extract it to some folder.
If you develop for Android using a Windows machine you can download a setup package which contains cross-compilers for Android (ARM, i386, MIPS). After installation of the cross-compilers you can build binaries for Android without any additional steps.
For other host platforms you need to download the FPC sources archive and build a cross-compiler as described below.
Download the latest release cross-compilers and FPC sources here: http://freepascal.org/download.var
The development (trunk) version of FPC may contain new features or bug fixes compared to the latest release version.
Check out the latest trunk sources of FPC using the following command:
svn co http://svn.freepascal.org/svn/fpc/trunk fpcsrc
Now you have the latest compiler sources in the fpcsrc sub-folder.
Building cross compiler
You need to have a working installation of FPC 3.0 compiler in order to create the cross-compiler.
This tutorial describes how to build a cross-compiler for the
arm-android target on Windows. But the tutorial can be applied to any system with small obvious modifications.
Use the similar approach to build a
Let's assume the following paths:
- Android NDK path:
C:\Program Files\Android SDK\android-ndk-r8d
- FPC svn sources path:
- The cross-compiler installation path:
Add path to Android binutils to the PATH environment variable. In this tutorial the path is:
C:\Program Files\Android SDK\android-ndk-r8d\toolchains\arm-linux-androideabi-4.7\prebuilt\windows\bin
Open the Command line prompt and
cd to the root of FPC sources folder:
Execute the following command:
make clean crossall crossinstall OS_TARGET=android CPU_TARGET=arm INSTALL_PREFIX=C:\Develop\FPC\pp
After that you should have the cross-compiler and units installed in folder
By default all units are built with -O2 optimization switch. Also software FPU emulation is used for ARM CPU by default. If you need to specify different compiler options for building, use the
CROSSOPT parameter. For example if you wish to build units with hardware FPU support and ARMv7a CPU use the following command:
make clean crossall crossinstall OS_TARGET=android CPU_TARGET=arm CROSSOPT="-Cparmv7a -Cfvfpv3" INSTALL_PREFIX=C:\Develop\FPC\pp
Now create a new
fpc.cfg file in folder
C:\Develop\FPC\pp\bin\i386-win32 and paste into it the following text:
-FuC:\Develop\FPC\pp\units\$FPCTARGET\* #ifdef android #ifdef cpuarm -FlC:\Program Files\Android SDK\android-ndk-r8d\platforms\android-14\arch-arm\usr\lib #endif #ifdef cpu386 -FlC:\Program Files\Android SDK\android-ndk-r8d\platforms\android-14\arch-x86\usr\lib #endif #endif
Now, when the cross-compiler is ready, you can compile programs for the Android target:
C:\Develop\FPC\pp\bin\i386-win32\ppcrossarm -Tandroid testprog.pas
- The compiler does NOT define
LINUXduring compilation! It defines
(Background: Android is not completely Linux compatible, e.g. a few syscalls and default library functions e.g. in pthreads, ... are missing, and it generally behaves different in a few ways. This makes the amount of defines manageable by avoiding
if defined(linux) and not defined(android). Actually, the list of things that are different is only growing).
- Shared libraries do not have argc/argv available (i.e. set to 0 or nil) because of Android.
- The selection of compiled packages is mostly driven by what libraries are shipped by default on Android, i.e. not much.
- Shared libraries compiled with FPC 3.0 does not work on Android 6.0+ and executables does not run on Android 5.0+. Starting from Android 6.0 (Marshmallow), it is required to build PIC compatible shared libraries. Starting from Android 5.0 (Lollipop), it is required to build PIC compatible executables (PIE).
PIC support for the Android target is present only in FPC 3.1.1 (development version).
If you need to target Android 6.0+ for shared libraries or target Android 5.0+ for executables, it is needed to build an Android cross compiler from the svn sources of FPC.
NOTE: By default FPC 3.1.1 builds PIC enabled shared libraries and executables for the Android target. PIC shared libraries work on any Android version (tested even on Android 1.5). But PIC executables can run only on Android 4.1+. To run executables on older versions of Android you need to disable PIC by passing the
-Cg-switch to the compiler.
- If you use a cwstring unit in a JNI shared library, your Java application will hang when loading the library on Android 4.0 or earlier. This also applies to any unit which dynamically loads shared libraries during initialization.
This issue is fixed in FPC 3.1.1 (development version).
- clocale unit is not implemented in FPC 3.0.
It is implemented in FPC 3.1.1 (development version).
Nowfunction always returns a GMT time in FPC 3.0.
This issue is fixed FPC 3.1.1 (development version).
- The following functions return unusable paths in FPC 3.0:
GetTempDir, GetAppConfigDir, GetAppConfigFile, GetUserDir.
This issue is fixed FPC 3.1.1 (development version).
- Tutorial "Lazarus and Android" (PDF file)