Skip to content

Device Services & Permissions

This content is for v1.2.0. Switch to the latest version for up-to-date documentation.

ZKite 1.2.0 exposes native Android device operations as ordinary Python modules under the zkite namespace. You can capture photos, record audio, query motion sensors, read/write the clipboard, vibrate the device, access encrypted Keystore secrets, and present biometric prompts directly from declarative Python—without writing any Java, Kotlin, Gradle, Android SDK, or JNI code.


The device bridge is implemented natively within the pre-compiled Android engine:

  • Pure Python API: Import device services directly from zkite:
    from zkite import (
    audio,
    biometrics,
    camera,
    clipboard,
    device,
    files,
    haptics,
    location,
    network,
    notifications,
    secrets,
    sensors,
    share,
    )
  • Desktop-Safe Imports: Importing device modules on your development computer (macOS, Linux, or Windows) is completely safe. This ensures linters, type checkers, unit tests, and IDE tooling function smoothly. Calling an Android-only operation on desktop raises a clean zkite.errors.DeviceUnavailable exception.
  • Native Backend Only: Device integration is supported on the primary native Android backend. The experimental WebView backend does not implement the device bridge.

Starting in ZKite 1.2.0, build and development commands (zkite run, zkite build apk, zkite build aab, and preview preparation) statically analyze your project’s Python source without executing it, automatically synchronizing required Android permissions into app.json before packaging.

Suppose your application imports audio and haptic feedback:

from zkite import audio, haptics
async def start_recording():
recording = await audio.record()
await haptics.vibrate(duration_ms=80)
return recording

During build or run preparation, ZKite’s static scanner identifies the usage of audio.record() and haptics.vibrate() and updates app.json:

app.json
{
"android": {
"permissions": ["RECORD_AUDIO", "VIBRATE"],
"inferred_permissions": ["RECORD_AUDIO", "VIBRATE"]
}
}
  • permissions: Contains all manifest permission declarations (both manually declared and automatically inferred).
  • inferred_permissions: Records permissions managed by the automatic scanner.
  • Automatic Pruning: When you remove the last detected code reference to a device API, subsequent builds automatically prune that permission from both permissions and inferred_permissions.
  • Retaining a Permission Manually: If you want to keep a permission permanently even if the scanner does not detect direct usage, keep it in permissions and remove it from inferred_permissions.
  • Version Control: Commit app.json to Git so that changes to inferred and manual permissions are clearly reviewable in pull requests.
  • Safe Rewrites: A failed parse leaves app.json completely untouched. If app.json is being edited concurrently, an explicit error is raised rather than overwriting changes. Scans with no changes do not touch the file.

Each scan prints the detection reasons to the terminal and records an auditable report to .zkite/permissions.json. The scanner detects:

  • Standard imports and module aliases (import zkite.audio as snd)
  • Local re-exports and function references
  • Literal getattr() lookups and nested imports
  • Remote Image URL sources (infers INTERNET)
  • Common Python HTTP clients (such as requests, urllib, httpx, and aiohttp)

The static scanner is intentionally conservative: it inspects code branches and functions without proving runtime reachability. However, it cannot deduce dynamic runtime behavior, such as reflection, eval(), dynamic imports, or third-party libraries calling platform APIs under the hood.

For these cases, declare explicit capabilities or permissions in app.json:

app.json
{
"android": {
"capabilities": ["audio.record", "location", "notifications"],
"permissions": ["INTERNET"]
}
}
Capability Name Triggered Permissions / Hardware
files System file & photo picking (no broad storage required)
camera.capture External camera intent delegation
camera.torch Flashlight / camera torch toggle
audio.record RECORD_AUDIO
audio.play Android MediaPlayer integration
haptics VIBRATE (for custom patterns)
sensors Accelerometer, gyroscope, orientation, light, proximity
share Android system share chooser
clipboard System clipboard read/write
device System device info, battery, keep-awake
network INTERNET
location ACCESS_COARSE_LOCATION / ACCESS_FINE_LOCATION
notifications POST_NOTIFICATIONS (Android 13+)
secrets App-owned Android Keystore AES-GCM encrypted storage
biometrics Android BiometricPrompt authentication

Declaring a permission in the manifest is only the first step:

  • Normal Permissions (such as INTERNET): Automatically granted at installation time without runtime prompts.
  • Dangerous Permissions (such as RECORD_AUDIO, location, camera, or POST_NOTIFICATIONS): Require explicit runtime consent from the user.
  • Contextual Prompts: ZKite device services automatically trigger Android’s standard runtime permission dialog right when the API is invoked.
  • Graceful Handling: If the user denies a permission request, ZKite raises a typed PermissionDenied exception instead of crashing your app. You can catch this exception and guide the user in your UI.
  • Granular Scoping: Photo/file pickers, system camera capture, and semantic haptics do not require broad storage or system permissions; ZKite uses privacy-preserving Android system pickers and intents.

Compiled Manifest Verification in APK & AAB

Section titled “Compiled Manifest Verification in APK & AAB”

To ensure that distribution packages adhere strictly to Android platform and Google Play policies, ZKite performs post-signing manifest verification:

  • Built-in Rust Parser: ZKite parses the compiled binary XML manifest directly from the signed package—inspecting AndroidManifest.xml in APKs and base/manifest/AndroidManifest.xml in AABs. No external Java SDK or aapt tools are required.
  • Structure Validation: Verifies that <uses-permission> tags are direct children of <manifest>, checks resource encoding and namespaces, and rejects duplicate permission declarations.
  • Config Matching: Compares compiled permissions against the resolved permissions in app.json.
  • Hardware Tagging: To prevent Google Play from unintentionally filtering out devices that lack specific sensors or hardware, ZKite marks camera, autofocus, microphone, and location hardware features as optional (android:required="false") in the manifest.