mirror of
https://github.com/rust-mobile/android-activity.git
synced 2026-07-04 05:47:26 +00:00
Merge pull request #86 from rust-mobile/readme-badges
README: Add badges to CI, crates.io, docs.rs and show the MSRV
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
# Overview
|
||||
# `android-activity`
|
||||
|
||||
[](https://github.com/rust-mobile/android-activity/actions/workflows/ci.yml)
|
||||
[](https://crates.io/crates/android-activity)
|
||||
[](https://docs.rs/android-activity)
|
||||
[](https://blog.rust-lang.org/2022/09/22/Rust-1.64.0.html)
|
||||
|
||||
## Overview
|
||||
|
||||
`android-activity` provides a "glue" layer for building native Rust
|
||||
applications on Android, supporting multiple [`Activity`] base classes.
|
||||
@@ -22,10 +29,11 @@ applications.
|
||||
[ndk-glue]: https://crates.io/crates/ndk-glue
|
||||
[agdk]: https://developer.android.com/games/agdk
|
||||
|
||||
# Example
|
||||
## Example
|
||||
|
||||
Cargo.toml
|
||||
```
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
log = "0.4"
|
||||
android_logger = "0.11"
|
||||
@@ -38,6 +46,7 @@ crate_type = ["cdylib"]
|
||||
_Note: that you will need to either specify the **"native-activity"** feature or **"game-activity"** feature to identify which `Activity` base class your application is based on_
|
||||
|
||||
lib.rs
|
||||
|
||||
```rust
|
||||
use android_activity::{AndroidApp, InputStatus, MainEvent, PollEvent};
|
||||
|
||||
@@ -69,14 +78,14 @@ fn android_main(app: AndroidApp) {
|
||||
}
|
||||
```
|
||||
|
||||
```
|
||||
```sh
|
||||
rustup target add aarch64-linux-android
|
||||
cargo install cargo-apk
|
||||
cargo apk run
|
||||
adb logcat example:V *:S
|
||||
```
|
||||
|
||||
# Full Examples
|
||||
## Full Examples
|
||||
|
||||
See [this collection of examples](https://github.com/rust-mobile/rust-android-examples) (based on both `GameActivity` and `NativeActivity`).
|
||||
|
||||
@@ -84,7 +93,7 @@ Each example is a standalone project that may also be a convenient templates for
|
||||
|
||||
For the examples based on middleware frameworks (Winit and or Egui) they also aim to demonstrate how it's possible to write portable code that will run on Android and other systems.
|
||||
|
||||
# Should I use NativeActivity or GameActivity?
|
||||
## Should I use NativeActivity or GameActivity?
|
||||
|
||||
To learn more about the `NativeActivity` class that's shipped with Android see [here](https://developer.android.com/ndk/guides/concepts#naa).
|
||||
|
||||
@@ -96,22 +105,23 @@ It's expected that the `GameActivity` backend will gain more sophisticated input
|
||||
|
||||
Even if you start out using `NativeActivity` for the convenience, it's likely that most moderately complex applications will eventually need to define their own `Activity` subclass (either subclassing `NativeActivity` or `GameActivity`) which will require compiling at least a small amount of Java or Kotlin code. This is generally due to Android's design which directs numerous events via the `Activity` class which can only be processed by overloading some associated Activity method.
|
||||
|
||||
# Switching from ndk-glue to android-activity
|
||||
## Switching from ndk-glue to android-activity
|
||||
|
||||
### Winit-based applications
|
||||
|
||||
## Winit-based applications
|
||||
Firstly; if you have a [Winit](https://crates.io/crates/winit) based application and also have an explicit dependency on `ndk-glue` your application will need to remove its dependency on `ndk-glue` for the 0.28 release of Winit which will be based on android-activity (Since glue crates, due to their nature, can't be compatible with alternative glue crates).
|
||||
|
||||
Winit-based applications can follow the [Android README](https://github.com/rust-windowing/winit#android) guidance for advice on how to switch over. Most Winit-based applications should aim to remove any explicit dependency on a specific glue crate (so not depend directly on `ndk-glue` or `android-activity` and instead rely on Winit to pull in the right glue crate). The main practical change will then be to add a `#[no_mangle]fn android_main(app: AndroidApp)` entry point.
|
||||
|
||||
See the [Android README](https://github.com/rust-windowing/winit#android) for more details and also see the [Winit-based examples here](https://github.com/rust-mobile/rust-android-examples).
|
||||
|
||||
## Middleware crates (i.e. not applications)
|
||||
### Middleware crates (i.e. not applications)
|
||||
|
||||
If you have a crate that would be considered a middleware library (for example using JNI to support access to Bluetooth, or Android's Camera APIs) then the crate should almost certainly remove any dependence on a specific glue crate because this imposes a strict compatibility constraint that means the crate can only be used by applications that use that exact same glue crate version.
|
||||
|
||||
Middleware libraries can instead look at using the [ndk-context](https://crates.io/crates/ndk-context) crate as a means for being able to use JNI without making any assumptions about the applications overall architecture. This way a middleware crate can work with alternative glue crates (including `ndk-glue` and `android-activity`) as well as work with embedded use cases (i.e. non-native applications that may just embed a dynamic library written in Rust to implement certain native functions).
|
||||
|
||||
## Other, non-Winit-based applications
|
||||
### Other, non-Winit-based applications
|
||||
|
||||
The steps to switch a simple standalone application over from `ndk-glue` to `android-activity` (still based on `NativeActivity`) should be:
|
||||
|
||||
@@ -120,7 +130,7 @@ The steps to switch a simple standalone application over from `ndk-glue` to `and
|
||||
3. Optionally add a dependency on `android_logger = "0.11.0"`
|
||||
4. Update the `main` entry point to look like this:
|
||||
|
||||
```
|
||||
```rust
|
||||
use android_activity::AndroidApp;
|
||||
|
||||
#[no_mangle]
|
||||
@@ -133,8 +143,7 @@ See this minimal [NativeActivity Mainloop](https://github.com/rust-mobile/androi
|
||||
|
||||
There is is no `#[ndk_glue::main]` replacement considering that `android_main()` entry point needs to be passed an `AndroidApp` argument which isn't compatible with a traditional `main()` function. Having an Android specific entry point also gives a place to initialize Android logging and handle other Android specific details (such as building an event loop based on the `app` argument)
|
||||
|
||||
|
||||
## Design Summary / Motivation behind android-activity
|
||||
### Design Summary / Motivation behind android-activity
|
||||
|
||||
Prior to working on android-activity, the existing glue crates available for building standalone Rust applications on Android were found to have a number of technical limitations that this crate aimed to solve:
|
||||
|
||||
@@ -142,12 +151,10 @@ Prior to working on android-activity, the existing glue crates available for bui
|
||||
2. **Encapsulate IPC + synchronization between the native thread and the JVM thread**: For example with `ndk-glue` the application itself needs to avoid race conditions between the native and Java thread by following a locking convention) and it wasn't clear how this would extend to support other requests (like state saving) that also require synchronization.
|
||||
3. **Avoid static global state**: Keeping in mind the possibility of supporting applications with multiple native activities there was interest in having an API that didn't rely on global statics to track top-level state. Instead of having global getters for state then `android-activity` passes an explicit `app: AndroidApp` argument to the entry point that encapsulates the state connected with a single `Activity`.
|
||||
|
||||
|
||||
[`GameTextInput`]: https://developer.android.com/games/agdk/add-support-for-text-input
|
||||
[`AppCompatActivity`]: https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity
|
||||
|
||||
|
||||
# MSRV
|
||||
## MSRV
|
||||
|
||||
We aim to (at least) support stable releases of Rust from the last three months. Rust has a 6 week release cycle which means we will support the last three stable releases.
|
||||
For example, when Rust 1.69 is released we would limit our `rust_version` to 1.67.
|
||||
|
||||
@@ -63,7 +63,7 @@ pub enum Class {
|
||||
|
||||
impl From<u32> for Class {
|
||||
fn from(source: u32) -> Self {
|
||||
let class = SourceFlags::from_bits_truncate(source as u32);
|
||||
let class = SourceFlags::from_bits_truncate(source);
|
||||
match class {
|
||||
SourceFlags::BUTTON => Class::Button,
|
||||
SourceFlags::POINTER => Class::Pointer,
|
||||
|
||||
Reference in New Issue
Block a user