Sign in Join
Wiki copytemporary

Android/pl: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Android/pl

OldNewLine
1+Wszystkie artykuły dotyczące Androida są wymienione w [[Portal:Android]].
2+
3+*(Android only)*
4+
5+## Cel na Androida
6+
7+Wsparcie dla docelowego systemu Android jest dostępne od FPC 3.0.
8+
9+Obsługiwane procesory:
10+
11+- ARM (`CPU_TARGET=arm`)
12+- x86 (`CPU_TARGET=i386`)
13+- MIPS (`CPU_TARGET=mipsel`)
14+- ARM64 (`CPU_TARGET=aaarch64`) - w FPC 3.2+
15+- x86-64 (`CPU_TARGET=x86_64`) - w FPC 3.2+
16+- [[FPC JVM|JVM]] (nie omówione na tej stronie)
17+
18+## Android NDK
19+
20+Musisz pobrać i zainstalować Android NDK, aby uzyskać binarne narzędzia krzyżowe, które kompilują programy dla systemu Android na Twoim obecnym komputerze.
21+
22+Pobierz Android NDK, korzystając z tego linku: <https://developer.android.com/ndk/downloads/index.html>
23+
24+Wypakuj go do jakiegoś folderu.
25+
26+## Pobierz FPC dla Androida
27+
28+### Wersja wydania
29+
30+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.
31+
32+W przypadku innych platform hosta musisz pobrać archiwum źródeł FPC i zbudować kompilator skrośny, jak opisano poniżej.
33+
34+Pobierz najnowsze wersje cross-kompilatorów i źródła FPC tutaj: <http://freepascal.org/download.html>
35+
36+### Wersja rozwojowa/deweloperska
37+
38+Wersja rozwojowa (trunk) FPC może zawierać nowe funkcje lub poprawki błędów w porównaniu z najnowszą wydaną wersją.
39+
40+Sprawdź najnowsze źródła trunk FPC za pomocą następującego polecenia:
41+
42+`svn co http://svn.freepascal.org/svn/fpc/trunk fpcsrc`
43+
44+Teraz masz najnowsze źródła kompilatora w podfolderze `fpcsrc`.
45+
46+## Budowanie kompilatora skrośnego
47+
48+Aby utworzyć kompilator skrośny, musisz mieć działającą instalację najnowszej wersji FPC dla swojej platformy hosta.
49+
50+W tym samouczku opisano, jak zbudować kompilator skrośny docelowo dla**`arm-android`** w systemie Windows. Ale samouczek można zastosować do dowolnego systemu z niewielkimi oczywistymi modyfikacjami.
51+
52+Use the similar approach to build a **`aarch64-android`** or **`i386-android`** or **`x86_64-android`** or **`mipsel-android`** cross-compiler.
53+
54+Użyj podobnego podejścia, aby zbudować kompilator skrośny **`aarch64-android`**, **`i386-android`**, **`x86_64-android`** lub **`mipsel-android`**.
55+
56+Załóżmy, że mamy następujące ścieżki:
57+
58+- Ścieżka Android NDK: `C:\Program Files\Android SDK\android-ndk-r12b`
59+- Ścieżka źródeł FPC svn: `C:\Develop\FPC\fpcsrc`
60+- Ścieżka instalacji kompilatora skrośnego: `C:\Develop\FPC\pp`
61+
62+Dodaj ścieżkę do binutils systemu Android, do zmiennej środowiskowej PATH. W tym samouczku ta ścieżka to: `C:\Program Files\Android SDK\android-ndk-r12b\toolchains\arm-linux-androideabi-4.9\prebuilt\windows\bin`
63+
64+Otwórz wiersz poleceń i przejdź do katalogu głównego folderu źródeł FPC: `cd C:\Develop\FPC\fpcsrc`
65+
66+Wykonaj następujące polecenie:
67+
68+`make clean crossall crossinstall OS_TARGET=android CPU_TARGET=arm INSTALL_PREFIX=C:\Develop\FPC\pp`
69+
70+Następnie powinieneś mieć zainstalowany kompilator skrośny i moduły w folderze `C:\Develop\FPC\pp`. Domyślnie wszystkie moduły są zbudowane z przełącznikiem optymalizacji -O2. Również programowa emulacja FPU jest używana domyślnie dla procesora ARM. Jeśli musisz określić różne opcje kompilatora do kompilacji, użyj parametru `CROSSOPT`. Na przykład, jeśli chcesz zbudować moduł ze sprzętową obsługą FPU i procesorem ARMv7a, użyj następującego polecenia:
71+
72+`make clean crossall crossinstall OS_TARGET=android CPU_TARGET=arm CROSSOPT="-Cparmv7a -Cfvfpv3" INSTALL_PREFIX=C:\Develop\FPC\pp`
73+
74+Teraz utwórz nowy plik `fpc.cfg` w folderze `C:\Develop\FPC\pp\bin\i386-win32` i wklej do niego następujący tekst:
75+
76+ -FuC:\Develop\FPC\pp\units\$FPCTARGET*
77+
78+ #ifdef android
79+ #ifdef cpuarm
80+ -FlC:\Program Files\Android SDK\android-ndk-r12b\platforms\android-21\arch-arm\usr\lib
81+ #endif
82+ #ifdef cpuaarch64
83+ -FlC:\Program Files\Android SDK\android-ndk-r12b\platforms\android-21\arch-arm64\usr\lib
84+ #endif
85+ #ifdef cpui386
86+ -FlC:\Program Files\Android SDK\android-ndk-r12b\platforms\android-21\arch-x86\usr\lib
87+ #endif
88+ #ifdef cpux86_64
89+ -FlC:\Program Files\Android SDK\android-ndk-r12b\platforms\android-21\arch-x86_64\usr\lib
90+ #endif
91+ #ifdef cpumips32
92+ -FlC:\Program Files\Android SDK\android-ndk-r12b\platforms\android-21\arch-mips\usr\lib
93+ #endif
94+ #endif
95+
96+## Compiling programs
97+
98+Now, when the cross-compiler is ready, you can compile programs for the Android target:
99+
100+`C:\Develop\FPC\pp\bin\i386-win32\ppcrossarm -Tandroid testprog.pas`
101+
102+## Notes
103+
104+- The compiler does NOT define **`LINUX`** during compilation! It defines **`UNIX`** and **`ANDROID`** though.\
105+ (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).
106+
107+- Shared libraries do not have **`argc/argv`** available (i.e. set to 0 or nil) because of Android.
108+
109+- The **System** unit contains some useful [Android-specific functions](https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/rtl/android/sysandroidh.inc?view=markup).
110+
111+- The compiler and RTL do special processing for JNI shared libraries. The compiler treats a library as a JNI library only if the library exports the **`JNI_OnLoad`** function. Therefore, if you are creating a JNI shared library, always export `JNI_OnLoad`, even if it is empty.
112+
113+- For JNI shared libraries standard output and error output are redirected to the Android system log. Therefore you can use `writeln()` to write to the system log.
114+
115+- By default FPC 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.
116+
117+## Known issues
118+
119+- 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).<span style="color:green">**\
120+ PIC support for the Android target is present starting from FPC 3.0.2.**</span>\
121+ NOTE: By default FPC 3.0.2+ 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.
122+- Shared libraries compiled with FPC 3.0.x does not work on Android 8.0+ when an application targets API 26+. This is caused by usage of syscalls which are blocked by SECCOMP starting from Android 8.\
123+ <span style="color:green">**This issue is fixed in FPC 3.2.**</span>
124+- If you use the **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. See more information below.\
125+ <span style="color:green">**This issue is fixed in FPC 3.2.**</span>
126+- The **clocale** unit is not implemented in FPC 3.0.\
127+ <span style="color:green">**It is implemented in FPC 3.2.**</span>
128+- The **netdb** unit is unusable in FPC 3.0 due to missing configuration of DNS servers.\
129+ <span style="color:green">**This issue is fixed in FPC 3.2.**</span>
130+- The `Now` function always returns a GMT time in FPC 3.0.\
131+ <span style="color:green">**This issue is fixed in FPC 3.2.**</span>
132+- The following functions return unusable paths in FPC 3.0: `GetTempDir, GetAppConfigDir, GetAppConfigFile, GetUserDir`.\
133+ <span style="color:green">**This issue is fixed in FPC 3.2.**</span>
134+
135+## Freeze during shared library initialization on Android prior 4.1
136+
137+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**.
138+
139+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.
140+
141+<span style="color:green">**This issue is fixed in FPC 3.2.0**</span>: 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()`.
142+
143+Therefore, if you are creating a JNI shared library, always export `JNI_OnLoad`, even if it is empty.
144+
145+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.
146+
147+## See also
148+
149+- [[Android tutorial]]
150+- You can use the [[pas2jni]] utility to easily create JNI shared libraries from your existing code and use it in Android native Java applications.
151+- [[Cross compiling]] for cross compiling from Linux, macOS and Windows along with other useful cross-compilation information.
152+
153+## External links
154+
155+- [Tutorial "Lazarus and Android"](http://sourceforge.net/projects/laztoapk/files/Lazarus%20and%20Android.pdf/download) (PDF file)
156+- [How to create Android apps with Lazarus](https://pascaldelphilazarus.wordpress.com/2014/06/02/how-to-create-android-apps-with-lazarus/)
157+- [How to setup cross-compilation to Android](https://forum.lazarus.freepascal.org/index.php/topic,32553.0.html) (Lazarus Forum)
158+
159+---
160+*From the Free Pascal wiki, page "Android/pl", as it stood on 2021-11-03 after a change by Slawek, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history