---
title: "Recipe: a GPU module that calls into Java on Android"
description: "On Android, a GPU surface whose crates call into Java needs the app's Java VM and Context before its first request."
modificationDate: "2026-11-16"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: a GPU module that calls into Java on Android

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/gpu-module-android: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

A surface whose crates call into Java (HTTP through `HttpURLConnection`, the app's `AssetManager`,
a crate that reads `ndk-context`) needs the app's Java VM and `Context` before its first request.
Android opens a GPU module (recipe `gpu-module`) with `dlopen`, which runs no `JNI_OnLoad`, so the
module asks for them with one line beside `module!`:

```prompt Call Java from my GPU module on Android
Give the GPU module of the Exact app in this folder Android's Java VM and `Context` before its first request, so that its crates can call into Java. Use https://github.com/expo/exact/blob/main/docs/recipes/gpu-module-android.md as the source for each step. The Exact checkout has the same page at docs/recipes/gpu-module-android.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me. If the app has no GPU module (no `gpu/` folder beside app.json), stop and tell me: the gpu-module recipe adds one.
2. If I did not say which crate or Java API needs the VM or the `Context` (HTTP through `HttpURLConnection`, the app's `AssetManager`, a crate that reads `ndk-context`), ask me before you write code. If Android is not one of the app's targets, tell me.
3. In the module's Rust, add one `exact_gpu::android_init!` line beside `module!`, as the recipe shows. Its closure gets `android`, which holds `vm`, `env` and `context`, and returns a `Result` whose error is `Display`. Compose several initializers with `?`.
4. Give each initializer what it needs. `env` is valid for this call only: do not keep it. `context` is valid on every attached thread for the life of the process. Use `vm` to attach a thread that the module starts. For a crate that reads `ndk-context`, call `ndk_context::initialize_android_context(android.vm, android.context)`.
5. Do not rely on `JNI_OnLoad`: Android opens the module with `dlopen`, which does not run it. Bind a Java class's `native` methods with `RegisterNatives`. Load an app's own class through `context.getClassLoader()`, because `FindClass` can see only the system class loader there.
6. Put no `cfg` on the line: on every other platform, the web included, it compiles to nothing. Return each error and leave no Java exception pending: either one refuses the module, and its canvases stay unpainted.
7. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. On Android, a log line with `Android initialization failed:` and the error means that the module was refused. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 8) and report it; do not keep trying.
8. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
9. Tell me which initializers the line runs, what the checks reported on Android and on each other target, and what stays untested if no Android device or emulator was available.
```

```rust
exact_gpu::android_init!(|android| unsafe {
    mln_storage::android::init(android.env, android.context)
});
```

`android` is an `exact_gpu::android::Android`, three `*mut c_void`:

- `vm`: the process's `JavaVM*`. Attach a thread the module starts with it.
- `env`: the calling thread's `JNIEnv*`, valid for this call only.
- `context`: the application `Context`, a global reference valid on every attached thread for the
  life of the process.

The expression returns a `Result` whose error is `Display`; compose several initializers with `?`.
A crate that reads `ndk-context` is given both by
`ndk_context::initialize_android_context(android.vm, android.context)`.

- The host calls it once in a process for each module, after checking the module's baked digest
  and before anything else in the module runs: before a device and before any surface.
- It may run on a thread the host attached to the VM, where `FindClass` sees only the system class
  loader ([JNI tips](https://developer.android.com/ndk/guides/jni-tips#faq:-why-didnt-findclass-find-my-class)):
  load an app's own class through `context.getClassLoader()`.
- It is not `JNI_OnLoad`: the VM did not load the library, so bind a Java class's `native` methods
  with `RegisterNatives`.
- An error, or a Java exception left pending, refuses the module: its canvases stay unpainted and
  the log says `GPU module …: Android initialization failed:` and the error.
- The line needs no `cfg`: on every other platform, the web included, it compiles to nothing. A
  module without it is opened as before.
