Skip to content

Commit 120d2f6

Browse files
authored
Merge pull request #86 from rust-mobile/readme-badges
README: Add badges to CI, crates.io, docs.rs and show the MSRV
2 parents 924e540 + 4a4efd8 commit 120d2f6

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

README.md

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
# Overview
1+
# `android-activity`
2+
3+
[![ci](https://github.com/rust-mobile/android-activity/actions/workflows/ci.yml/badge.svg)](https://github.com/rust-mobile/android-activity/actions/workflows/ci.yml)
4+
[![crates.io](https://img.shields.io/crates/v/android-activity.svg)](https://crates.io/crates/android-activity)
5+
[![Docs](https://docs.rs/android-activity/badge.svg)](https://docs.rs/android-activity)
6+
[![MSRV](https://img.shields.io/badge/rustc-1.64.0+-ab6000.svg)](https://blog.rust-lang.org/2022/09/22/Rust-1.64.0.html)
7+
8+
## Overview
29

310
`android-activity` provides a "glue" layer for building native Rust
411
applications on Android, supporting multiple [`Activity`] base classes.
@@ -22,10 +29,11 @@ applications.
2229
[ndk-glue]: https://crates.io/crates/ndk-glue
2330
[agdk]: https://developer.android.com/games/agdk
2431

25-
# Example
32+
## Example
2633

2734
Cargo.toml
28-
```
35+
36+
```toml
2937
[dependencies]
3038
log = "0.4"
3139
android_logger = "0.11"
@@ -38,6 +46,7 @@ crate_type = ["cdylib"]
3846
_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_
3947

4048
lib.rs
49+
4150
```rust
4251
use android_activity::{AndroidApp, InputStatus, MainEvent, PollEvent};
4352

@@ -69,22 +78,22 @@ fn android_main(app: AndroidApp) {
6978
}
7079
```
7180

72-
```
81+
```sh
7382
rustup target add aarch64-linux-android
7483
cargo install cargo-apk
7584
cargo apk run
7685
adb logcat example:V *:S
7786
```
7887

79-
# Full Examples
88+
## Full Examples
8089

8190
See [this collection of examples](https://github.com/rust-mobile/rust-android-examples) (based on both `GameActivity` and `NativeActivity`).
8291

8392
Each example is a standalone project that may also be a convenient templates for starting a new project.
8493

8594
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.
8695

87-
# Should I use NativeActivity or GameActivity?
96+
## Should I use NativeActivity or GameActivity?
8897

8998
To learn more about the `NativeActivity` class that's shipped with Android see [here](https://developer.android.com/ndk/guides/concepts#naa).
9099

@@ -96,22 +105,23 @@ It's expected that the `GameActivity` backend will gain more sophisticated input
96105

97106
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.
98107

99-
# Switching from ndk-glue to android-activity
108+
## Switching from ndk-glue to android-activity
109+
110+
### Winit-based applications
100111

101-
## Winit-based applications
102112
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).
103113

104114
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.
105115

106116
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).
107117

108-
## Middleware crates (i.e. not applications)
118+
### Middleware crates (i.e. not applications)
109119

110120
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.
111121

112122
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).
113123

114-
## Other, non-Winit-based applications
124+
### Other, non-Winit-based applications
115125

116126
The steps to switch a simple standalone application over from `ndk-glue` to `android-activity` (still based on `NativeActivity`) should be:
117127

@@ -120,7 +130,7 @@ The steps to switch a simple standalone application over from `ndk-glue` to `and
120130
3. Optionally add a dependency on `android_logger = "0.11.0"`
121131
4. Update the `main` entry point to look like this:
122132

123-
```
133+
```rust
124134
use android_activity::AndroidApp;
125135

126136
#[no_mangle]
@@ -133,21 +143,18 @@ See this minimal [NativeActivity Mainloop](https://github.com/rust-mobile/androi
133143

134144
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)
135145

136-
137-
## Design Summary / Motivation behind android-activity
146+
### Design Summary / Motivation behind android-activity
138147

139148
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:
140149

141150
1. **Support alternative Activity classes**: Prior glue crates were based on `NativeActivity` and their API precluded supporting alternatives. In particular there was an interest in the [`GameActivity`] class in conjunction with it's [`GameTextInput`] library that can facilitate onscreen keyboard support. This also allows building applications based on the standard [`AppCompatActivity`] base class which isn't possible with `NativeActivity`. Finally there was interest in paving the way towards supporting a first-party `RustActivity` that could be best tailored towards the needs of Rust applications on Android.
142151
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.
143152
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`.
144153

145-
146154
[`GameTextInput`]: https://developer.android.com/games/agdk/add-support-for-text-input
147155
[`AppCompatActivity`]: https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity
148156

149-
150-
# MSRV
157+
## MSRV
151158

152159
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.
153160
For example, when Rust 1.69 is released we would limit our `rust_version` to 1.67.

android-activity/src/input.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub enum Class {
6363

6464
impl From<u32> for Class {
6565
fn from(source: u32) -> Self {
66-
let class = SourceFlags::from_bits_truncate(source as u32);
66+
let class = SourceFlags::from_bits_truncate(source);
6767
match class {
6868
SourceFlags::BUTTON => Class::Button,
6969
SourceFlags::POINTER => Class::Pointer,

0 commit comments

Comments
 (0)