Sign in Join
Wiki copytemporary

Android/es: what changed

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

OldNewLine
11*(Android only)*
22
33# Android target
44
55Support for the Android target is present starting from FPC 3.0.
66
77CPUs soportadas:
88
99- ARM (`CPU_TARGET=arm`)
1010- x86 (`CPU_TARGET=i386`)
1111- MIPS (`CPU_TARGET=mipsel`)
1212- [[FPC JVM|JVM]] (not discussed on this page)
1313
1414# Android NDK (Native Development Kit)
1515
1616Para obtener las binutils de la plataforma cruzada de Android se necesita descargar e instalar el NDK (Native Development Kit) de Android. Esto permitirá compilar los programas en tu máquina actual teniendo como objetivo Android o dicho de otro modo generará archivos ejecutables bajo Android.
1717
1818La descarga se puede realizar desde el siguiente enlace Android NDK:
1919
2020<https://developer.android.com/ndk/downloads/index.html>
2121
2222A continuación es necesario descomprimirlo a una carpeta.
2323
2424# Descarga de FPC para Android
2525
2626## Release version
2727
2828If 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.
2929
3030For other host platforms you need to download the FPC sources archive and build a cross-compiler as described below.
3131
3232Download the latest release cross-compilers and FPC sources here: <http://freepascal.org/download.var>
3333
3434## Development version
3535
3636The development (trunk) version of FPC may contain new features or bug fixes compared to the latest release version.
3737
3838Check out the latest trunk sources of FPC using the following command:
3939
40-<code>
41-
4240 svn co http://svn.freepascal.org/svn/fpc/trunk fpcsrc
4341
44-</code>
45-
4642Now you have the latest compiler sources in the `fpcsrc` sub-folder.
4743
4844# Building cross compiler
4945
5046You need to have a working installation of FPC 3.0 compiler in order to create the cross-compiler.
5147
5248This 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.
5349
5450Use the similar approach to build a **`i386-android`** or **`mipsel-android`** cross-compiler.
5551
5652Let's assume the following paths:
5753
5854- Android NDK path: `C:\Program Files\Android SDK\android-ndk-r8d`
5955- FPC svn sources path: `C:\Develop\FPC\fpcsrc`
6056- The cross-compiler installation path: `C:\Develop\FPC\pp`
6157
6258Add 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`
6359
6460Open the Command line prompt and `cd` to the root of FPC sources folder: `C:\Develop\FPC\fpcsrc`
6561
6662Ejecuta el siguiente mandato:
6763
68-<code>
69-
70-`make clean crossall crossinstall OS_TARGET=android CPU_TARGET=arm INSTALL_PREFIX=C:\Develop\FPC\pp`</code>
64+`make clean crossall crossinstall OS_TARGET=android CPU_TARGET=arm INSTALL_PREFIX=C:\Develop\FPC\pp`
7165
7266After 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:
7367
74-<code>
75-
76-`make clean crossall crossinstall OS_TARGET=android CPU_TARGET=arm CROSSOPT="-Cparmv7a -Cfvfpv3" INSTALL_PREFIX=C:\Develop\FPC\pp`</code>
68+`make clean crossall crossinstall OS_TARGET=android CPU_TARGET=arm CROSSOPT="-Cparmv7a -Cfvfpv3" INSTALL_PREFIX=C:\Develop\FPC\pp`
7769
7870Now create a new `fpc.cfg` file in folder `C:\Develop\FPC\pp\bin\i386-win32` and paste into it the following text:
7971
80-<code>
81-
8272 -FuC:\Develop\FPC\pp\units\$FPCTARGET*
8373
8474 #ifdef android
8575 #ifdef cpuarm
8676 -FlC:\Program Files\Android SDK\android-ndk-r8d\platforms\android-14\arch-arm\usr\lib
8777 #endif
8878 #ifdef cpu386
8979 -FlC:\Program Files\Android SDK\android-ndk-r8d\platforms\android-14\arch-x86\usr\lib
9080 #endif
9181 #endif
9282
93-</code>
94-
9583# Compiling programs
9684
97-Now, when the cross-compiler is ready, you can compile programs for the Android target: <code>
85+Now, when the cross-compiler is ready, you can compile programs for the Android target:
9886
9987`C:\Develop\FPC\pp\bin\i386-win32\ppcrossarm -Tandroid testprog.pas`
10088
101-</code>
102-
10389# Notas
10490
10591- El compilador **NO define** **`LINUX`** durante la compilación! It defines **`UNIX`** and **`ANDROID`** though.\
10692 (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)`. Actualmente el listado de cosas que son diferentes está solamente aumentando).
10793
10894- Shared libraries do not have **`argc/argv`** available (i.e. establecer a 0 o nil) debido a Android.
10995
11096- La selección de paquetes compilados viene mayoritariamente dada por que librerias vienen incorporadas por defecto con Android, i.e. no muchas.
11197
11298- **\[trunk\]** La unit **System** contiene algunas funciones útiles específicas de Android [funciones útiles específicas de Android](http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/rtl/android/sysandroidh.inc?view=markup).
11399
114100- **\[trunk\]** El compilador y la RTL realizan procesamiento especial para librerias compartidas JNI. El compilador trata una librería como del tipo JNI solamente si dicha librería exporta la función **`JNI_OnLoad`**. POr tanto, si estás creando una librería compartida JNI, entonces siempre exporta `JNI_OnLoad`, incluso si está vacía.
115101
116102- **\[trunk\]** Para librerias compartidas tipo JNI las salidas estandar y de error se redireccionan al log de sistema de Android. Por tanto se puede utilizar `writeln()` para escribir en el log del sistema.
117103
118104# Known issues
119105
120-- 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">**\
121- PIC support for the Android target is present starting from FPC 3.0.2.**</span>\
106+- 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).**\
107+ PIC support for the Android target is present starting from FPC 3.0.2.**\
122108 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.
123109- 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.\
124- <span style="color:green">**This issue is fixed in FPC 3.1.1 (development version).**</span>
110+ **This issue is fixed in FPC 3.1.1 (development version).**
125111- The **clocale** unit is not implemented in FPC 3.0.\
126- <span style="color:green">**It is implemented in FPC 3.1.1 (development version).**</span>
112+ **It is implemented in FPC 3.1.1 (development version).**
127113- The **netdb** unit is unusable in FPC 3.0 due to missing configuration of DNS servers.\
128- <span style="color:green">**This issue is fixed in FPC 3.1.1 (development version).**</span>
114+ **This issue is fixed in FPC 3.1.1 (development version).**
129115- The `Now` function always returns a GMT time in FPC 3.0.\
130- <span style="color:green">**This issue is fixed in FPC 3.1.1 (development version).**</span>
116+ **This issue is fixed in FPC 3.1.1 (development version).**
131117- The following functions return unusable paths in FPC 3.0: `GetTempDir, GetAppConfigDir, GetAppConfigFile, GetUserDir`.\
132- <span style="color:green">**This issue is fixed in FPC 3.1.1 (development version).**</span>
118+ **This issue is fixed in FPC 3.1.1 (development version).**
133119
134120## Freeze during shared library initialization on Android prior 4.1
135121
136122If 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**.
137123
138124Due 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.
139125
140-<span style="color:green">**This issue is fixed in FPC 3.1.1 (development version).**</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()`.
126+**This issue is fixed in FPC 3.1.1 (development version).**: 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()`.
141127
142128Therefore, if you are creating a JNI shared library, always export `JNI_OnLoad`, even if it is empty.
143129
144130Also 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.
145131
146132# See also
147133
148134- [[Android tutorial]]
149135- You can use the [[pas2jni]] utility to easily create JNI shared libraries from your existing code and use it in Android native Java applications.
150136
151137# External Links
152138
153139- [Tutorial "Lazarus and Android"](http://sourceforge.net/projects/laztoapk/files/Lazarus%20and%20Android.pdf/download) (PDF file)
154140
155141---
156142*From the Free Pascal wiki, page "Android/es", as it stood on 2017-03-02 after a change by FTurtle, 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