Skip to content
imaged

Java setup

Android 12+ · arm64-v8a

Slab ships for Android as an AAR:

android/
  slab.aar     Java API (dev.slab.*) + libslab.so (arm64-v8a) + R8 rules
  slab.h       C header, for native (NDK) code calling libslab.so directly

Kotlin and Java use the same dev.slab classes. Minimums: Android 12 (API 31), arm64-v8a.

Examples (Kotlin): Classify, Faces, Chat, CameraDetector.

Add to an app

Copy slab.aar into app/libs/ and add it to the app module:

dependencies {
    implementation(files("libs/slab.aar"))
}

The AAR's R8 rules keep Slab's native methods when the app is minified.

Use

import dev.slab.*

Slab.bind(context)   // once per process, e.g. in Application.onCreate

val config = SlabConfig()
    .setCacheDir(context.cacheDir.path)
    .setRuntimeLicense(licenseString)

// Off the main thread:
SlabSession(config, modelPath).use { session ->
    SlabFrame.fromFile(imagePath).use { frame ->
        val output = session.run(SlabInput().setFrame(frame))
        for (c in output.topK(5)) println("${c.label} ${c.score}")
    }
}

Java is the same API:

try (SlabSession session = new SlabSession(config, modelPath);
     SlabFrame frame = SlabFrame.fromFile(imagePath);
     SlabInput input = new SlabInput().setFrame(frame);
     SlabOutput output = session.run(input)) {
    for (SlabOutput.Classification c : output.topK(5)) Log.i("slab", c.label + " " + c.score);
}

Model files must be on the file system: copy them from assets or download them to app storage first.

API

The classes are the same five objects as in every Slab API:

Class Notes
Slab bind(context), version(), constants (BACKEND_*, PIXEL_*, RESIZE_*, FINISH_*, DTYPE_*)
SlabConfig setModelsDir, setCacheDir, setBackend, setThreads, setRuntimeLicense, licenseStatus, licenseExpiresAt, probe(backend); protected models: setUnlockKey, setModelLicenseToken. Hexagon NPU: setSkelDir
SlabSession SlabSession(config, path) opens at once. run, generate(input, output, listener), start / nextToken, cancel (any thread), reset, inspect, backendProblem, inputs(), outputs(), labels(), meta(key); preprocessing overrides setResize, setColorBgr, setNormalization
SlabFrame fromFile, fromEncoded(bytes), fromBitmap, fromBuffer(direct, w, h, format, rowStride, orientation) for camera planes, fromPixels(bytes...), crop (a view, no copy)
SlabInput setFrame, addBatchFrame, setTensor(index or name, float[] / long[] / byte[]), setTensorF16; prompts: setPrompt, addMessage(role, text[, image]), setMaxTokens, setTemperature, setJsonSchema, setImageTiled ...
SlabOutput topK, boxes (Box: left/top/right/bottom in image pixels, landmarks as x, y pairs), floats / longs / bytes, shape, normalizeL2, cosineSimilarity, toFrame, text, fullText, finishReason, token counts, latencyMs, backendUsed
  • Failures throw SlabException (code; licenseReason for license errors).
  • Every object is AutoCloseable: use use { } (Kotlin) or try-with-resources (Java). Unclosed objects are freed when garbage collected.
  • One operation at a time per session; separate sessions run in parallel. Run sessions on a background thread, never the main thread.

Licensing

The runtime license is bound to your app's package name and signing certificates. With Play App Signing, the license must name Google's app signing certificate, not your upload key. Call Slab.bind(context) before setRuntimeLicense.

Hexagon NPU

The NPU on Snapdragon devices needs Qualcomm's QNN HTP libraries from one QAIRT SDK release in the app, and a writable folder holding the skel libraries passed to setSkelDir. Without them, Slab uses the GPU (Vulkan) or CPU.