How to build the MuPDF viewer for Android

Prerequisites

You need a working Android development environment, consisting of the Android SDK and the Android NDK. The easiest way is to use Android Studio to download and install the SDK and NDK. Make sure that the Android/Sdk/tools and Android/Sdk/ndk-bundle directories are on your path.

You also need Oracle's Java JDK (OpenJDK is not compatible with Android). You also need the Apache Ant build system. You also need Git, GNU Make, and a C compiler.

If everything is working, you should be able to run these commands from the command line:

Android SDK tools: android, emulator, adb and apksigner.
Android NDK tools: ndk-build.
Oracle Java JDK 8: java, javac and keytool.
Git: git.
GNU Make: make, or gmake.
C compiler: cc, gcc, or clang.

Building

Download the project using Git (and don't forget the --recursive flag):

$ git clone --recursive git://git.ghostscript.com/mupdf-android-viewer-mini.git

If all tools have been installed as per the prerequisites, first use your host compiler to run the 'generate' step in the mupdf project:

$ make -C libmupdf generate

The build the app using the gradle wrapper:

$ ./gradlew assembleDebug

You may need to set ANDROID_HOME or add a local.properties configuration file to point to where you have installed the Android SDK.

Running

To run the app in the android emulator, first you'll need to set up an "Android Virtual Device" for the emulator. Run "android avd" and create a new device. You can also use Android Studio to set up a virtual device. Use the x86 ABI for best emulator performance.

Then launch the emulator, or connect a device with USB debugging enabled:

$ emulator -avd MyVirtualDevice &

Then copy some test files to the device:

$ adb push file.pdf /mnt/sdcard/Download

Then install the app on the device:

$ ./gradlew installDebug

To see the error and debugging message log:

$ adb logcat

Release

To release you MUST first change the package name. Do NOT use the com.artifex domain for your custom app!

Change all references to com.artifex.mupdf.mini into com.YourCompanyName.mupdf.mini in the Java source files and XML resources.

$ git mv src/com/artifex src/com/YourCompanyName
$ sed -i -e s,artifex,YourCompanyName,g AndroidManifest.xml
$ sed -i -e "s,package com.artifex,package com.YourCompanyName,g" src/com/YourCompanyName/mupdf/mini/*.java
$ sed -i -e s,artifex,YourCompanyName,g res/layout/*.xml

In order to sign a release build, you will need to create a key and a key store.

$ keytool -genkey -v -keystore android.keystore -alias MyKey -validity 3650 -keysize 2048 -keyalg RSA

Then add the following entries to gradle.properties:

RELEASE_KEY_STORE=android.keystore
RELEASE_KEY_STORE_PASSWORD=your keystore password
RELEASE_KEY_ALIAS=MyKey
RELEASE_KEY_ALIAS_PASSWORD=your key password

If your keystore has been set up properly, you can now build a release APK.

$ ./gradlew assembleRelease

First verify that the release APK has indeed been signed by the correct key.

apksigner verify --print-certs ./build/outputs/apk/mupdf-x86-debug.apk
apksigner verify --print-certs ./build/outputs/apk/mupdf-x86-release.apk

The debug APK should be signed by Android Debug while the release APK ought to be signed with the key you created above.

Finally install the release app on the device:

$ ./gradlew installRelease

Good Luck!