Skip to content

android-activity should initialize ndk-context with an android.app.Application as the Context #228

@rib

Description

@rib

In order for Rust library crates like app_dirs2 or webbrower to be able to support Android without being tied to any specific Rust Android framework they need some minimal application state that lets them use JNI to interact with the Android Java SDK.

The current solution for this is the ndk-context crate that has a frozen "1.0" API that Rust library crates wanting to use JNI can rely on to get access to a JavaVM pointer and an android.content.Context reference.

It's then up to crates like android-activity or other Rust Android frameworks to initialize ndk-context on behalf of these library crates.

Currently android-activity satisfies this by providing a reference to the current Activity as the Context reference.

A big problem with this choice is that it implies that we need to re-initialize this state if the application ever tries to start more than one Activity, which can lead to a panic: #58

..but the ndk-context documentation specifically does not guarantee that the context reference will be for an Activity:

A handle to an android.content.Context. In most cases this will be a ptr to an Activity, but this isn’t guaranteed.

and in practice, if we look at the requirements of app_dirs2 and webbrowser we can see that neither of these actually require an Activity - they really do only need a Context reference.

(skimming the dependants of ndk-context, I would guess that app_dirs2 and webbrowser are probably the two main library use cases that we really don't want to break)

In order to avoid ever needing to re-initialize ndk-context I think we should update android-activity so that it does a one-time initialization of ndk-context with an android.app.Application reference instead of an Activity reference.

Applications based on android-activity that really need a reference to the Activity can instead get this via AndroidApp::activity_as_ptr() and other frameworks for Rust Android applications probably have their own more suitable way of giving access to any Activity they have.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions