Sign in Join
Wiki copytemporary

Android Build APK: what changed

Revision 2 to 3. 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-```
23+```text
2424C:\FPC\3.1.1\bin\i386-win32\fpc.exe ^
2525-Tandroid -Parm -MDelphi ^
2626-Scghi -CX -O3 -Xs -XX -l -vbq ^
2727-FUlib\arm-android ^
2828-oandroid\mydemo.so ^
2929-FE.\Android\files\lib\armeabi\ ^
3030-CpARMv6 ^
3131-CfVFPv2 ^
3232-Xd ^
3333-XParm-linux-androideabi- ^
3434-FDC:\android\ndk-r11c\toolchains\arm-linux-androideabi-4.9\prebuilt\windows\bin ^
3535-FuC:\android\ndk-r11c\platforms\android-9\arch-arm\usr\lib ^
3636mydemo.lpr
3737```
3838
3939What you should pay attention to. Keys:
4040
4141- -FD - specifies the path where build utilities are stored
4242- -XParm-linux-androideabi- - specifies the prefix for the build utilities
4343- -Fu - pointing to NDK libraries - required for linking
4444- -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.
4545
46462\. Prepare the AndroidManifest.xml manifest file
4747
48483\. Create the .apk file
4949
50-```
50+```text
5151set buildtoolsdir=C:\android\sdk-windows\build-tools\23.0.3
5252set APP_NAME=testapp
5353set APK_SDK_PLATFORM=C:\android\sdk-windows\platforms\android-11
5454rem -S res - fails, if "res" directory doesn't exist
5555rem generate-dependencies is needed to generate R.java file
5656```
5757
58-```
58+```text
5959%buildtoolsdir%\aapt p -v -f -M AndroidManifest.xml ^
6060 -F bin\%APP_NAME% .ap_ ^
6161 --generate-dependencies ^
6262 -I%APK_SDK_PLATFORM%\android.jar -S res -m -J gen ^
6363 files
6464```
6565
6666The 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:
6767
68-```
68+```text
6969AndroidManifest.xml
7070resources.arsc
7171res
7272lib
7373lib\armeabi
7474... all other folders/files that were located in "files"
7575```
7676
7777If 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:
7878
79-```
79+```text
8080%buildtoolsdir%\aapt ... ^
8181 dir1 dir2 dir3
8282```
8383
84844\. Compile Java code of the main program
8585
8686``` pascal
8787set jdkbindir=C:\Program Files\Java\jdk1.7.0_25\bin
8888set androidjar=C:\android\sdk-windows\platforms\android-11\android.jar
8989```
9090
91-```
91+```text
9292"%jdkbindir%\javac.exe" ^
9393 .\zengl\android\ZenGL.java ^
9494 .\zengl\demo01\Demo01Activity.java ^
9595 -cp "%androidjar%" ^
9696 -d.\classes
9797```
9898
9999It 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.
100100
101101In 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:
102102
103103``` pascal
104104set jdkbindir= C:\Program Files\Java\jdk1.8.0_25\bin
105105set androidjar=C:\android\sdk-windows\platforms\android-11\android.jar
106106```
107107
108-```
108+```text
109109"%jdkbindir%\javac.exe" ^
110110 .\zengl\android\ZenGL.java ^
111111 .\zengl\demo01\Demo01Activity.java ^
112112 -cp "%androidjar%" ^
113113 -d.\classes -target 1.7 -source 1.7
114114```
115115
1161165\. Convert Java code to dalvik
117117
118-```
118+```text
119119SET DX_PATH=C:\android\sdk-windows\build-tools\23.0.3\lib
120120java -Djava.ext.dirs=%DX_PATH%\ -jar %DX_PATH%\dx.jar --dex --verbose --output=.\bin\classes.dex .\classes
121121```
122122
123123Converting java class files to dalwick files.
124124
1251256\. Create an unsigned .apk file by adding dalvik class files
126126
127127``` pascal
128128set jdkdir=C:\Program Files\Java\jdk1.7.0_25\bin
129129set ANDROID_HOME=C:\android\sdk-windows
130130set APK_PROJECT_PATH=.
131131set APP_NAME=testapp
132132```
133133
134-```
134+```text
135135del %APK_PROJECT_PATH%\bin\%APP_NAME%-unsigned.apk
136136"%jdkdir%\java" -classpath %ANDROID_HOME%\tools\lib\sdklib.jar ^
137137 com.android.sdklib.build.ApkBuilderMain ^
138138 %APK_PROJECT_PATH%\bin\%APP_NAME%-unsigned.apk -v -u ^
139139 -z %APK_PROJECT_PATH%\bin\%APP_NAME%.ap_ ^
140140 -f %APK_PROJECT_PATH%\bin\classes.dex
141141```
142142
143143At 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.
144144
1451457\. 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)
146146
147147``` pascal
148148set jdkbindir=C:\Program Files\Java\jdk1.7.0_25\bin
149149set APP_NAME=testapp
150150```
151151
152-```
152+```text
153153REM Generating on the fly a debug key
154154"%jdkbindir%\keytool" -genkeypair -v -keystore bin\LCLDebugBKKey.keystore -alias LCLDebugBKKey ^
155155 -keyalg RSA -validity 10000 ^
156156 -dname "cn=Havefunsoft, o=company, c=US" ^
157157 -storepass "123456" -keypass "123456" -keysize 2048
158158```
159159
160-```
160+```text
161161REM Signing the APK with a debug key
162162del bin\%APP_NAME%-unaligned.apk
163163"%jdkbindir%\jarsigner" -verbose ^
164164 -sigalg SHA1withRSA ^
165165 -digestalg SHA1 ^
166166 -keystore bin\LCLDebugBKKey.keystore -keypass 123456 -storepass 123456 ^
167167 -signedjar bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%-unsigned.apk LCLDebugBKKey
168168```
169169
1701707.1 signature verification. (optional step)
171171
172172``` pascal
173173set jdkbindir=C:\Program Files\Java\jdk1.7.0_25\bin
174174set APP_NAME=testapp
175175```
176176
177-```
177+```text
178178REM Signing the APK with a debug key
179179"%jdkbindir%\jarsigner" -verify -verbose -certs bin\%APP_NAME%-unaligned.apk
180180```
181181
1821828\. Align the resulting .apk. (Alignment should occur AFTER the signature, since the signature makes the apk file unaligned)
183183
184-```
184+```text
185185set APP_NAME=testapp
186186set buildtoolsdir=C:\android\sdk-windows\build-tools\23.0.3
187187```
188188
189-```
189+```text
190190REM Align the final APK package
191191%buildtoolsdir%\zipalign -v 4 bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%.apk
192192```
193193
194194On completion of the 8th step, the resulting .apk file can be downloaded directly to the device.
195195
1961969\. 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)
197197
198-```
198+```text
199199C:\android\sdk-windows\platform-tools\adb.exe uninstall zengl.demo01
200200C:\android\sdk-windows\platform-tools\adb.exe install bin\testapp.apk
201201```
202202
203203## See also
204204
205205- [[Android|Cross compiler instructions]] for building cross compiler on Android.
206206
207207---
208208*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).*
209209Category: Android
210210Category: FPC

Back to history