Skip to content

Commit d3188e2

Browse files
committed
IOS, Android... same thing (#7493)
# Objective - Merge the examples on iOS and Android - Make sure they both work from the same code ## Solution - don't create window when not in an active state (from #6830) - exit on suspend on Android (from #6830) - automatically enable dependency feature of bevy_audio on android so that it works out of the box - don't inverse y position of touch events - reuse the same example for both Android and iOS Fixes #4616 Fixes #4103 Fixes #3648 Fixes #3458 Fixes #3249 Fixes #86
1 parent 4fd092f commit d3188e2

File tree

22 files changed

+73
-130
lines changed

22 files changed

+73
-130
lines changed

.github/workflows/post-release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
--no-push \
4545
--exclude ci \
4646
--exclude errors \
47-
--exclude bevy-ios-example \
47+
--exclude bevy_mobile_example \
4848
--exclude build-wasm-example
4949
5050
- name: Create PR

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
--dependent-version upgrade \
4242
--exclude ci \
4343
--exclude errors \
44-
--exclude bevy-ios-example \
44+
--exclude bevy_mobile_example \
4545
--exclude build-wasm-example
4646
4747
- name: Create PR

.github/workflows/validation-jobs.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
run: rustup target add aarch64-apple-ios x86_64-apple-ios
3030

3131
- name: Build and install iOS app in iOS Simulator.
32-
run: cd examples/ios && make install
32+
run: cd examples/mobile && make install
3333

3434
build-android:
3535
runs-on: ubuntu-latest
@@ -56,7 +56,7 @@ jobs:
5656
run: cargo install --force cargo-apk
5757

5858
- name: Build APK
59-
run: ANDROID_NDK_ROOT=$ANDROID_NDK_LATEST_HOME cargo apk build --package bevy-android-example
59+
run: ANDROID_NDK_ROOT=$ANDROID_NDK_LATEST_HOME cargo apk build --package bevy_mobile_example
6060

6161
run-examples-on-windows-dx12:
6262
runs-on: windows-latest

Cargo.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ rust-version = "1.67.0"
1616
exclude = ["benches", "crates/bevy_ecs_compile_fail_tests", "crates/bevy_reflect_compile_fail_tests"]
1717
members = [
1818
"crates/*",
19-
"examples/android",
20-
"examples/ios",
19+
"examples/mobile",
2120
"tools/ci",
2221
"tools/build-example-pages",
2322
"tools/build-wasm-example",
@@ -44,6 +43,7 @@ default = [
4443
"vorbis",
4544
"x11",
4645
"filesystem_watcher",
46+
"android_shared_stdcxx"
4747
]
4848

4949
# Force dynamic linking, which improves iterative compile times
@@ -118,6 +118,9 @@ debug_asset_server = ["bevy_internal/debug_asset_server"]
118118
# Enable animation support, and glTF animation loading
119119
animation = ["bevy_internal/animation"]
120120

121+
# Enable using a shared stdlib for cxx on Android.
122+
android_shared_stdcxx = ["bevy_internal/android_shared_stdcxx"]
123+
121124
[dependencies]
122125
bevy_dylib = { path = "crates/bevy_dylib", version = "0.9.0", default-features = false, optional = true }
123126
bevy_internal = { path = "crates/bevy_internal", version = "0.9.0", default-features = false }

crates/bevy_audio/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ anyhow = "1.0.4"
2121
rodio = { version = "0.16", default-features = false }
2222
parking_lot = "0.12.1"
2323

24+
[target.'cfg(target_os = "android")'.dependencies]
25+
oboe = { version = "0.4", optional = true }
26+
2427
[target.'cfg(target_arch = "wasm32")'.dependencies]
2528
rodio = { version = "0.16", default-features = false, features = ["wasm-bindgen"] }
2629

@@ -37,3 +40,5 @@ symphonia-isomp4 = ["rodio/symphonia-isomp4"]
3740
symphonia-mp3 = ["rodio/symphonia-mp3"]
3841
symphonia-vorbis = ["rodio/symphonia-vorbis"]
3942
symphonia-wav = ["rodio/symphonia-wav"]
43+
# Enable using a shared stdlib for cxx on Android.
44+
android_shared_stdcxx = ["oboe/shared-stdcxx"]

crates/bevy_internal/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ animation = ["bevy_animation", "bevy_gltf?/bevy_animation"]
7474
# Used to disable code that is unsupported when Bevy is dynamically linked
7575
dynamic_linking = ["bevy_diagnostic/dynamic_linking"]
7676

77+
# Enable using a shared stdlib for cxx on Android.
78+
android_shared_stdcxx = ["bevy_audio/android_shared_stdcxx"]
79+
7780
[dependencies]
7881
# bevy
7982
bevy_app = { path = "../bevy_app", version = "0.9.0" }

crates/bevy_winit/src/lib.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ struct WinitPersistentState {
231231
impl Default for WinitPersistentState {
232232
fn default() -> Self {
233233
Self {
234-
active: true,
234+
active: false,
235235
low_power_event: false,
236236
redraw_request_sent: false,
237237
timeout_reached: false,
@@ -290,7 +290,7 @@ pub fn winit_runner(mut app: App) {
290290
}
291291
}
292292

293-
{
293+
if winit_state.active {
294294
#[cfg(not(target_arch = "wasm32"))]
295295
let (commands, mut new_windows, created_window_writer, winit_windows) =
296296
create_window_system_state.get_mut(&mut app.world);
@@ -475,14 +475,7 @@ pub fn winit_runner(mut app: App) {
475475
}
476476
},
477477
WindowEvent::Touch(touch) => {
478-
let mut location =
479-
touch.location.to_logical(window.resolution.scale_factor());
480-
481-
// On a mobile window, the start is from the top while on PC/Linux/OSX from
482-
// bottom
483-
if cfg!(target_os = "android") || cfg!(target_os = "ios") {
484-
location.y = window.height() as f64 - location.y;
485-
}
478+
let location = touch.location.to_logical(window.resolution.scale_factor());
486479

487480
// Event
488481
input_events
@@ -616,6 +609,13 @@ pub fn winit_runner(mut app: App) {
616609
}
617610
event::Event::Suspended => {
618611
winit_state.active = false;
612+
#[cfg(target_os = "android")]
613+
{
614+
// Bevy doesn't support suspend/resume so we just exit
615+
// and Android will restart the application on resume
616+
// TODO: Save save some state and load on resume
617+
*control_flow = ControlFlow::Exit;
618+
}
619619
}
620620
event::Event::Resumed => {
621621
winit_state.active = true;

examples/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ When using `NDK (Side by side)`, the environment variable `ANDROID_NDK_ROOT` mus
367367
To run on a device setup for Android development, run:
368368

369369
```sh
370-
cargo apk run --example android_example
370+
cargo apk run -p bevy_mobile_example
371371
```
372372

373373
When using Bevy as a library, the following fields must be added to `Cargo.toml`:
@@ -414,7 +414,7 @@ min_sdk_version = >>API or less<<
414414

415415
Example | File | Description
416416
--- | --- | ---
417-
`android` | [`android/android.rs`](./android/android.rs) | The `3d/3d_scene.rs` example for Android
417+
`android` | [`mobile/src/lib.rs`](./mobile/src/lib.rs) | A 3d Scene with a button and playing sound
418418

419419
## iOS
420420

@@ -435,7 +435,7 @@ rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-sim
435435
Using bash:
436436

437437
```sh
438-
cd examples/ios
438+
cd examples/mobile
439439
make run
440440
```
441441

@@ -450,15 +450,15 @@ DEVICE_ID=${YOUR_DEVICE_ID} make run
450450
If you'd like to see xcode do stuff, you can run
451451

452452
```sh
453-
open bevy_ios_example.xcodeproj/
453+
open bevy_mobile_example.xcodeproj/
454454
```
455455

456456
which will open xcode. You then must push the zoom zoom play button and wait
457457
for the magic.
458458

459459
Example | File | Description
460460
--- | --- | ---
461-
`ios` | [`ios/src/lib.rs`](./ios/src/lib.rs) | The `3d/3d_scene.rs` example for iOS
461+
`ios` | [`mobile/src/lib.rs`](./mobile/src/lib.rs) | A 3d Scene with a button and playing sound
462462

463463
## WASM
464464

examples/README.md.tpl

+5-5
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ When using `NDK (Side by side)`, the environment variable `ANDROID_NDK_ROOT` mus
9898
To run on a device setup for Android development, run:
9999

100100
```sh
101-
cargo apk run --example android_example
101+
cargo apk run -p bevy_mobile_example
102102
```
103103

104104
When using Bevy as a library, the following fields must be added to `Cargo.toml`:
@@ -145,7 +145,7 @@ min_sdk_version = >>API or less<<
145145

146146
Example | File | Description
147147
--- | --- | ---
148-
`android` | [`android/android.rs`](./android/android.rs) | The `3d/3d_scene.rs` example for Android
148+
`android` | [`mobile/src/lib.rs`](./mobile/src/lib.rs) | A 3d Scene with a button and playing sound
149149

150150
## iOS
151151

@@ -166,7 +166,7 @@ rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-sim
166166
Using bash:
167167

168168
```sh
169-
cd examples/ios
169+
cd examples/mobile
170170
make run
171171
```
172172

@@ -181,15 +181,15 @@ DEVICE_ID=${YOUR_DEVICE_ID} make run
181181
If you'd like to see xcode do stuff, you can run
182182

183183
```sh
184-
open bevy_ios_example.xcodeproj/
184+
open bevy_mobile_example.xcodeproj/
185185
```
186186

187187
which will open xcode. You then must push the zoom zoom play button and wait
188188
for the magic.
189189

190190
Example | File | Description
191191
--- | --- | ---
192-
`ios` | [`ios/src/lib.rs`](./ios/src/lib.rs) | The `3d/3d_scene.rs` example for iOS
192+
`ios` | [`mobile/src/lib.rs`](./mobile/src/lib.rs) | A 3d Scene with a button and playing sound
193193

194194
## WASM
195195

examples/android/src/lib.rs

-54
This file was deleted.

examples/ios/Cargo.toml

-14
This file was deleted.
File renamed without changes.

examples/android/Cargo.toml examples/mobile/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
[package]
2-
name = "bevy-android-example"
2+
name = "bevy_mobile_example"
33
version = "0.1.0"
44
edition = "2021"
5-
description = "Example for building an Android app with Bevy"
5+
description = "Example for building an iOS or Android app with Bevy"
66
publish = false
77
license = "MIT OR Apache-2.0"
88

99
[lib]
10-
name = "bevy_android_example"
11-
crate-type = ["cdylib"]
10+
name = "bevy_mobile_example"
11+
crate-type = ["staticlib", "cdylib"]
1212

1313
[dependencies]
1414
bevy = { path = "../../" }

examples/ios/Makefile examples/mobile/Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ ifndef DEVICE_ID
66
endif
77

88
run: install
9-
xcrun simctl launch --console $(DEVICE) com.rust.bevy-ios-example
9+
xcrun simctl launch --console $(DEVICE) com.rust.bevy_mobile_example
1010

1111
boot-sim:
1212
xcrun simctl boot $(DEVICE) || true
1313

1414
install: xcodebuild-simulator boot-sim
15-
xcrun simctl install $(DEVICE) build/Build/Products/Debug-iphonesimulator/bevy_ios_example.app
15+
xcrun simctl install $(DEVICE) build/Build/Products/Debug-iphonesimulator/bevy_mobile_example.app
1616

1717
xcodebuild-simulator:
18-
IOS_TARGETS=x86_64-apple-ios xcodebuild -scheme bevy_ios_example -configuration Debug -derivedDataPath build -destination "id=$(DEVICE)"
18+
IOS_TARGETS=x86_64-apple-ios xcodebuild -scheme bevy_mobile_example -configuration Debug -derivedDataPath build -destination "id=$(DEVICE)"
1919

2020
xcodebuild-iphone:
21-
IOS_TARGETS=aarch64-apple-ios xcodebuild -scheme bevy_ios_example -configuration Debug -derivedDataPath build -arch arm64
21+
IOS_TARGETS=aarch64-apple-ios xcodebuild -scheme bevy_mobile_example -configuration Debug -derivedDataPath build -arch arm64
2222

2323
clean:
2424
rm -r build

0 commit comments

Comments
 (0)