为 Android 编译 MuPDF 查看器
先決條件
你需要一個 工作的 Android 開發環境,它由 Android SDK 和 Android NDK 組成。建立這一環境最簡單的方式就是使用 Android Studio 下載并安裝 SDK 和 NDK。確保 Android/Sdk/tools 和 Android/Sdk/ndk-bundle 目錄在你的 PATH 上。
你也需要 Oracle 的 Java JDK(OpenJDK 與 Android 不兼容)。你也需要 Apache Ant 構建系統。你也需要 Git,GNU Make,和一個 C 編譯器。
如果一切正常,你應該也能夠在命令中運行如下這些命令:
- Android SDK tools: android, emulator, and adb.
- Android NDK tools: ndk-build.
- Oracle Java JDK 8: java, and javac.
- Apache Ant: ant.
- Git: git.
- GNU Make: make, or gmake.
- C compiler: cc, gcc, or clang.
構建
使用 Git 下載工程(不要忘了加上 –recursive 標記):
$ git clone --recursive git://git.ghostscript.com/mupdf-android-viewer-mini.git加上 –recursive 標記是為了讓 Git 可以遞歸地下載 mupdf-android-viewer-mini 工程及其依賴的所有工程。MuPDF 由于牽涉到多個模塊,因而采用了 Git 的 submodule 機制來管理這些模塊。在 mupdf-android-viewer-mini 工程的根目錄下,有一個名為 .gitmodules 描述了它依賴的子模塊:
[submodule "jni"]path = jniurl = ../mupdf-android-fitz.git在下載代碼時,加了 –recursive 標記,Git 在下載完 mupdf-android-viewer-mini 工程之后,就會下載 mupdf-android-fitz 工程,并把它放在 mupdf-android-viewer-mini 工程的 jni 子目錄下。而在 mupdf-android-fitz 工程的根目錄下,同樣有一個 .gitmodules 文件,描述 mupdf-android-fitz 工程依賴的模塊:
[submodule "thirdparty/jbig2dec"]path = thirdparty/jbig2decurl = ../jbig2dec.git [submodule "thirdparty/mujs"]path = thirdparty/mujsurl = ../mujs.git [submodule "thirdparty/freetype"]path = thirdparty/freetypeurl = ../thirdparty-freetype2.git [submodule "thirdparty/harfbuzz"]path = thirdparty/harfbuzzurl = ../thirdparty-harfbuzz.git [submodule "thirdparty/jpeg"]path = thirdparty/libjpegurl = ../thirdparty-libjpeg.git [submodule "thirdparty/lcms2"]path = thirdparty/lcms2url = ../thirdparty-lcms2.git [submodule "thirdparty/openjpeg"]path = thirdparty/openjpegurl = ../thirdparty-openjpeg.git [submodule "thirdparty/zlib"]path = thirdparty/zliburl = ../thirdparty-zlib.git [submodule "thirdparty/curl"]path = thirdparty/curlurl = ../thirdparty-curl.git [submodule "thirdparty/freeglut"]path = thirdparty/freegluturl = ../thirdparty-freeglut.gitGit 在下載完 mupdf-android-fitz 工程之后,還會下載這些模塊,并放在 mupdf-android-fitz 工程目錄的 thirdparty 目錄下,即 mupdf-android-viewer-mini/jni/libmupdf 目錄下。
直接使用 Git 的 –recursive 標記下載,與如下的命令序列是等價的:
$ git clone git://git.ghostscript.com/mupdf-android-viewer-mini.git $ cd mupdf-android-viewer-mini $ git submodule update --init $ cd jni $ git submodule update --init $ cd libmupdf $ git submodule update --init在開始構建之前,還需要在 mupdf-android-viewer-mini 工程的根目錄下創建 local.properties 文件,配置 Android SDK 和 NDK 的路徑:
## This file is automatically generated by Android Studio. # Do not modify this file -- YOUR CHANGES WILL BE ERASED! # # This file must *NOT* be checked into Version Control Systems, # as it contains information specific to your local configuration. # # Location of the SDK. This is only used by Gradle. # For customization when using a Version Control System, please read the # header note. #Tue Jun 05 19:57:20 CST 2018 sdk.dir=/home/hanpfei0306/data/dev_tools/Android/Sdk ndk.dir=/home/hanpfei0306/data/dev_tools/Android/android-ndk-r12b此外,還需要在 mupdf-android-viewer-mini/jni/libmupdf 目錄下執行 make generate 命令生成必要的文件:
mupdf-android-viewer-mini/jni/libmupdf $ make generate否則,如果直接執行 make 進行構建,將很快報出找不到某些文件的錯誤:
[mips64] Compile : mupdf_core <= bbox-device.c[mips64] Compile : mupdf_core <= draw-mesh.cIn file included from /media/data/osprojects/mupdf-android-viewer-mini/jni/libmupdf/platform/java/mupdf_native.c:10:0:/media/data/osprojects/mupdf-android-viewer-mini/jni/libmupdf/include/mupdf/pdf.h:10:34: fatal error: mupdf/pdf/name-table.h: No such file or directory#include "mupdf/pdf/name-table.h"^compilation terminated./mmedia/data/dev_tools/Android/android-ndk-r12b/build/core/build-bake[1]: *** [/media/data/osprojects/mupdf-androiid-viewer-mini/jni/build/intenary.mk:472: recirmediates/ndkBuild/release/obj/local/mips64p/objs/mupdf_java//media/data/osprojecets/mupdf-android-viewer-mini/jni/libmupdf/platform/java/mupdf_nati vfeor .o] Error 1target '/media/data/osprojects/mupdf-android-viewer-mini/jni/build/intermediates/ndkBuild/release/obj/local/mips64/objs/mupdf_java//media/data/osprojects/mupdf-android-viewer-mini/jni/libmupdf/platform/java/mupdf_native.o' failedmake[1]: *** 正在等待未完成的任務....make[1]: Leaving directory '/media/data/osprojects/mupdf-android-viewer-mini/jni之后就可以在 mupdf-android-viewer-mini 工程的根目錄下執行如下命令來構建了:
$ make查看 mupdf-android-viewer-mini 工程的 Makefile 文件的內容:
# This is a very simple Makefile that calls 'gradlew' to do the heavy lifting. # # The tool 'adb' must be on the path, so that we can find the Android SDK.ANDROID_HOME := $(shell which adb | sed 's,/platform-tools/adb,,')default: assembleDebug release: assembleRelease install: installDebugassembleDebug:ANDROID_HOME=$(ANDROID_HOME) ./gradlew assembleDebug assembleRelease:ANDROID_HOME=$(ANDROID_HOME) ./gradlew assembleRelease installDebug:ANDROID_HOME=$(ANDROID_HOME) ./gradlew installDebug lint:ANDROID_HOME=$(ANDROID_HOME) ./gradlew lint archive:ANDROID_HOME=$(ANDROID_HOME) ./gradlew uploadArchives sync: archiversync -av MAVEN/com/ ghostscript.com:/var/www/maven.ghostscript.com/com/run: installadb shell am start -n com.artifex.mupdf.mini.app/.LibraryActivityclean:rm -rf .gradle buildrm -rf jni/.externalNativeBuild jni/.gradle jni/buildrm -rf lib/.gradle lib/buildrm -rf app/.gradle app/build執行 make 只是調用了 gradle 命令 ./gradlew assembleDebug。
執行上面的 make 命令,在執行到為 armeabi ABI 編譯動態鏈接庫時將報錯:
. . . . . ./tmp/ccrxybCM.s:4815: Error: cannot honor width suffix -- `add r4,r4,r2'/tmp/ccrxybCM.s:4824: Error: cannot honor width suffix -- `mul r2,r3'/tmp/ccrxybCM.s:4827: Error: lo register required -- `add r3,r3,#128'/tmp/ccrxybCM.s:4828: Error: cannot honor width suffix -- `asr r3,r3,#8'/tmp/ccrxybCM.s:4876: Error: cannot honor width suffix -- `mov r0,#0'/tmp/ccrxybCM.s:4937: Error: cannot honor width suffix -- `mov r4,#0'/tmp/ccrxybCM.s:5010: Error: cannot honor width suffix -- `mov r1,#1'/tmp/ccrxybCM.s:5011: Error: cannot honor width suffix -- `mov r2,#44'make[1]: *** [/media/data/osprojects/mupdf-android-viewer-mini/jni/build/intermediates/ndkBuild/release/obj/local/armeabi/objs/mupdf_core//media/data/osprojects/mupdf-android-viewer-mini/jni/libmupdf/source/fitz/draw-scale-simple.o] Error 1make[1]: *** 正在等待未完成的任務....make[1]: Leaving directory '/media/data/osprojects/mupdf-android-viewer-mini/jni'* Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.BUILD FAILEDTotal time: 4 mins 5.65 secs Makefile:12: recipe for target 'assembleDebug' failed make: *** [assembleDebug] Error 1默認情況下,./gradlew assembleDebug 將為 ‘armeabi’,’armeabi-v7a’,’arm64-v8a’,’x86’,’x86_64’,’mips’,’mips64’ 起種 ABI 構建動態鏈接庫,但實際上對某些 ABI 的支持完全沒有必要,比如早已過時的 ‘armeabi’,以及非常小眾的 ‘mips’,’mips64’。因而修改 mupdf-android-viewer-mini/jni/build.gradle 文件,配置 ndk.abiFilters,過濾掉不需要的 ABI:
. . . . . . android {compileSdkVersion 25buildToolsVersion '25.0.3'defaultConfig {minSdkVersion 16targetSdkVersion 25externalNativeBuild.ndkBuild.arguments '-j4'// Uncomment one of the following lines to limit builds to certain ABIs.ndk.abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'} . . . . . .無需編譯 armeabi 的 abi 之后,mupdf-android-viewer-mini 工程順利編譯通過:
. . . . . . :lib:transformNativeLibsWithStripDebugSymbolForDebug :lib:transformNativeLibsWithSyncJniLibsForDebug :lib:bundleDebug :lib:compileDebugSources :lib:assembleDebugBUILD SUCCESSFULTotal time: 1 mins 58.883 secs參考文檔:
How to build the MuPDF viewer for Android
總結
以上是生活随笔為你收集整理的为 Android 编译 MuPDF 查看器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Jenkins 在 Tomcat 中的部
- 下一篇: mupdf-android-viewer