Skip to content

android: Correct the JNI helper docs - #40

Open
Bombatomica64 wants to merge 1 commit into
longbridge:mainfrom
Bombatomica64:upstream/jni-docs
Open

Bombatomica64 wants to merge 1 commit into
longbridge:mainfrom
Bombatomica64:upstream/jni-docs

Conversation

@Bombatomica64

Copy link
Copy Markdown

Part of #31 (PR 1 of the proposed list). Comments and one deprecation only; no behaviour change.

Problem

  • The with_env doc says the thread is detached again when the closure returns. In jni 0.22, JavaVM::attach_current_thread attaches the thread for good (until it exits) and runs the closure in its own local frame. It also clears a Java exception still pending at the end and returns it as the error, in place of the closure's own result. That last part matters to callers, and the doc did not mention it.
  • obtain_env is an alias of with_env with no callers in the crate.
  • Several unsafe blocks in the JNI helpers and exports had no SAFETY comment, and one comment in nativeOnDeepLink talked about not dropping a JObject, which has no Drop in jni 0.22.

Change

  • with_env: the doc now describes what jni 0.22 does: the thread stays attached, the closure gets its own local frame, and an exception still pending at the end becomes the returned error.
  • obtain_env is marked #[deprecated(note = "use `with_env`")]. It is still public, so nothing breaks.
  • SAFETY comments on the unsafe blocks in java_vm_safe, find_app_class, nativeIme, nativeOnDeepLink, nativeMediaAction and document_picker::pick_files. The stale JObject comment is removed.

activity() and get_string() don't get a SAFETY comment here, because what they rely on is not guaranteed yet. #31 fixes both in later PRs: get_string checks the type in PR 2, and activity() keeps its reference alive under the lock in PR 3.

Testing

Run on this branch with the Android NDK 27, target aarch64-linux-android:

  • cargo fmt --all -- --check
  • cargo check --target aarch64-linux-android --all-targets, plus cargo check of example/
  • cargo clippy --target aarch64-linux-android: the same 8 warnings as on main, none new
  • cargo test --lib: 46 passed

🤖 Generated with Claude Code

with_env claimed the thread is detached when the closure returns. jni
0.22's attach_current_thread attaches for good, runs the closure in its
own local frame, and turns an exception still pending at the end into the
returned error, replacing the closure's result. The doc now says so.

obtain_env, an alias of with_env with no callers, is deprecated. The
unsafe blocks in the JNI helpers, the JNI exports and the document picker
gain SAFETY comments, and a stale comment about dropping a JObject (it has
no Drop in jni 0.22) goes.

Part of longbridge#31.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant