Android/pl

This is an old revision of Android/pl, edited by Slawek at . The current revision is 144160.

/* Download FPC for Android */ tłumaczenie na j. polski

Wszystkie artykuły dotyczące Androida są wymienione w Portal:Android.

Ten artykuł odnosi się tylko do systemu Android.

Zobacz także: Przewodnik programowania wieloplatformowego

Cel na Androida

Wsparcie dla docelowego systemu Android jest dostępne od FPC 3.0.

Obsługiwane procesory:

Android NDK

Musisz pobrać i zainstalować Android NDK, aby uzyskać binarne narzędzia krzyżowe, które kompilują programy dla systemu Android na Twoim obecnym komputerze.

Pobierz Android NDK, korzystając z tego linku: https://developer.android.com/ndk/downloads/index.html

Wypakuj go do jakiegoś folderu.

Pobierz FPC dla Androida

Wersja wydania

Jeśli programujesz dla systemu Android na komputerze z systemem Windows, możesz pobrać pakiet instalacyjny, który zawiera kompilatory skrośne dla systemu Android (ARM, i386, MIPS). Po zainstalowaniu cross-kompilatorów możesz budować binaria dla Androida bez żadnych dodatkowych czynności.

W przypadku innych platform hosta musisz pobrać archiwum źródeł FPC i zbudować kompilator skrośny, jak opisano poniżej.

Pobierz najnowsze wersje cross-kompilatorów i źródła FPC tutaj: http://freepascal.org/download.html

Wersja rozwojowa/deweloperska

Wersja rozwojowa (trunk) FPC może zawierać nowe funkcje lub poprawki błędów w porównaniu z najnowszą wydaną wersją.

Sprawdź najnowsze źródła trunk FPC za pomocą następującego polecenia:

svn co http://svn.freepascal.org/svn/fpc/trunk fpcsrc

Teraz masz najnowsze źródła kompilatora w podfolderze fpcsrc.

Building cross compiler

You need to have a working installation of the latest version of FPC for your host platform 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 aarch64-android or i386-android or x86_64-android or mipsel-android cross-compiler.

Let's assume the following paths:

Add the path to Android binutils to the PATH environment variable. In this tutorial the path is: C:\Program Files\Android SDK\android-ndk-r12b\toolchains\arm-linux-androideabi-4.9\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 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-r12b\platforms\android-21\arch-arm\usr\lib
#endif
#ifdef cpuaarch64
-FlC:\Program Files\Android SDK\android-ndk-r12b\platforms\android-21\arch-arm64\usr\lib
#endif
#ifdef cpui386
-FlC:\Program Files\Android SDK\android-ndk-r12b\platforms\android-21\arch-x86\usr\lib
#endif
#ifdef cpux86_64
-FlC:\Program Files\Android SDK\android-ndk-r12b\platforms\android-21\arch-x86_64\usr\lib
#endif
#ifdef cpumips32
-FlC:\Program Files\Android SDK\android-ndk-r12b\platforms\android-21\arch-mips\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

Known issues

Freeze during shared library initialization on Android prior 4.1

If your shared library contains a unit which performs dynamic loading of other shared libraries using LoadLibaray() or dlopen(), your library may fail to load on Android 4.0 or earlier. The library loading will freeze in such case. One of the units, which can trigger the issue is cwstring.

Due to a bug in Android versions prior to 4.1, the dlopen() API function does not support recursive calls. When a shared library is being loaded by JVM, using dlopen(), the library initialization is executed, including initialization code of all units in the library. If a unit initialization code calls dlopen() - it simply hangs, since the initial dlopen() is not finished yet and recursion is not allowed.

This issue is fixed in FPC 3.2.0: To workaround this issue the compiler uses the following trick: If a library exports JNI_OnLoad(), then no unit initialization is performed during library load. The initialization is called when JVM has loaded the library and then calls JNI_OnLoad().

Therefore, if you are creating a JNI shared library, always export JNI_OnLoad, even if it is empty.

Also you can trigger this bug if you have a JNI shared library, which use other shared library, created using FPC. The loading of this second library may hang. To workaround this, you need to export an empty JNI_OnLoad() function in the second library. Then you need to call JNI_OnLoad() from the initialization of the first library.

See also