Difference between revisions of "Android"

From Free Pascal wiki
m (Android for ARM)
m (Android for ARM)
Line 95: Line 95:
 
== Android for ARM ==
 
== Android for ARM ==
  
By default FPC generates <code>ARMv6</code> code for the <code>arm-android</code> target, while 1.x and 2.x versions of Android support the <code>ARMv5T</code> CPU. There have been few real devices with the <code>ARMv5T</code> CPUs. But emulator images for Android 1.x and 2.x use <code>ARMv5T</code> CPU. If need to test your application on old Android versions using the emulator or wish to support <code>ARMv5T</code> devices, you need to build an arm-android cross compiler with the <code>-CpARMv5T</code> switch.
+
By default FPC generates <code>ARMv6</code> code for the <code>arm-android</code> target, while 1.x and 2.x versions of Android support the <code>ARMv5T</code> CPU. There have been few real devices with the ARMv5T CPUs. But emulator images for Android 1.x and 2.x use ARMv5T CPU. If need to test your application on old Android versions using the emulator or wish to support ARMv5T devices, you need to build an arm-android cross compiler with the <code>-CpARMv5T</code> switch.
  
 
= Known issues =
 
= Known issues =

Revision as of 12:28, 13 October 2015

Android robot.svg

This article applies to Android only.

See also: Multiplatform Programming Guide

Android target

Support for the Android target is present starting from FPC 3.0.

Supported CPUs:

  • ARM (CPU_TARGET=arm)
  • x86 (CPU_TARGET=i386)
  • MIPS (CPU_TARGET=mipsel)
  • JVM (not discussed on this page)

Android NDK

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: http://developer.android.com/tools/sdk/ndk/index.html

Extract it to some folder.

FPC sources

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 2.6.4 or 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 i386-android or mipsel-android cross-compiler.

Let's assume the following paths:

  • Android NDK path: C:\Program Files\Android SDK\android-ndk-r8d
  • FPC svn sources path: C:\Develop\FPC\fpcsrc
  • The cross-compiler installation path: C:\Develop\FPC\pp

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: C:\Develop\FPC\fpcsrc

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 C:\Develop\FPC\pp 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 use the following command:

make clean crossall crossinstall OS_TARGET=android CPU_TARGET=arm CROSSOPT="-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

Compiling programs

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

Notes

General

  • The compiler does NOT define LINUX during compilation! It defines UNIX and ANDROID though.

(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.

Android for ARM

By default FPC generates ARMv6 code for the arm-android target, while 1.x and 2.x versions of Android support the ARMv5T CPU. There have been few real devices with the ARMv5T CPUs. But emulator images for Android 1.x and 2.x use ARMv5T CPU. If need to test your application on old Android versions using the emulator or wish to support ARMv5T devices, you need to build an arm-android cross compiler with the -CpARMv5T switch.

Known issues

  • clocale unit is not implemented yet.

See also

External Links