[UPDATE] This post is no longer valid due to MuPDF licensing, please visit official mupdf.com for complete information
Hello guys, today i’d like to share my experience about building mupdf for android. I need to use mupdf library to read pdf file in my Android Magazine app. Building source to executable binary or .so file is a little bit tricky, that’s depends on your host environment whether on Windows, Mac or Linux OS. I’m using Linux Ubuntu 12.04 , Android Studio 1.4 , Android SDK and Android NDK r8e. Basically, i didn’t directly import android platform included in mupdf source. I’ve tried that before but there are a lot of error that Android Studio complaining to missing file error, gradle wrapper error etc. So, i tried to build mupdf static library first, create new project in Android Studio and then copy prebuilt static file to jniLibs directory. Maybe this isn’t efficient way but it works for and i hope it helps someone who get problem using mupdf on their own project.
- Get the source, go to mupdf.com, in the download section you will get
Default1git clone --recursive git://git.ghostscript.com/mupdf.git
- Open terminal, and run that git command. When done, go to mupdf directory.
- Make sure your mupdf directory contains
- Make generated directory, by command make generated
- And then make . You will see some warning, but as long as it doesn’t stop compiling just wait until finish.
- Go to /platform/android/ directory.
- Copy local.properties.sample to local.properties . And then uncomment and edit sdk.dir to path where your android sdk located, for example sdk.dir=/home/abhie/android-sdk
- Make build.sh to executable file, chmod +x build.sh
- Run ./build.sh
- After compiling finish, the static library .so file will be available in /libs/ directory. By default .so file compiled for armeabi-v7a architecture. If you plan to support devices with different architecture, you can change it in /jni/Application.mk by uncomment APP_PLATFORM and APP_ABI.
- Now create new project or new module in Android Studio. Make sure your package is com.artifex.mupdfdemo, if you have different package you will get UnsatisfiedLinkError, because the static library compiled with this package as identifier. If you want to change this update /jni/mupdf.c line 18-19 to your package name in the same format. And run ./build.sh again.
- Copy all source in src directory (platform/android/src) to your module. And don’t forget res/ directory too.
- Edit AndroidManifest.xml, and make sure you register all activity according to /mupdf/platform/android/AndroidManifest.xml .
- In your module inside Android Studio, create jniLibs directory in src/main directory, yes folder name should be exactly jniLibs.
- Copy .so file from /platform/android/libs to jniLibs.
- Try rebuild project, if no errors you can run to real devices.