Platform notes
Main thread and background work, onTrimMemory and large documents, ABIs and app size, and deployment notes for NexPDF for Android.
Threading
- Every NexPDF call is synchronous and runs on the thread that calls it. Opening a large file, rendering, OCR, signing and conversion can take long enough to drop frames or cause an ANR, so call them off the main thread.
- A document is not thread-safe. Use each
NexPdfDocument(or raw handle) from one thread at a time. Different documents can be used on different threads at the same time. The state shared between documents (the license and the renderer's caches) is thread-safe. - To work on the same content from two threads, open a second document from
doc.saveBytes(). - The license is process-wide: one
NexPdfLicense.initializecall covers every thread. - Errors are tracked per thread: read
NexPdfJni.lastError()on the thread that made the failing call.
A single-thread dispatcher keeps all calls on a document in order, without locks:
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.nexdevit.nexpdf.NexPdfDocument
import java.util.concurrent.Executors
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.withContext
val pdfThread = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
suspend fun open(bytes: ByteArray): NexPdfDocument = withContext(pdfThread) {
NexPdfDocument.openBytes(bytes)
}
suspend fun render(doc: NexPdfDocument, page: Int, density: Float): Bitmap? = withContext(pdfThread) {
val png = doc.renderPagePng(page, 72.0 * density) // one PDF point per dp
if (png.isEmpty()) null else BitmapFactory.decodeByteArray(png, 0, png.size)
}
// In an Activity or Fragment:
lifecycleScope.launch {
imageView.setImageBitmap(render(doc, 0, resources.displayMetrics.density))
}The reader already does this: it renders pages on its own background
thread, from a private copy of the document reopened after each change, so the main thread never
shares a native document with it. NexPdfReaderActivity.setEventListener delivers events on the
main thread. The listener is static and shared by every reader, so clear it with null when you no
longer need it.
Several processes
Each process loads its own copy of the engine and needs its own license activation. Android runs
Application.onCreate() in every process of the app, so activating there covers a :remote
service or any other process you declare.
Memory
- A document is read fully into memory when it is opened:
openBytestakes the whole file as abyte[]and the engine keeps its own copy, so budget at least twice the file size while it opens, plus the decoded pages you touch. Native memory does not count against the Java heap limit, but it does count towards the memory the system allows your app. - A page rendered with
renderPagePngis PNG data; decoded into aBitmapit takes width x height x 4 bytes. A full A4 page at 216 dpi is about 1786 x 2526 pixels, or 18 MB. Render at the size you show, userenderThumbPngfor thumbnails, and keep only the pages on screen. - The renderer keeps process-wide caches of parsed fonts, glyphs and decoded images, shared by every document so a font or logo used on many pages is decoded once. Each cache is bounded by size and drops its least recently used entries first; together they can hold up to about 1.3 GB in a process that renders many large, image-heavy files. Memory that stays high after rendering is this cache being reused, not a leak.
NexPdfJni.memoryUsageBytes(handle)estimates what a document uses.NexPdfJni.trimMemory(handle)compacts its internal buffers and drops objects nothing refers to any more, and returns the bytes reclaimed. The document stays usable.close()frees everything; a document that is never closed is never freed, becauseNexPdfDocumenthas no finalizer.
Call trimMemory from onTrimMemory, on the document's own thread:
override fun onTrimMemory(level: Int) {
super.onTrimMemory(level)
lifecycleScope.launch(pdfThread) { NexPdfJni.trimMemory(doc.handle()) }
bitmapCache.evictAll() // and drop your own rendered bitmaps
}The reader holds two native copies of the document (one for editing, one for rendering), renders pages at most 3072 pixels on the long side, and keeps a full copy of the PDF bytes for each of the last 25 edits for undo. Editing a very large document in the reader uses several times its size.
ABIs and app size
- The AAR has the engine for
arm64-v8a,armeabi-v7aandx86_64, as two libraries per ABI:libnexpdf_ffi.so(the engine) andlibnexpdf_jni.so(a small JNI bridge). The arm64 engine is about 20 MB uncompressed. 32-bit x86 is not included. - An Android App Bundle (
.aab) lets Google Play deliver only the device's ABI, so users download one engine, not three. This is the default for Play Store releases.
For APKs you distribute yourself, build one APK per ABI with ABI splits instead of one universal APK that carries all three:
android {
splits {
abi {
isEnable = true
reset()
include("arm64-v8a", "armeabi-v7a", "x86_64")
isUniversalApk = false
}
}
}- To drop an ABI entirely, for example
armeabi-v7aif you only target 64-bit devices, setndk { abiFilters += listOf("arm64-v8a", "x86_64") }. Devices of a dropped ABI cannot load NexPDF. - The 64-bit libraries are aligned for 16 KB memory pages, which Android 15 devices with 16 KB pages need.
- The engine compiles in the Liberation fonts (about 4 MB) used for non-embedded standard fonts and PDF/A.
- The AAR has no dependencies; the reader is plain Android views and needs no AndroidX.
Deployment
- Android 7.0 (API 24) or later.
- Android keys are bound to the package name (
applicationId): each app, and each build variant with its ownapplicationIdSuffix, needs a key issued for it. See License. - The AAR's keep rules for R8 and ProGuard are applied automatically; see Troubleshooting if you repackage the classes.
Network
The engine makes no network calls of its own: license keys are verified offline and documents
never leave the app. Only signing options you set (tsa_url for a timestamp, ltv for OCSP and CRL
lookups) and NexPdfJni.addLtv contact the servers named in the options and the certificates. The
AAR does not request the INTERNET permission; add it to your manifest if you use those options.