Sign in Join
Wiki copytemporary

Android Build APK: what changed

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

OldNewLine
11This article covers a way to compile a project in FPC and download it to an Android device. It is assumed that the project is an NDK library that is loaded in Java code. Also, this method is not tied to any IDE extension (for example Android4Laz) and can be used separately for arbitrary projects.
22
33## Requirements
44
55- FPC (with ready-made Android rtl libraries).
66- JRE (Java-Run Time) - needed for the JDK, as well as to run the "java" machine (for executing the utilities included in the Android SDK)
77- JDK (required to install javac ), JDK will require JRE to be installed.
88- Android NDK
99- Android SDK
1010
1111No additional build tools (e.g. ant) ​​are required
1212
1313### JRE / JDK version
1414
1515At the moment (winter 2017) only version 8 (1.8) is officially available for download. Previously, you could download version 7. Many components of the Android SDK are tied to a specific version of the JDK.
1616
1717## Assembly
1818
19190\. It is assumed that the entire project is in a folder, for example "mydemo". And all scripts and utilities for building under android are located in "mydemo \\ android". This only matters in the next step where the -FE switch is specified.
2020
21211\. you need to compile the project (library). The command line (windows) might look like this.
2222
23-`C:\FPC\3.1.1\bin\i386-win32\fpc.exe ^`\
24-`-Tandroid -Parm -MDelphi ^`\
25-`-Scghi -CX -O3 -Xs -XX -l -vbq ^`\
26-`-FUlib\arm-android ^`\
27-`-oandroid\mydemo.so ^`\
28-`-FE.\Android\files\lib\armeabi\ ^`\
29-`-CpARMv6 ^ `\
30-`-CfVFPv2 ^`\
31-`-Xd ^`\
32-`-XParm-linux-androideabi- ^`\
33-`-FDC:\android\ndk-r11c\toolchains\arm-linux-androideabi-4.9\prebuilt\windows\bin ^`\
34-`-FuC:\android\ndk-r11c\platforms\android-9\arch-arm\usr\lib ^`\
35-`mydemo.lpr`
23+```
24+C:\FPC\3.1.1\bin\i386-win32\fpc.exe ^
25+-Tandroid -Parm -MDelphi ^
26+-Scghi -CX -O3 -Xs -XX -l -vbq ^
27+-FUlib\arm-android ^
28+-oandroid\mydemo.so ^
29+-FE.\Android\files\lib\armeabi\ ^
30+-CpARMv6 ^
31+-CfVFPv2 ^
32+-Xd ^
33+-XParm-linux-androideabi- ^
34+-FDC:\android\ndk-r11c\toolchains\arm-linux-androideabi-4.9\prebuilt\windows\bin ^
35+-FuC:\android\ndk-r11c\platforms\android-9\arch-arm\usr\lib ^
36+mydemo.lpr
37+```
3638
3739What you should pay attention to. Keys:
3840
3941- -FD - specifies the path where build utilities are stored
4042- -XParm-linux-androideabi- - specifies the prefix for the build utilities
4143- -Fu - pointing to NDK libraries - required for linking
4244- -FE - specifies the path where the created library should be written. The destination directory in this case is listed as "lib \\ armeabi". This path will be saved in the apk itself. It should be just that, because it is tied to the target architecture (arm). For arm64 or arm7, the path must be different. All third-party dynamic link libraries for ARM architecture should also be located in the lib \\ armeabi folder.
4345
44462\. Prepare the AndroidManifest.xml manifest file
4547
46483\. Create the .apk file
4749
48-`set buildtoolsdir=C:\android\sdk-windows\build-tools\23.0.3`\
49-`set APP_NAME=testapp`\
50-`set APK_SDK_PLATFORM=C:\android\sdk-windows\platforms\android-11`\
51-`rem -S res - fails, if "res" directory doesn't exist`\
52-`rem generate-dependencies is needed to generate R.java file`
50+```
51+set buildtoolsdir=C:\android\sdk-windows\build-tools\23.0.3
52+set APP_NAME=testapp
53+set APK_SDK_PLATFORM=C:\android\sdk-windows\platforms\android-11
54+rem -S res - fails, if "res" directory doesn't exist
55+rem generate-dependencies is needed to generate R.java file
56+```
5357
54-`%buildtoolsdir%\aapt p -v -f -M AndroidManifest.xml ^`\
55-` -F bin\%APP_NAME% .ap_ ^`\
56-` --generate-dependencies ^`\
57-` -I%APK_SDK_PLATFORM%\android.jar -S res -m -J gen ^`\
58-` files`
58+```
59+%buildtoolsdir%\aapt p -v -f -M AndroidManifest.xml ^
60+ -F bin\%APP_NAME% .ap_ ^
61+ --generate-dependencies ^
62+ -I%APK_SDK_PLATFORM%\android.jar -S res -m -J gen ^
63+ files
64+```
5965
6066The utility creates an initial .apk file (-F bin\\%APP_NAME% .ap_ - the extension is specified as "ap_" intentionally, since this is not a final file), as well as a R.java file in the "gen" folder (-J gen). When creating an apk (essentially a .zip) file, the "files" folder will be included. (at step \# 1, the library "-FE.\android\files\lib\armeabi\\" was copied into it, so "lib\armeabi\mydemo.so" will get into the apk file) "-S" indicates the folder with standard android resources (icons and layout xml-s), the folder will be copied to .apk The structure of the generated .apk file will look like this:
6167
62-`AndroidManifest.xml `\
63-`resources.arsc`\
64-`res`\
65-`lib`\
66-`lib\armeabi`\
67-`... all other folders/files that were located in "files"`
68+```
69+AndroidManifest.xml
70+resources.arsc
71+res
72+lib
73+lib\armeabi
74+... all other folders/files that were located in "files"
75+```
6876
6977If the program has any resource files, they must be added to "files" immediately BEFORE calling "aapt". It is not necessary to copy them to "files" either (as well as to use the "files" folder). you can specify several source folders, for example:
7078
71-`%buildtoolsdir%\aapt ... ^`\
72-`  dir1 dir2 dir3`\
79+```
80+%buildtoolsdir%\aapt ... ^
81+ dir1 dir2 dir3
82+```
7383
74844\. Compile Java code of the main program
7585
76-`set jdkbindir=C:\Program Files\Java\jdk1.7.0_25\bin`\
77-`set androidjar=C:\android\sdk-windows\platforms\android-11\android.jar `
86+``` pascal
87+set jdkbindir=C:\Program Files\Java\jdk1.7.0_25\bin
88+set androidjar=C:\android\sdk-windows\platforms\android-11\android.jar
89+```
7890
79-`"%jdkbindir%\javac.exe" ^`\
80-`  .\zengl\android\ZenGL.java ^`\
81-`  .\zengl\demo01\Demo01Activity.java ^`\
82-`  -cp "%androidjar%" ^`\
83-`  -d.\classes`
91+```
92+"%jdkbindir%\javac.exe" ^
93+ .\zengl\android\ZenGL.java ^
94+ .\zengl\demo01\Demo01Activity.java ^
95+ -cp "%androidjar%" ^
96+ -d.\classes
97+```
8498
8599It is important to specify "-cp" (class-path) for the used Android-SDK. The java compiler, like jdk itself, does not know anything about the android system. And the compiled program will use the SDK classes. Therefore, if the class-path is not specified, then errors will appear that the class is not declared, etc. -d - specifies the path where to put the compiled .class files, they are needed in the next step.
86100
87101In a further step, the compiled code must be converted to Dalvik. It is possible that version "dx.jar" is expected for classes version 1.7, not 1.8 (which you can use). In this case, you need javac to specify the compatibility version:
88102
89-`set jdkbindir= C:\Program Files\Java\jdk1.8.0_25\bin`\
90-`set androidjar=C:\android\sdk-windows\platforms\android-11\android.jar `
103+``` pascal
104+set jdkbindir= C:\Program Files\Java\jdk1.8.0_25\bin
105+set androidjar=C:\android\sdk-windows\platforms\android-11\android.jar
106+```
91107
92-`"%jdkbindir%\javac.exe" ^`\
93-`  .\zengl\android\ZenGL.java ^`\
94-`  .\zengl\demo01\Demo01Activity.java ^`\
95-`  -cp "%androidjar%" ^`\
96-`  -d.\classes -target 1.7 -source 1.7`
108+```
109+"%jdkbindir%\javac.exe" ^
110+ .\zengl\android\ZenGL.java ^
111+ .\zengl\demo01\Demo01Activity.java ^
112+ -cp "%androidjar%" ^
113+ -d.\classes -target 1.7 -source 1.7
114+```
97115
981165\. Convert Java code to dalvik
99117
100-`SET DX_PATH=C:\android\sdk-windows\build-tools\23.0.3\lib`\
101-`java -Djava.ext.dirs=%DX_PATH%\ -jar %DX_PATH%\dx.jar --dex --verbose --output=.\bin\classes.dex .\classes`
118+```
119+SET DX_PATH=C:\android\sdk-windows\build-tools\23.0.3\lib
120+java -Djava.ext.dirs=%DX_PATH%\ -jar %DX_PATH%\dx.jar --dex --verbose --output=.\bin\classes.dex .\classes
121+```
102122
103123Converting java class files to dalwick files.
104124
1051256\. Create an unsigned .apk file by adding dalvik class files
106126
107-`set jdkdir=C:\Program Files\Java\jdk1.7.0_25\bin`\
108-`set ANDROID_HOME=C:\android\sdk-windows`\
109-`set APK_PROJECT_PATH=.`\
110-`set APP_NAME=testapp`
127+``` pascal
128+set jdkdir=C:\Program Files\Java\jdk1.7.0_25\bin
129+set ANDROID_HOME=C:\android\sdk-windows
130+set APK_PROJECT_PATH=.
131+set APP_NAME=testapp
132+```
111133
112-`del %APK_PROJECT_PATH%\bin\%APP_NAME%-unsigned.apk`\
113-`"%jdkdir%\java" -classpath %ANDROID_HOME%\tools\lib\sdklib.jar ^`\
114-` com.android.sdklib.build.ApkBuilderMain ^`\
115-` %APK_PROJECT_PATH%\bin\%APP_NAME%-unsigned.apk -v -u ^`\
116-` -z %APK_PROJECT_PATH%\bin\%APP_NAME%.ap_ ^`\
117-` -f %APK_PROJECT_PATH%\bin\classes.dex`
134+```
135+del %APK_PROJECT_PATH%\bin\%APP_NAME%-unsigned.apk
136+"%jdkdir%\java" -classpath %ANDROID_HOME%\tools\lib\sdklib.jar ^
137+ com.android.sdklib.build.ApkBuilderMain ^
138+ %APK_PROJECT_PATH%\bin\%APP_NAME%-unsigned.apk -v -u ^
139+ -z %APK_PROJECT_PATH%\bin\%APP_NAME%.ap_ ^
140+ -f %APK_PROJECT_PATH%\bin\classes.dex
141+```
118142
119143At the time of writing the script, there was no separate utility for adding classes.dex to the .apk file ... But here ApkBuilderMain is called (via a call to the java machine). The only thing it does is add classes.dex (created in step \# 5) to the root folder of the existing .apk file (created in step \# 3 .ap_) and write it to the -unsigned.apk file. A regular zip utility (like 7z) would probably do just as well.
120144
1211457\. Sign the .apk file. Attention! this script generates a new self-signed key every time. The program downloaded on Google Play must use the real key (this is also important for further program updates)
122146
123-`set jdkbindir=C:\Program Files\Java\jdk1.7.0_25\bin`\
124-`set APP_NAME=testapp`
147+``` pascal
148+set jdkbindir=C:\Program Files\Java\jdk1.7.0_25\bin
149+set APP_NAME=testapp
150+```
125151
126-`REM Generating on the fly a debug key`\
127-`"%jdkbindir%\keytool" -genkeypair -v -keystore bin\LCLDebugBKKey.keystore -alias LCLDebugBKKey ^`\
128-` -keyalg RSA -validity 10000 ^`\
129-` -dname "cn=Havefunsoft, o=company, c=US" ^`\
130-` -storepass "123456" -keypass "123456" -keysize 2048`
152+```
153+REM Generating on the fly a debug key
154+"%jdkbindir%\keytool" -genkeypair -v -keystore bin\LCLDebugBKKey.keystore -alias LCLDebugBKKey ^
155+ -keyalg RSA -validity 10000 ^
156+ -dname "cn=Havefunsoft, o=company, c=US" ^
157+ -storepass "123456" -keypass "123456" -keysize 2048
158+```
131159
132-`REM Signing the APK with a debug key`\
133-`del bin\%APP_NAME%-unaligned.apk`\
134-`"%jdkbindir%\jarsigner" -verbose ^`\
135-`  -sigalg SHA1withRSA ^`\
136-`  -digestalg SHA1 ^`\
137-`  -keystore bin\LCLDebugBKKey.keystore -keypass 123456 -storepass 123456 ^`\
138-`  -signedjar bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%-unsigned.apk LCLDebugBKKey`
160+```
161+REM Signing the APK with a debug key
162+del bin\%APP_NAME%-unaligned.apk
163+"%jdkbindir%\jarsigner" -verbose ^
164+ -sigalg SHA1withRSA ^
165+ -digestalg SHA1 ^
166+ -keystore bin\LCLDebugBKKey.keystore -keypass 123456 -storepass 123456 ^
167+ -signedjar bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%-unsigned.apk LCLDebugBKKey
168+```
139169
1401707.1 signature verification. (optional step)
141171
142-`set jdkbindir=C:\Program Files\Java\jdk1.7.0_25\bin`\
143-`set APP_NAME=testapp`
172+``` pascal
173+set jdkbindir=C:\Program Files\Java\jdk1.7.0_25\bin
174+set APP_NAME=testapp
175+```
144176
145-`REM Signing the APK with a debug key`\
146-`"%jdkbindir%\jarsigner" -verify -verbose -certs bin\%APP_NAME%-unaligned.apk`
177+```
178+REM Signing the APK with a debug key
179+"%jdkbindir%\jarsigner" -verify -verbose -certs bin\%APP_NAME%-unaligned.apk
180+```
147181
1481828\. Align the resulting .apk. (Alignment should occur AFTER the signature, since the signature makes the apk file unaligned)
149183
150-`set APP_NAME=testapp`\
151-`set buildtoolsdir=C:\android\sdk-windows\build-tools\23.0.3`
184+```
185+set APP_NAME=testapp
186+set buildtoolsdir=C:\android\sdk-windows\build-tools\23.0.3
187+```
152188
153-`REM Align the final APK package`\
154-`%buildtoolsdir%\zipalign -v 4 bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%.apk`
189+```
190+REM Align the final APK package
191+%buildtoolsdir%\zipalign -v 4 bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%.apk
192+```
155193
156194On completion of the 8th step, the resulting .apk file can be downloaded directly to the device.
157195
1581969\. Downloading any .apk file can be downloaded to the device if it is connected via USB and developer rights are allowed on it. The SDK includes an adb utility that allows you to uninstall the application and install a new .apk. (If the application is already installed, then the new .apk will not be loaded, and you must first uninstall it using uninstall)
159197
160-`C:\android\sdk-windows\platform-tools\adb.exe uninstall zengl.demo01`\
161-`C:\android\sdk-windows\platform-tools\adb.exe install bin\testapp.apk`
198+```
199+C:\android\sdk-windows\platform-tools\adb.exe uninstall zengl.demo01
200+C:\android\sdk-windows\platform-tools\adb.exe install bin\testapp.apk
201+```
162202
163203## See also
164204
165205- [[Android|Cross compiler instructions]] for building cross compiler on Android.
166206
167207---
168208*From the Free Pascal wiki, page "Android Build APK", as it stood on 2020-08-06 after a change by Trev, 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).*
169209Category: Android
170210Category: FPC

Back to history