Skip to content
imaged

Kotlin 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). 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, fromImage(image, rotationDegrees) for Android YUV camera images, fromYuv(...) for YUV planes, fromBuffer(direct, w, h, format, rowStride, orientation) for packed pixels, 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

List the app's applicationId, including any applicationIdSuffix, in its runtime license. Each debug build or flavor with a different ID must be listed explicitly as a variant of the same product. New licenses do not require signing-certificate digests or Play App Signing details.

Call Slab.bind(context) before setRuntimeLicense. See Licenses & renewals.

Device requirements

The native library supports arm64-v8a and is aligned for 16 KB memory pages. Vulkan GPU execution requires Vulkan 1.1, a compute queue, storage buffers of at least 64 MB, 128-byte push constants and host-visible memory. See Platforms & requirements for tested devices.

Hexagon NPU

Qualcomm's QNN libraries are not included in slab.aar. For a Snapdragon device with Hexagon HTP, bundle libQnnHtp.so, libQnnHtpPrepare.so, libQnnSystem.so and the chip's HTP stub and skel library from one Qualcomm AI Runtime SDK release, under Qualcomm's license. Pass a writable folder containing the skel libraries to setSkelDir.

Tested with QAIRT 2.45 on Hexagon v73 (Snapdragon 8 Gen 2) and v79 (Snapdragon 8 Elite). The NPU takes a model with a single float32 input. In automatic mode, Slab falls back to available GPU or CPU execution when QNN is absent. An explicitly requested backend must be available and support the model.

License details

Slab 0.2.0 exposes the attached license on Config and Session. Use it to display expiry and send renewal reminders to whoever maintains the license.

Config member Value
licenseSecondsLeft() Seconds remaining
licenseExpiresAt() Expiry, Unix seconds
licenseNotBefore() Start of validity, Unix seconds
licenseIssuedAt() Issue time, Unix seconds
licenseKind() License kind
licenseFeatures() Enabled feature flags
licenseId() License ID

Seconds remaining becomes negative after expiry and is 0 without a license. Kind values are 0 none, 1 production, 2 trial and 3 pilot. Feature bits are 1 core, 2 graph models, 4 generative, 8 GPU, 16 NPU and 32 protected models.

The session object exposes the same license members.

For protected models, generate a license with your model key in the console. Attach that license before opening the pack. See licensing for model protection and renewals.