Sign in Join
Wiki copytemporary

Custom Drawn Interface/Android: what changed

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

OldNewLine
11*(Android only)*
22
33> **Warning:** THIS INFORMATION IS DATED AND NEEDS UPDATING
44
55Go back to [[Custom Drawn Interface]]
66
77## Architecture
88
99LCL-CustomDrawn-Android utilizes a minimal Java application which communicates with our Pascal library and sends all events to it and also obeys commands from it. The communication is done via JNI as supported by Google. The controls are not native, instead they are all drawing using TRawImage+TLazIntfImage+TLazCanvas and events and the painting are clipped using LazRegions. The drawing itself is done in Pascal using jnigraphics to draw on a Bitmap Java object which is then drawn by the minimal Java activity on a SurfaceView.
1010
1111Our previous and short-lived attempt to write an LCL Interface for Android (LCL-Android) utilized a non-standard method of using native executables which communicated with a Java machine via Pipes. This method was abandoned because it was considered unsupported by Google. It is considered obsolete and people should use LCL-CustomDrawn-Android instead. This older interface was documented in [[Android Interface]].
1212
1313## Roadmap
1414
1515Part 1, until March 2012:
1616
17-1. <s>Build the set of [[Lazarus Custom Drawn Controls]]</s>
18-2. <s>Initial bindings for the Android APIs</s>
19-3. <s>Create an application to automatically generate the bindings</s>
20-4. <s>Start the new widgetset</s>
21-5. <s>Implement support for JNI</s>
22-6. <s>Merge the Lazarus Custom Drawn Controls into the LCL and use them to implement all basic controls</s>
23-7. <s>Add text support for Android</s>
24-8. <s>Implement basic window client area scrolling</s>
17+1. Build the set of [[Lazarus Custom Drawn Controls]]
18+2. Initial bindings for the Android APIs
19+3. Create an application to automatically generate the bindings
20+4. Start the new widgetset
21+5. Implement support for JNI
22+6. Merge the Lazarus Custom Drawn Controls into the LCL and use them to implement all basic controls
23+7. Add text support for Android
24+8. Implement basic window client area scrolling
25259. Add mobile emulator for desktop platforms
262610. Improve the mobile scrolling
27-11. <s>Add text support in X11</s>
28-12. <s>Add DPI awareness and adaptation in the LCL</s>
29-13. <s>Add non-native text support via pasfreetype</s>
27+11. Add text support in X11
28+12. Add DPI awareness and adaptation in the LCL
29+13. Add non-native text support via pasfreetype
3030
3131Part 2: April, May, June 2012:
3232
33-1. <s>TButton improve the Android visual</s>
33+1. TButton improve the Android visual
34342. TBitBtn
35-3. <s>TRadioButton finish the native look</s>
35+3. TRadioButton finish the native look
36364. Calendar dialog -\> Problematic as the LCL has no calendar dialog, it just opens a normal form and puts a calendar on it, so we need a TCDCalendar widget
37375. MaskEdit
38-6. <s>Improve TComboBox</s>
38+6. Improve TComboBox
39397. TMemo
40408. TListView as a list with columns
41-9. <s>TMainMenu</s>
41+9. TMainMenu
424210. Database with sqlite
434311. Barcode reading
444412. Windows add-on installer
4545
4646## Using the Android SDK, Emulator and Phones
4747
4848[[Custom Drawn Interface/Using the Android SDK, Emulator and Phones]]
4949
5050## Android Programming
5151
5252See [[Android Programming]], also includes caracteristics of the various phones relevant for programming.
5353
5454## Configuring the Free Pascal Compiler for Android
5555
5656| NOTE: |
5757|----|
5858| The cross-compiler instructions below are probably obsolete, since support for the [[Android|Android target]] is present in the trunk (development) version 2.7.1 of FPC. Here are up to date instructions: [[Android|building a cross-compiler for Android target]]. |
5959
6060### Use the ready Virtual Box Image with FPC and Lazarus
6161
6262The fastest way to configure Free Pascal and Lazarus for Android development is simply using a pre-configured virtual machine image which already includes Mageia Linux 1, KDE, Lazarus 0.9.31, Subversion, Free Pascal for both x86-linux and arm-linux (targetting Android), the Android SDK, the Android NDK, Java, Ant, Gtk2 and all it's development packages, the GNU debugger (GDB), subversion and everything else necessary for building Android applications with Lazarus.
6363
6464The download like for the virtual machine image is: <https://sourceforge.net/projects/p-tools/files/Lazarus_for_Android_VM_Image/snapshot_30_dez_2011/> (warning: the VirtualBox image was created using the Mac OS X default Zip utility and not all zip applications can handle it. The Mac OS X default zip application can, and [PeaZip](https://peazip.github.io/) too and [WinRAR](http://win-rar.com/download.html) too. Both 7-Zip and the Windows 7 default unzipper fail.
6565
6666The virtual machine image is composed by two files: “Android Devel VM.vbox” and “Android Devel VM.hdd”. Both need to be located in the same folder and also note that the source forge download is a zipped file, so it needs to be unzipped. After unzipping the VM image has 5,2GB. To use this virtual machine one should install Virtual Box and then use it to open the .vbox file. Then one can already use this Linux virtual machine to build Android applications. The process of building an application involves first opening Lazarus and building the application in Lazarus, for example the example project available on ~/Programas/lazarus/example/androidlcl/ and then going into the Linux terminal, navigating to ~/Programas/lazarus/example/androidlcl/android and issuing the command “ant debug” which will generate the APK file in ~/Programas/ lazarus/example/androidlcl/android/bin/LCLExample-debug.apk. There are two users in the virtual machine: “root” and “pascaldev” and both of them use the password “root”.
6767
6868Note that the virtual machine contains the Lazarus source code from when it was created, but one can also use svn update to get the latest code. Note that this involves some risks, however, as the trunk version might ocasionally contain problems.
6969
7070### Building the compiler yourself in Windows
7171
7272**Step 1** - Install the Android SDK and NDK
7373
7474Download from Google and install both the SDK and the NDK for Windows.
7575
7676Check your path to the binutils installed by the NDK. For example, for NDK r7 installed in C:\Programas\android-ndk-r7\\ the path is C:\Programas\android-ndk-r7\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\bin
7777
7878Also check your path to the libraries installed by the NDK. For example, for NDK r7 installed in C:\Programas\android-ndk-r7\\ the path is C:\Programas\android-ndk-r7\platforms\android-8\arch-arm\usr
7979
8080**Step 2** - Download FPC 2.5.1 from January 19th
8181
8282Create a folder to store the FPC trunk source code and checkout revision 16790
8383
8484Or if you want support for database and many other nice fixes use the branch fpc4android from here:
8585
8686svn co <https://p-tools.svn.sourceforge.net/svnroot/p-tools/fpc4android> fpc4android
8787
8888**Step 3** - Install FPC 2.4.4
8989
9090An easy way to obtain a good FPC 2.4.4 is installing Lazarus 0.9.30.2. For example, let's say you install this Lazarus version in C:\Programas\lazarusfpc244
9191
9292**Step 4** - Create the build script and built it
9393
9494Create the output directory, for example: C:\Programas\fpcandroid\output
9595
9696Next create a file called build.bat in the fpc root folder, in our example C:\Programas\fpcandroid and copy to it:
9797
9898 SET PATH=C:\Programas\lazarusfpc244\fpc\2.4.4\bin\i386-win32\
9999 make crossinstall CPU_TARGET=arm OS_TARGET=linux CROSSBINDIR=C:\Programas\android-ndk-r7\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\bin OPT=-dFPC_ARMEL BINUTILSPREFIX=arm-linux-androideabi- INSTALL_PREFIX=C:\Programas\fpcandroid\output
100100 pause
101101
102102Next run this batch script and you should have FPC installed into C:\Programas\fpcandroid\output
103103
104104**Step 5** - Copy some cross-binutils into the bin dir
105105
106106You now should have a FPC binary in C:\Programas\fpcandroid\output\bin\i386-win32
107107
108108Now get these 3 files:
109109
110110C:\Programas\android-ndk-r7\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\bin\arm-linux-androideabi-as.exe C:\Programas\android-ndk-r7\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\bin\arm-linux-androideabi-ld.exe C:\Programas\android-ndk-r7\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\bin\arm-linux-androideabi-strip.exe
111111
112112And copy and then rename to have:
113113
114114C:\Programas\fpcandroid\output\bin\i386-win32\arm-linux-as.exe C:\Programas\fpcandroid\output\bin\i386-win32\arm-linux-ld.exe C:\Programas\fpcandroid\output\bin\i386-win32\arm-linux-strip.exe
115115
116116**Step 6** - Merge this into a Lazarus FPC setup
117117
118118Let's say you have the following Lazarus C:\Programas\lazarus31 with the following fpc installed inside it: C:\Programas\lazarus31\fpc\2.6.1\bin\i386-win32 and C:\Programas\lazarus31\fpc\2.6.1\units\\
119119
120120Now create a directory C:\Programas\lazarus31\fpc\2.5.1\units\arm-linux and copy C:\Programas\fpcandroid\output\units\arm-linux there
121121
122122Copy all .exe files from C:\Programas\fpcandroid\output\bin\i386-win32\\ into C:\Programas\lazarus31\fpc\2.6.1\bin\i386-win32
123123
124124(! copy the exe-files to FPC 2.6.1 !)
125125
126126Now you can already build the example project androidlcltest.lpi using the Windows-Release build mode
127127
128128### Using the pre-compiled compiler in Windows
129129
130130**Step 1** - Download and unzip the pre-compiled compiler
131131
132132The download is available here: <https://sourceforge.net/projects/p-tools/files/Free%20Pascal%20for%20ARM/Windows/>
133133
134134**Step 2** - Merge it into the FPC installation
135135
136136Let's say you have the following Lazarus C:\Programas\lazarus31 with the following fpc installed inside it: C:\Programas\lazarus31\fpc\2.6.1\bin\i386-win32 and C:\Programas\lazarus31\fpc\2.6.1\units\\
137137
138138Now create a directory C:\Programas\lazarus31\fpc\2.5.1\units\arm-linx and copy fpc4android_2_5_1_26_mar_2012\units\arm-linux there
139139
140140Copy all .exe files from fpc4android_2_5_1_26_mar_2012\bin\i386-win32\\ into C:\Programas\lazarus31\fpc\2.6.1\bin\i386-win32 (the version here should be of the installed Windows compiler, not the version of the cross-compiler, so might be 2.6.0 or 2.7.1 or something else)
141141
142142**Step 3** - Install the Java JDK and the Android SDK and NDK
143143
144144If you haven't done so already, then install the Java JDK and the Android SDK and NDK. I recommend the JDK 6 (JDK 7 doesnt work!), latest SDK and the NDK version 7. The Windows NDK 7b has a serious bug: [1](https://groups.google.com/group/android-ndk/browse_thread/thread/d8c88bd58ad6df87/5396d1223f0c65b1?hl=pl&lnk=gst&q=Felipe+Monteiro+de+Carvalho#5396d1223f0c65b1) Some people also report that 7c works fine, and I tested that 8c works fine.
145145
146146- Java JDK (!WARNING! Download JDK version 6! Version 7 does not work!!!): <http://www.oracle.com/technetwork/java/javase/downloads/index.html>
147147- Android SDK Download (!WARNING! Instead of clicking in the first download link that you see, go to "Use an existing IDE"!): <http://developer.android.com/sdk/index.html> !WARNING! The SDKs r21 and superior removed the nice apkbuilder.bat script which is utilized in our build system in Windows. If you want to use our Windows build scripts, download the SDK r20 from this link: <http://dl.google.com/android/installer_r20-windows.exe>
148148- Android NDK version 7 download link: <http://dl.google.com/android/ndk/android-ndk-r7-windows.zip>
149149
150150Now you can already build the example project androidlcltest.lpi using the Windows-Release build mode and these instructions: [[Custom Drawn Interface/Android|Custom Drawn Interface/Android#Compiling_the_example_LCL_Android_Application]]
151151
152152### Building the compiler yourself in Linux
153153
154154See [[Setup Cross Compile For ARM]] and make sure to use the option OPT="-dFPC_ARMEL" for building the compiler.
155155
156156### Using the pre-compiled compiler in Linux
157157
158158A pre-compiled compiler is provided for convenience for users. The following steps were tested in Mandriva Linux 2010.0 and 2010.1:
159159
160160**Required Environment**
161161
162162- The latest stable FPC installed in the system via the RPM / DEB / TAR package
163163
164164**Step 1** - Install the cross-binutils
165165
166166For Mandriva Linux the RPM package containing arm-linux-as, arm-linux-ld, etc, which are the cross-binutils can be found here: <http://rpm.pbone.net/index.php3/stat/4/idpl/14252825/dir/mandriva_2010/com/cross-arm-binutils-2.20.51.0.4-2mnb2.i586.rpm.html>
167167
168168Just download the RPM package and install it using:
169169
170170`rpm -ivh cross-arm-binutils-2.20.51.0.4-2mnb2.i586.rpm `
171171
172172In Mandriva Linux 2010.0 the dependencies won't match, as the package is for 2010.1, but one can simply ignore this problem and it works fine using --nodeps:
173173
174174`rpm -ivh --nodeps cross-arm-binutils-2.20.51.0.4-2mnb2.i586.rpm `
175175
176176For other distributions use the corresponding package, or else read the instructions for building the cross-binutils yourself at [[Setup Cross Compile For ARM]]
177177
178178**Step 2** - Configure the cross-binutils
179179
180180The assembler needs a parameter to tell it which ARM ABI to use. A choice which works good is EABI-5, which is compatible with all Android devices available as of Jan 2011. To set this, we will rename the original assembler and substitute it with a shell script which passes the desired parameter. These commands will do it:
181181
182182`su`\
183183`mv /usr/bin/arm-linux-as /usr/bin/arm-linux-as_org`\
184184`gedit /usr/bin/arm-linux-as`
185185
186186Now paste into the editor this code:
187187
188188`#!/bin/sh`\
189189`/usr/bin/arm-linux-as_org -meabi=5 $@`
190190
191191And don't forget to then make it executable:
192192
193193`chmod 755 /usr/bin/arm-linux-as`
194194
195195**Step 3** - Install the Free Pascal Cross-Compiler
196196
197197At this point the pre-compiled FPC can be download from here: <http://sourceforge.net/projects/p-tools/files/Free%20Pascal%20for%20ARM/>
198198
199199Then use these commands to install the pre-compiled Free Pascal cross-compiler into /usr:
200200
201201`[felipe@localhost Programas]$ ls -l`\
202202`total 20664`\
203203`-rw-rw-r--  1 felipe felipe 17098552 2010-10-25 08:17 fpc-2.5.1.arm-linux.tar.gz`\
204204`[felipe@localhost Programas]$ su`\
205205`Password: `\
206206`[root@localhost Programas]# cp fpc-2.5.1.arm-linux.tar.gz /usr/`\
207207`[root@localhost Programas]# cd /usr/`\
208208`[root@localhost usr]# tar -xvf fpc-2.5.1.arm-linux.tar.gz `\
209209`[root@localhost usr]# ln -s /usr/lib/fpc/2.5.1/ppcrossarm /usr/bin/ppcrossarm`
210210
211211**Step 4** - Verify if your Cross-Compiler works
212212
213213If you made no errors in the previous steps, it should work, so try to call it like this:
214214
215215`[felipe@localhost Programas]$ /usr/bin/ppcrossarm`
216216
217217If this command works and fpc shows its options, then you configured the cross-compiler correctly, if not, then try to find out if your symbolic link points to a correct location with this command:
218218
219219\[felipe@localhost Programas\]\$ ls -ls /usr/bin/ppc*
220220
221221Now we are ready to compile Android applications using the Lazarus IDE! Configuring the fpc.cfg file isn't necessary, the old compiler will automatically find the new compiler and it's object files.
222222
223223## Install the Java JDK and the Android SDK and NDK
224224
225225If you haven't done so already, then install the Java JDK and the Android SDK and NDK. I recommend the JDK 6 (JDK 7 doesnt work!), latest SDK and the NDK version 7. The Windows NDK 7b has a serious bug: [2](https://groups.google.com/group/android-ndk/browse_thread/thread/d8c88bd58ad6df87/5396d1223f0c65b1?hl=pl&lnk=gst&q=Felipe+Monteiro+de+Carvalho#5396d1223f0c65b1) Some people also report that 7c works fine, and I tested that 8c works fine.
226226
227227- Java JDK (Choose version 6!): <http://www.oracle.com/technetwork/java/javase/downloads/index.html>
228228- Android SDK Download (Choose Use an existing IDE!): <http://developer.android.com/sdk/index.html>
229229- Android NDK version 7 download link: <http://dl.google.com/android/ndk/android-ndk-r7-windows.zip>
230230
231231Now you can already build the example project androidlcltest.lpi using the Windows-Release build mode and these instructions: [[Custom Drawn Interface/Android|Custom Drawn Interface/Android#Compiling_the_example_LCL_Android_Application]]
232232
233233## Compiling the example LCL Android Application
234234
235235**Step 1 - Download the source code**
236236
237237The source code of the example is located inside the Lazarus sources in lazarus/examples/androidlcl/androidlcltest.lpi
238238
239239**Step 2 - Make sure you have the Java JDK and the Android SDK and NDK installed**
240240
241241Read here: [[Custom Drawn Interface/Android|Custom_Drawn_Interface/Android#Install_the_Java_JDK_and_the_Android_SDK_and_NDK]]
242242
243243**Step 3 - Build the project using the Lazarus IDE**
244244
245245Configuring Lazarus itself to use the new compiler should not normally be necessary because fpc should be able to find the symlink created, but if you have trouble in this part you can try to hard code the compiler path to use the new crosscompiler. To hardcode the compiler path in cause of trouble go to the menu "Tools-\>Options" and change the "Compiler Path" to "/usr/bin/ppcrossarm"
246246
247247But it is necessary to configure the LPI file to build the example. Open the Project Options dialog of the androidlcltest.lpi project and go to the section Paths and change the libraries path to reflect the correct paths of where your Android NDK was installed and where your jnigraphics.so, libc.so, etc, libraries are located for your target Android API level. For LCL-CustomDrawn-Android API level 8 is the best choice, because it supports Android 2.2+ like the LCL. The image below shows where to configure this:
248248
249-<figure>
250-<img src="Configure_androidlcl_libraries.jpg" title="Configure_androidlcl_libraries.jpg" />
251-<figcaption>Configure_androidlcl_libraries.jpg</figcaption>
252-</figure>
249+![](/wiki-images/b/b0/Configure_androidlcl_libraries.jpg)
253250
254251The actual lazarus release contains a error, they create a .so.so - file instead of a .so - file. So you have to change this manually in lazarus.
255252
256253**Step 4 - Build the APK**
257254
258255If you are using Windows, use these instructions to build the APK: [[Custom Drawn Interface/Android|Custom Drawn Interface/Android#Step_4_-_Creating_the_APK_package_in_Windows]]
259256
260257If you are using Linux follow these instructions instead: Before going to the command line to build the APK open the file lazarus/examples/androidlcl/android/local.properties and change it to reflect your Android SDK path. The file says you should not modify it, but ignore that and modify it anyway. Here is how the file looks like:
261258
262259 # This file is automatically generated by Android Tools.
263260 # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
264261 #
265262 # This file must *NOT* be checked in Version Control Systems,
266263 # as it contains information specific to your local configuration.
267264
268265 # location of the SDK. This is only used by Ant
269266 # For customization when using a Version Control System, please read the
270267 # header note.
271268 sdk.dir=/home/felipe/Programas/android-sdk-linux
272269
273270After configuring that one, go to the command line and issue these commands:
274271
275272` cd lazarus/examples/androidlcl/android`\
276273` ant debug`
277274
278275The APK will be located in lazarus/examples/androidlcl/android/bin
279276
280277**Step 5 - Install the APK in your phone and run it**
281278
282279You can install via ADB or by copying the file to the sdcard. You should see this:
283280
284-<figure>
285-<img src="lcl_android_30_mar.png" title="lcl_android_30_mar.png" />
286-<figcaption>lcl_android_30_mar.png</figcaption>
287-</figure>
281+![](/wiki-images/4/4c/Lcl_android_30_mar.png)
288282
289283## How to create an LCL Android Application
290284
291285To create a new LCL-CustomDrawn-Android application simply copy all of the file structure and build and java files from the example project called "androidlcl". This example can be obtained from the Lazarus source tree in lazarus/examples/androidlcl
292286
293287Then you will need to modify the build files to change them to your new project name and your new Java package name.
294288
295289### Step 1 - Creating the LPI
296290
297291You need a separate LPI at the moment for the Android version of the application but all the rest of the code can be shared. Create it using the template for a "Library" and then adapt the code from the example located in the lazarus source code in lazarus/examples/androidlcl
298292
299293You need to adapt the exported JNI method names to your Android Package Name.
300294
301295### Step 2 - Building the library
302296
303297First of all, build the Pascal executable without debug information. This debug information is not so useful in Android and makes the executable much bigger. Open the menu Project-\>Project Options and set the build mode, widgetset, architecture and operating system targets, as shown in these screenshots:
304298
305-<figure>
306-<img src="Android_project_options_1.png" title="Android_project_options_1.png" />
307-<figcaption>Android_project_options_1.png</figcaption>
308-</figure>
299+![](/wiki-images/6/64/Android_project_options_1.png)
309300
310-<figure>
311-<img src="Android_project_options_2.png" title="Android_project_options_2.png" />
312-<figcaption>Android_project_options_2.png</figcaption>
313-</figure>
301+![](/wiki-images/f/f7/Android_project_options_2.png)
314302
315-<figure>
316-<img src="Android_project_options_3.png" title="Android_project_options_3.png" />
317-<figcaption>Android_project_options_3.png</figcaption>
318-</figure>
303+![](/wiki-images/9/9f/Android_project_options_3.png)
319304
320305### Step 3 - Create the Android project structure
321306
322307Besides the LCL project building, such as configuring a proper LPI and having a library main project file, one also needs to add all of the android project structure. Simply copy from the example project in lazarus/examples/androidlcl and adapt it to the new project. Things to change are the path to the SDK, the package name. The package name needs to be updated in the build files, in the directory structure android/src/packagename, in the activity java source code and also in the main library pascal source in the exports section.
323308
324309### Step 4 - Creating the APK package
325310
326311#### Step 4 - Creating the APK package in Linux
327312
328313To create the APK package in Linux simply go to the directory "android" inside the project folder and issue the command "ant debug". The package will be in android/bin/MyProject.apk
329314
330315`cd myproject/android ant debug`
331316
332317Ant and all dependencies must be installed using the package management of your linux distribution.
333318
334319#### Step 4 - Creating the APK package in Windows
335320
336321**Pre-requisites**:
337322
338323- Java JDK installed: <http://www.oracle.com/technetwork/java/javase/downloads/index.html>
339324- Android SDK Installed
340325
341326**Instructions**:
342327
343328In Windows we provide a APK build system which uses only the tools provided in the Android SDK and therefore does not require ANT.
344329
345330First it is necessary to create a debug key to use. To do this use this script:
346331
347332`lazarus/examples/androidlcl/android/generate_debug_key.bat`
348333
349334which can be copied from the example application to your own project folder.
350335
351336it contains following:
352337
353338`REM Adjust these paths to yours`\
354339`SET PATH=C:\Programme\Android\android-sdk\tools;C:\Programme\Android\android-sdk\platform-tools\;C:\Programme\Java\jdk1.6.0_38\bin`\
355340`SET APP_NAME=androidlcltest`\
356341`SET ANDROID_HOME=C:\Programme\Android\android-sdk`\
357342`SET APK_SDK_PLATFORM=C:\Programme\Android\android-sdk\platforms\android-9`\
358343`SET APK_PROJECT_PATH=C:\Programme\lazarus\examples\androidlcl\android`\
359-\
344+
360345`mkdir bin`\
361-\
346+
362347`keytool --help`\
363-\
348+
364349`keytool -genkey -v -keystore bin\LCLDebugKey.keystore -alias LCLDebugKey -keyalg RSA -validity 10000`\
365-\
350+
366351`REM call and pause together allow us to see the results in the end`\
367352`pause`\
368353
369354You have to fix everything after SET to your own paths. Don't forget adjusting the android version from APK_SDK_PLATFORM!
370355
371356After generating the key, use the other script:
372357
373358`lazarus/examples/androidlcl/android/build_debug_apk.bat`
374359
375360To build the APK file. Also remember to edit this batch file to reflect your paths.
376361
377362This script contains the following:
378363
379364`REM Adjust these paths to yours`\
380365`SET PATH=C:\Programme\Android\android-sdk\tools;C:\Programme\Android\android-sdk\platform-tools\;C:\Programme\Java\jdk1.6.0_38\bin`\
381366`SET APP_NAME=androidlcltest`\
382367`SET ANDROID_HOME=C:\Programme\Android\android-sdk`\
383368`SET APK_SDK_PLATFORM=C:\Programme\Android\android-sdk\platforms\android-9`\
384369`SET APK_PROJECT_PATH=C:\lazarus\examples\androidlcl\android `\
385-\
370+
386371`REM Create necessary directory Structure`\
387372`mkdir bin`\
388373`mkdir bin\classes`\
389374`mkdir gen`\
390375`mkdir gen\com`\
391376`mkdir gen\com\pascal`\
392377`mkdir gen\com\pascal\lcltest`\
393378`mkdir raw`\
394379`mkdir raw\lib`\
395380`mkdir raw\lib\armeabi`\
396-\
381+
397382`REM Cleanup`\
398383`del bin\%APP_NAME%.ap_`\
399384`del bin\%APP_NAME%.apk`\
400385`del raw\lib\armeabi*.so`\
401-\
386+
402387`REM More directory preparation`\
403388`copy libs\armeabi*.so raw\lib\armeabi\ `\
404-\
389+
405390`REM Resource compilation`\
406391`call aapt p -v -f -M AndroidManifest.xml -F bin\%APP_NAME%.ap_ -I %APK_SDK_PLATFORM%\android.jar -S res -m -J gen raw`\
407-\
392+
408393`REM Java compiler`\
409394`call javac -verbose -classpath %APK_SDK_PLATFORM%\android.jar -d bin\classes src\com\pascal\lcltest\LCLActivity.java`\
410-\
395+
411396`REM DX to convert the java bytecode to dalvik bytecode`\
412397`call dx --dex --verbose --output=%APK_PROJECT_PATH%\bin\classes.dex %APK_PROJECT_PATH%\bin\classes `\
413-\
398+
414399`REM It seams that dx calls echo off`\
415400`@echo on`\
416401`REM Now build the unsigned APK`\
417402`del %APK_PROJECT_PATH%\bin\%APP_NAME%-unsigned.apk`\
418403`call apkbuilder %APK_PROJECT_PATH%\bin\%APP_NAME%-unsigned.apk -v -u -z %APK_PROJECT_PATH%\bin\%APP_NAME%.ap_ -f %APK_PROJECT_PATH%\bin\classes.dex`\
419-\
404+
420405`REM Generating on the fly a debug key`\
421406`rem keytool -genkey -v -keystore bin\LCLDebugKey.keystore -alias LCLDebugKey -keyalg RSA -validity 10000 -dname NAME -storepass senhas  -keypass senhas`\
422-\
407+
423408`REM Signing the APK with a debug key`\
424409`del bin\%APP_NAME%-unaligned.apk`
425410
426411`REM Change keypass and storepass pw                        here|         and here|`\
427412`jarsigner -verbose -keystore bin\LCLDebugKey.keystore -keypass senhas -storepass senhas -signedjar bin\%APP_NAME%-unaligned.apk  bin\%APP_NAME%-unsigned.apk LCLDebugKey`\
428-\
413+
429414`REM Align the final APK package`\
430415`zipalign -v 4 bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%.apk `\
431-\
416+
432417`REM call and pause together allow us to see the results in the end`\
433418`pause`
434419
435420Don't forget changing the set-variables in the top AND change the pw (see above where).
436421
437422### Building an LCL Android application with debug info
438423
439424It is useful to add another Android build mode which has debug info. Use all of the same options as shown above, except for the debug information:
440425
441-<figure>
442-<img src="Android_project_options_4.png" title="Android_project_options_4.png" />
443-<figcaption>Android_project_options_4.png</figcaption>
444-</figure>
426+![](/wiki-images/1/11/Android_project_options_4.png)
445427
446428## Oh no! My LCL Android application doesn't work
447429
448430There are various reasons why the app may not work. The most important thing to do when an app doesn't work is to open the logcat and see what the log says. This can be done by running this command line command:
449431
450432``` bash
451433./adb logcat
452434```
453435
454436And then test one of the hypotheses in the next subsections.
455437
456438### The Pascal executable was compiled for a wrong architecture, operating system and/or widgetset
457439
458440This is the leading cause of executables not running. Always verify that you compiled the program to the "android" widgetset, "linux" operating system and "arm" architecture. This is done by going into the menu Project-\>Project Options. Then set these in the dialog all options as explained in this section:
459441
460442[[How to build an LCL Android application]]
461443
462444To check if you library is arm-linux or not, execute the following commands:
463445
464446``` bash
465447cd libs/armeabi
466448ls -l
467449file liblclapp.so
468450ldd liblclapp.so
469451```
470452
471453If it mentions anything like 80386 or gtk, then your program was compiled for linux-x86 and gtk2, not for Android like it should.
472454
473455### My Pascal application crashed. How to get a stacktrace?
474456
475457If a Pascal application crashes you should use the command "adb logcat" to obtain the stack
476458
477459Read also this thread in the Google NDK group about converting addresses to lines: <http://groups.google.com/group/android-ndk/msg/2ed2286e85f4ccdd>?
478460
479461### The indispensable build options
480462
481463- -Xd This build option is indispensable when cross-compiling from x86-linux to Android or else FPC will try to link the application against for example /usr/lib/libc.so instead of the libc.so in the NDK, even if you specify a -Fl library path
482464- -CpARMV6 This build option is indispensable because code generated for older ARM versions is unsupported by Android and might crash in some devices. See <http://groups.google.com/group/android-ndk/browse_thread/thread/ba542483f062a828>
483465- -dAndroid in the package LCLBase.lpk
484466- -Parm -Tlinux for the process and target operating system
485467- -Fl should contain the path to the NDK directory with the target link shared object, such as libc.so, liblog.so, libjni.so, etc
486468
487469### Wrong package name
488470
489471If you change the package name of the project, then you need to adapt the main project file to inform the widgetset of the change. In the example project this is done in the main project file, androidlcltest.lpr in this part:
490472
491473 CDWidgetset.ActivityClassName := 'com/pascal/lcltest/LCLActivity';
492474
493475If your new package name is com.pascal.myapp, then you should change the command above to this:
494476
495477 CDWidgetset.ActivityClassName := 'com/pascal/myapp/LCLActivity';
496478
497479## Free Pascal Bugs on Android Support
498480
499481The following summarizes the state of Android Support in Free Pascal:
500482
501483- The pre-compiled snapshot for x86-linux -\> Android is known to work very well and it was built on 19th January 2011: <https://sourceforge.net/projects/p-tools/files/Free%20Pascal%20for%20ARM/>
502484- On February this revision <http://svn.freepascal.org/cgi-bin/viewvc.cgi?view=rev&revision=16981> introduced this bug: <http://mantis.freepascal.org/view.php?id=18833>
503485- In the end of 2011 this revision <http://svn.freepascal.org/cgi-bin/viewvc.cgi?view=rev&revision=19036> broke linking Android projects
504486
505487We are in contact with FPC developers to fix the situation as can be seen in: <http://mantis.freepascal.org/view.php?id=20726>
506488
507489### FPC4Android Branches
508490
509491Because of FPC bugs related to Android support I have rolled my own branches.
510492
511493#### FPC4Android 2.5.1
512494
513495FPC4Android 2.5.1 has the following fixes applied to it:
514496
515497<http://bugs.freepascal.org/view.php?id=21208> <http://bugs.freepascal.org/view.php?id=21341> <http://bugs.freepascal.org/view.php?id=19559>
516498
517499And I have created a pre-compiled x86-linux -\> android-arm cross-compiler and hosted it here (it is the -v2 cross-compiler);
518500
519501<https://sourceforge.net/projects/p-tools/files/Free%20Pascal%20for%20ARM/>
520502
521503To download the source execute:
522504
523505`svn co `[`https://p-tools.svn.sourceforge.net/svnroot/p-tools/fpc4android`](https://p-tools.svn.sourceforge.net/svnroot/p-tools/fpc4android)` fpc4android`
524506
525507#### FPC4Android 2.6.1
526508
527509This branch is not finished, it lacks patches to fix Android support, patches are welcome. SVN link:
528510
529511`svn co `[`https://p-tools.svn.sourceforge.net/svnroot/p-tools/fpc4android_2_6`](https://p-tools.svn.sourceforge.net/svnroot/p-tools/fpc4android_2_6)` fpc4android_2_6`
530512
531513## Android SDK and NDK bugs
532514
533515### Android NDK for Windows broken linker
534516
535517The linker in android-ndk-r7b (path C:\Programas\android-ndk-r7\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\bin\arm-linux-androideabi-ld.exe) crashes for no reason while linking LCL apps =( The linker from android-ndk-r7 and r5 work correctly.
536518
537519See <http://groups.google.com/group/android-ndk/browse_thread/thread/d8c88bd58ad6df87>
538520
539521### Error while calling DX: Trouble processing: bad class file magic (cafebabe)
540522
541523If while using the build script you get the following errors:
542524
543525 D:\djsystem\djpalm\android>call dx --dex --verbose --output=D:\djsystem\djpalm\a
544526 ndroid\bin\classes.dex D:\djsystem\djpalm\android\bin\classes
545527 processing D:\djsystem\djpalm\android\bin\classes\.\com\pascal\djpalm\LCLActivit
546528 y$1.class...
547529
548530 trouble processing:
549531 bad class file magic (cafebabe) or version (0033.0000)
550532 ...while parsing com/pascal/djpalm/LCLActivity$1.class
551533 ...while processing com/pascal/djpalm/LCLActivity$1.class
552534 processing D:\djsystem\djpalm\android\bin\classes\.\com\pascal\djpalm\LCLActivit
553535 y$2.class...
554536
555537 trouble processing:
556538 bad class file magic (cafebabe) or version (0033.0000)
557539 ...while parsing com/pascal/djpalm/LCLActivity$2.class
558540 ...while processing com/pascal/djpalm/LCLActivity$2.class
559541 processing D:\djsystem\djpalm\android\bin\classes\.\com\pascal\djpalm\LCLActivit
560542 y$3.class...
561543
562544This probably means that you are using JDK 7. Unfortunatelly the current Android SDK r20 is not compatible with JDK 7, only with JDK 5 and 6. Please install JDK 6 and use that instead. See:
563545
564546- <http://stackoverflow.com/questions/8538891/bad-class-file-magic-when-using-dx-bat>
565547- <http://www.oracle.com/technetwork/java/javase/downloads/jdk6-downloads-1637591.html>
566548
567549## Development Notes
568550
569551### The mysterious JNI Crash (SIGILL - illegal instruction)
570552
571553See the thread: <http://groups.google.com/group/android-ndk/browse_thread/thread/ba542483f062a828>
572554
573555The problem was that vm^^.GetEnv would crash in HTC Wildfire, Alcatel and in the emulator with SIGILL but not in Xperia Mini, HTC Desire HD, Motorola Atrix and other smartphones.
574556
575557The answer was that one needs to specify -CpARMV6 when building because older instructions might fail in some devices. Also try restarting the emulator or running on a different device.
576558
577559### NDK Libraries available in Android 2.2 (API level 8)
578560
579561LCL-CustomDrawn targets Android 2.2+ and in this API level the following libraries are supported by the NDK:
580562
5815631. libc.so
5825642. libdl.so (linker)
5835653. OpenGL ES 1
5845664. OpenGL ES 2
5855675. libjnigraphics.so
5865686. liblog.so
5875697. libm.so
5885708. libz.so
5895719. libthread_db.so
59057210. libstdc++.so
591573
592574LCL-CustomDrawn-Android uses libjnigraphics.so and liblog.so at the moment.
593575
594576### Native text drawing
595577
596578Native text drawing is a bit convoluted in Android and the documentation is quite bad too. Some gotchas already found:
597579
598580- It draws text starting on a Y coordinate which represents the baseline, which is the bottom of strings like "abc", but not the bottom of strings like "qg". See <http://code.google.com/p/android/issues/detail?id=393>
599581- There is no documentation about what the size in Paint.setTextSize represents. Some people say it is pixels, but to me it looks something else because it seams to differ across devices.
600582
601583But there are also good things about it:
602584
603585- It can draw on a transparent bitmap and use alpha transparency to implement anti-aliasing and subpixel rendering. The text gets quite beaultiful.
604586- It can draw on the ARGB 32-bits non-premultiplied format which is a pretty good one
605587
606588### Flood of garbage collector messages when the caret is visible
607589
608590Something like this:
609591
610592 D/dalvikvm( 6364): GC_EXTERNAL_ALLOC freed 188 objects / 6664 bytes in 94ms
611593 D/dalvikvm( 6364): GC_EXTERNAL_ALLOC freed 170 objects / 6160 bytes in 78ms
612594 D/dalvikvm( 6364): GC_EXTERNAL_ALLOC freed 162 objects / 5904 bytes in 82ms
613595 D/dalvikvm( 6364): GC_EXTERNAL_ALLOC freed 162 objects / 5856 bytes in 78ms
614596 D/dalvikvm( 6364): GC_EXTERNAL_ALLOC freed 162 objects / 5872 bytes in 80ms
615597 D/dalvikvm( 2825): GC_EXPLICIT freed 371 objects / 18480 bytes in 91ms
616598 D/dalvikvm( 6364): GC_EXTERNAL_ALLOC freed 162 objects / 5896 bytes in 78ms
617599 D/dalvikvm( 6364): GC_EXTERNAL_ALLOC freed 162 objects / 5896 bytes in 78ms
618600 D/dalvikvm( 6364): GC_EXTERNAL_ALLOC freed 162 objects / 5896 bytes in 81ms
619601 D/dalvikvm( 6364): GC_EXTERNAL_ALLOC freed 162 objects / 5920 bytes in 78ms
620602 D/dalvikvm( 6364): GC_EXTERNAL_ALLOC freed 162 objects / 5920 bytes in 83ms
621603
622604In my investigation this happens probably because the TCDEdit will request a full control invalidate on each timer tick, which currently will main a full form invalidate, so the GC needs to collect the bitmaps and other paint objects when painting that often.
623605
624606### Why the caret of a TEdit keeps blinking after clicking on the form
625607
626608When clicking on the form the LCL focus will refuse to move from the Edit to the Form because this behavior seems to be hard coded in the LCL, although maybe we might change this for Android. So even while we set focus into the form, the LCL refuses to do that and keeps its focus in the Edit, thus we have a focus mismatch. This might eventually be fixed in the future.
627609
628610### Android ComboBox
629611
630612For adding Android combobox support a new method was added to LCLIntf along with a global variable to store the callback of this dialog:
631613
632614``` pascal
633615var
634616 OnShowSelectItemDialogResult: TOnShowSelectItemDialogResult;
635617
636618function ShowSelectItemDialog(const AItems: TStrings): Boolean; override;
637619```
638620
639621This method will run a native item selection dialog, if the platform has one, or result false without doing anything otherwise.
640622
641-<figure>
642-<img src="Android_combobox.png" title="Android_combobox.png" />
643-<figcaption>Android_combobox.png</figcaption>
644-</figure>
623+![](/wiki-images/8/82/Android_combobox.png)
645624
646625### Using the Native ListView Dialog
647626
648627To allow the user to select items from a list more easily we offer a routine which launch dialog with the native ListView.
649628
650629``` pascal
651630{$ifdef LCLCustomDrawn}
652631uses customdrawnint, lclintf;
653632{$endif}
654633
655634procedure TForm2.Button3Click(Sender: TObject);
656635begin
657636{$ifdef LCLCustomDrawn}
658637 LCLIntf.OnListViewDialogResult := @MyOnListViewDialogResult;
659638 CDWidgetSet.ShowListViewDialog('Dialog Title',
660639 ['Title1', 'Title2', 'Title3', 'Title4', 'Title5', 'Title6'],
661640 ['Descr1', 'Descr2', 'Descr3', 'Descr4', 'Descr5', 'Descr6']);
662641{$endif}
663642end;
664643
665644procedure TForm2.MyOnListViewDialogResult(ASelectedItem: Integer);
666645begin
667646 DebugLn(Format('[MyOnListViewDialogResult] ASelectedItem=%d', [ASelectedItem]));
668647end;
669648```
670649
671650LCLIntf.OnListViewDialogResult will be called with the selected item, with a zero-based index, or -1 if the dialog was cancelled by the user. One way to cancel the dialog is when the user pressed the back button.
672651
673652Note that there are 2 more parameters in the end of this routine with default values, which allow choosing the color for the odd and the other for the even rows. The color is an integer of 32-bits in the ARGB format, don't use TColor here!
674653
675654``` pascal
676655procedure TCDWidgetSet.ShowListViewDialog(ATitle: string; ATitles,
677656 ADescriptions: array of string;
678657 AColorOddRow: jint = $ff292C29; AColorEvenRow: jint = $ff424542);
679658```
680659
681-<figure>
682-<img src="Android_ListView.png" title="Android_ListView.png" />
683-<figcaption>Android_ListView.png</figcaption>
684-</figure>
660+![](/wiki-images/2/2c/Android_ListView.png)
685661
686662### LazDeviceAPIs
687663
688664See: [[LazDeviceAPIs]]
689665
690666---
691667*From the Free Pascal wiki, page "Custom Drawn Interface/Android", as it stood on 2021-01-08 after a change by Giorgio Tani, 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).*
692668Category: Android
693669Category: Custom Drawn

Back to history