Sign in Join
Wiki copytemporary

Android Build APK/zh CN: what changed

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

OldNewLine
11这篇文章介绍了一种在FPC中编译一个工程并将其下载到Android设备的方法. 它认为该工程是一个加载在Java代码中的NDK库。当然,这个方法并没有绑定到任何的IDE扩展部分中(例如:Android4Laz),可单独地应用于任何工程.
22
33## 必备条件
44
55- FPC (附带有预制好的Android的rtl库).
66- JRE (Java运行时) - 需要JDK,以及来运行"java"的机器(用于执行包含在Android SDK中的实用程序)
77- JDK (需要安装javac ),JDK需要安装JRE.
88- Android NDK
99- Android SDK
1010
1111不需要附加的构建工具(例如,ant)
1212
1313### JRE / JDK 版本
1414
1515目前(2017年冬季),可下载仅受官方支持版本8(1.8).以前,你可以下载版本7.Android SDK的很多组件都绑定到JDK的具体版本.
1616
1717## 编译
1818
19190\. 它假设整个工程都在一个文件夹中,例如"mydemo".并且在android下用于构建的所有的脚本和实用程序都位于"mydemo \\ android".这只在接下来的具体指定-FE开关的步骤中起作用.
2020
21211\. 你需要编译工程(库).命令行(windows)可能看起来像这样.
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
3739你应该注意的关键点是:
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清单文件
4547
46483\. 创建.apk文件
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
6066该实用程序创建一个 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
6977如果程序有任何一些资源文件, 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\. 编译主程序的Java代码
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
87101在下一步中,已编译的Java代码必须被转换为Dalvik代码. It is possible that version "dx.jar" is expected for classes version 1.7, not 1.8 (which you can use). 在这种情况下,您需要javac来具体指定兼容版本:
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\. 转换Java代码为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
103123转换java类文件为dalwick文件.
104124
1051256\. 通过添加dalvik类文件来创建一个unsigned .apk文件
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\. 签名.apk文件. 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/zh CN", as it stood on 2021-01-11 after a change by Robsean, 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