如何在Android环境中运行PenAndPdf开源项目?
Hey there! I’ve messed around with PenAndPdf a bit before, so let me walk you through the steps to get it up and running in Android Studio without pulling your hair out.
1. First, Get Your Environment Ready
PenAndPdf relies on MuPDF and native C code, so you’ll need to make sure these tools are set up correctly:
- NDK Version: The project is built around a specific NDK version (usually something like r21e or r23b—check the project’s
build.gradleor README for the exact one). Download it via Android Studio’s SDK Manager (SDK Tools -> Show Package Details -> NDK (Side by side)). - CMake: You’ll need CMake to compile the native code. Grab it from the SDK Manager under SDK Tools.
- Android Studio Stable Build: Avoid canary versions—stick to a stable release like Arctic Fox, Bumblebee, or newer to minimize compatibility headaches.
2. Fix Project Config After Import
Once you’ve pulled the code from GitHub and imported it into Android Studio, fix these common configuration issues first:
- Update
local.properties: Make sure this file has the correct paths to your SDK and NDK. If it’s missing, create it in the project root and add lines like:sdk.dir=/path/to/your/android/sdk ndk.dir=/path/to/your/android/ndk/your-ndk-version - Check Gradle Versions: Open the project-level
build.gradleand ensure the Android Gradle Plugin version matches your installed Gradle version. For example, if you’re using Gradle 7.4, the plugin should becom.android.tools.build:gradle:7.4.0. Update one to match the other if there’s a mismatch. - Verify MuPDF Module: PenAndPdf depends on the MuPDF submodule. Check that
settings.gradleincludesinclude ':mupdf'—if not, add it and sync the project. If the MuPDF code is missing (submodules sometimes don’t clone automatically), rungit submodule update --init --recursivein your project’s root directory via terminal.
3. Squash Common Compilation & Run Errors
Here’s how to fix the most annoying issues you’ll likely hit:
- Native Code Compilation Failures: If you get errors about missing headers or undefined symbols, check the
CMakeLists.txtfiles in both the main app and MuPDF modules. Ensure paths to MuPDF’s include directories are correct. Also, in the app-levelbuild.gradle, underdefaultConfig.externalNativeBuild.cmake, setabiFiltersto architectures MuPDF supports:abiFilters 'armeabi-v7a', 'arm64-v8a' - Gradle Sync Issues: If sync fails repeatedly, try clearing Android Studio’s cache (File -> Invalidate Caches... -> Invalidate and Restart). You can also delete the
.gradleand.ideafolders in your project root, then re-sync. - Runtime Missing
.soFiles: If the app crashes on launch complaining about missing native libraries, make sure the MuPDF module is compiling correctly and generating.sofiles in thejniLibsdirectory. Check that the app module is set to include these libraries in its build. - Permission Errors: PenAndPdf needs storage access to open PDFs. Ensure the
AndroidManifest.xmlincludes:
For Android 10+, you’ll need to handle scoped storage or request the<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>MANAGE_EXTERNAL_STORAGEpermission if needed.
4. Finally, Run the App
- Use a Real Device (If Possible): Emulators can be finicky with native code, especially for arm architectures. If you must use an emulator, choose an x86_64 image with Android 10 or higher, and enable ARM translation (some emulators have this built-in now).
- Hit Run: If everything is set up right, select your device/emulator and click the run button. If it still fails, check the Logcat for specific error messages—they’ll tell you exactly what’s broken (e.g., missing dependencies, permission denials).
Pro tip: If you’re stuck on a specific error, copy the full log from the Build window or Logcat and search for it—chances are someone else has run into the same problem and posted a fix!
内容的提问来源于stack exchange,提问作者Korab Muhadri




