Android Build APK/zh CN

From Free Pascal wiki
Jump to navigationJump to search

English (en) русский (ru) 中文(中国大陆)‎ (zh_CN)

这篇文章介绍了一种在FPC中编译一个工程并将其下载到Android设备的方法. 它认为该工程是一个加载在Java代码中的NDK库。当然,这个方法并没有绑定到任何的IDE扩展部分中(例如:Android4Laz),可单独地应用于任何工程.


  • FPC (附带有预制好的Android的rtl库).
  • JRE (Java运行时) - 需要JDK,以及来运行"java"的机器(用于执行包含在Android SDK中的实用程序)
  • JDK (需要安装javac ),JDK需要安装JRE.
  • Android NDK
  • Android SDK


JRE / JDK 版本

目前(2017年冬季),可下载仅受官方支持版本8(1.8).以前,你可以下载版本7.Android SDK的很多组件都绑定到JDK的具体版本.


0. 它假设整个工程都在一个文件夹中,例如"mydemo".并且在android下用于构建的所有的脚本和实用程序都位于"mydemo \ android".这只在接下来的具体指定-FE开关的步骤中起作用.

1. 你需要编译工程(库).命令行(windows)可能看起来像这样.

C:\FPC\3.1.1\bin\i386-win32\fpc.exe ^
-Tandroid -Parm -MDelphi ^
-Scghi -CX -O3 -Xs -XX -l -vbq ^
-FUlib\arm-android ^
-oandroid\ ^
-FE.\Android\files\lib\armeabi\ ^
-CpARMv6 ^ 
-CfVFPv2 ^
-Xd ^
-XParm-linux-androideabi- ^
-FDC:\android\ndk-r11c\toolchains\arm-linux-androideabi-4.9\prebuilt\windows\bin ^
-FuC:\android\ndk-r11c\platforms\android-9\arch-arm\usr\lib ^


  • -FD - specifies the path where build utilities are stored
  • -XParm-linux-androideabi- - specifies the prefix for the build utilities
  • -Fu - pointing to NDK libraries - required for linking
  • -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.

2. 准备Prepare the AndroidManifest.xml清单文件

3. 创建.apk文件

set buildtoolsdir=C:\android\sdk-windows\build-tools\23.0.3
set APP_NAME=testapp
set APK_SDK_PLATFORM=C:\android\sdk-windows\platforms\android-11
rem -S res - fails, if "res" directory doesn't exist
rem generate-dependencies is needed to generate file
%buildtoolsdir%\aapt p -v -f -M AndroidManifest.xml ^
 -F bin\%APP_NAME% .ap_ ^
 --generate-dependencies ^
 -I%APK_SDK_PLATFORM%\android.jar -S res -m -J gen ^

该实用程序创建一个 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 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\" 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:

... all other folders/files that were located in "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:

%buildtoolsdir%\aapt ... ^
  dir1 dir2 dir3

4. 编译主程序的Java代码

set jdkbindir=C:\Program Files\Java\jdk1.7.0_25\bin
set androidjar=C:\android\sdk-windows\platforms\android-11\android.jar 
"%jdkbindir%\javac.exe" ^
  .\zengl\android\ ^
  .\zengl\demo01\ ^
  -cp "%androidjar%" ^

It 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.

在下一步中,已编译的Java代码必须被转换为Dalvik代码. It is possible that version "dx.jar" is expected for classes version 1.7, not 1.8 (which you can use). 在这种情况下,您需要javac来具体指定兼容版本:

set jdkbindir= C:\Program Files\Java\jdk1.8.0_25\bin
set androidjar=C:\android\sdk-windows\platforms\android-11\android.jar 
"%jdkbindir%\javac.exe" ^
  .\zengl\android\ ^
  .\zengl\demo01\ ^
  -cp "%androidjar%" ^
  -d.\classes -target 1.7 -source 1.7

5. 转换Java代码为dalvik代码

SET DX_PATH=C:\android\sdk-windows\build-tools\23.0.3\lib
java -Djava.ext.dirs=%DX_PATH%\ -jar %DX_PATH%\dx.jar --dex --verbose --output=.\bin\classes.dex .\classes


6. 通过添加dalvik类文件来创建一个unsigned .apk文件

set jdkdir=C:\Program Files\Java\jdk1.7.0_25\bin
set ANDROID_HOME=C:\android\sdk-windows
set APP_NAME=testapp
del %APK_PROJECT_PATH%\bin\%APP_NAME%-unsigned.apk
"%jdkdir%\java" -classpath %ANDROID_HOME%\tools\lib\sdklib.jar ^ ^
 %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

At 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.

7. 签名.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)

set jdkbindir=C:\Program Files\Java\jdk1.7.0_25\bin
set APP_NAME=testapp
REM Generating on the fly a debug key
"%jdkbindir%\keytool" -genkeypair -v -keystore bin\LCLDebugBKKey.keystore -alias LCLDebugBKKey ^
 -keyalg RSA -validity 10000 ^
 -dname "cn=Havefunsoft, o=company, c=US" ^
 -storepass "123456" -keypass "123456" -keysize 2048
REM Signing the APK with a debug key
del bin\%APP_NAME%-unaligned.apk
"%jdkbindir%\jarsigner" -verbose ^
  -sigalg SHA1withRSA ^
  -digestalg SHA1 ^
  -keystore bin\LCLDebugBKKey.keystore -keypass 123456 -storepass 123456 ^
  -signedjar bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%-unsigned.apk LCLDebugBKKey

7.1 signature verification. (optional step)

set jdkbindir=C:\Program Files\Java\jdk1.7.0_25\bin
set APP_NAME=testapp
REM Signing the APK with a debug key
"%jdkbindir%\jarsigner" -verify -verbose -certs bin\%APP_NAME%-unaligned.apk

8. Align the resulting .apk. (Alignment should occur AFTER the signature, since the signature makes the apk file unaligned)

set APP_NAME=testapp
set buildtoolsdir=C:\android\sdk-windows\build-tools\23.0.3
REM Align the final APK package
%buildtoolsdir%\zipalign -v 4 bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%.apk

On completion of the 8th step, the resulting .apk file can be downloaded directly to the device.

9. 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)

C:\android\sdk-windows\platform-tools\adb.exe uninstall zengl.demo01
C:\android\sdk-windows\platform-tools\adb.exe install bin\testapp.apk

See also