bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Add features to switch `NativeActivity` and `GameActivity` usage

Open Litttlefish opened this issue 1 year ago • 79 comments

Objective

Add two features to switch bevy to use NativeActivity or GameActivity on Android, use GameActivity by default.

Also close #12058 and probably #12026 .

Solution

Add two features to the corresponding crates so you can toggle it, like what winit and android-activity crate did.


Changelog

Removed default NativeActivity feature implementation for Android, added two new features to enable NativeActivity and GameActivity, and use GameActivity by default.

Migration Guide

Because cargo-apk is not compatible with GameActivity, building/running using cargo apk build/run -p bevy_mobile_example is no longer possible. Users should follow the new workflow described in document.

Litttlefish avatar Feb 24 '24 16:02 Litttlefish

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

github-actions[bot] avatar Feb 24 '24 16:02 github-actions[bot]

You added a new feature but didn't add a description for it. Please update the root Cargo.toml file.

github-actions[bot] avatar Feb 24 '24 16:02 github-actions[bot]

You added a new feature but didn't update the readme. Please run cargo run -p build-templated-pages -- update features to update it, and commit the file change.

github-actions[bot] avatar Feb 24 '24 16:02 github-actions[bot]

Addendum: In my tests, bevy with android-game-activity feature enabled runs better than native without any code changes, and I didn't encounter bugs.

Litttlefish avatar Feb 25 '24 02:02 Litttlefish

bevy with android-game-activity feature enabled runs better than native

ca you be more specific about what better means?

extrawurst avatar Feb 25 '24 14:02 extrawurst

bevy with android-game-activity feature enabled runs better than native

ca you be more specific about what better means?

It can let rust code(originally C++ though) control more stuff on app level without much jni coding For example, in my tests when sending AppExit event, in Native version it will quit, but it will also cause a crash/segfault(I don't know but maybe can handle it with jni crate) but in Game version, it will simply quit the game without crashing. It also has better input handling, etc. can see it here

Litttlefish avatar Feb 25 '24 14:02 Litttlefish

I think android-game-activity should be part of the default features for the Bevy crate.

Example https://github.com/bevyengine/bevy/blob/main/examples/mobile/Cargo.toml should still work, so it must have one selected if it's not part of the default features

mockersf avatar Feb 25 '24 21:02 mockersf

I think android-game-activity should be part of the default features for the Bevy crate.

Ugh I just reckoned what this line really means, so forget what I have said. RIP for my comprehension.

Example https://github.com/bevyengine/bevy/blob/main/examples/mobile/Cargo.toml should still work, so it must have one selected if it's not part of the default features

Build these(game, probably with native) examples from android-activity examples might be a good choice since cargo apk is deprecated, as #12026 mentioned.

So if we can add these examples then we can close it too.

Litttlefish avatar Feb 26 '24 02:02 Litttlefish

You added a new example but didn't add metadata for it. Please update the root Cargo.toml file.

github-actions[bot] avatar Feb 27 '24 15:02 github-actions[bot]

Ok I added android-game-activity as a default feature now🤔 But I'm not sure if cargo apk still works with GameActivity in examples. Still needs some tweaking...

Litttlefish avatar Feb 27 '24 15:02 Litttlefish

You added a new feature but didn't update the readme. Please run cargo run -p build-templated-pages -- update features to update it, and commit the file change.

github-actions[bot] avatar Feb 27 '24 15:02 github-actions[bot]

After my tests the original mobile example(used cargo-apk) no longer works with GameActivity, because GameActivity needs the app theme to be the child of Theme.AppCompat but cargo-apk can't recognize it.

So we do need a new example(or probably split them to something like mobile-ios, mobine-android-native, mobile-android-game)

Litttlefish avatar Feb 28 '24 05:02 Litttlefish

it would be nice to be able to keep a common example for iOS and Android, does the code for game activity differs that much?

mockersf avatar Feb 28 '24 17:02 mockersf

it would be nice to be able to keep a common example for iOS and Android, does the code for game activity differs that much?

nope, GameActivity won't change rust code, the reason is GameActivity is the child of AppCompat so the theme must also use the child of it or else it'll crash while cargo-apk can't recognize it.

Litttlefish avatar Feb 29 '24 01:02 Litttlefish

so if we have to give up cargo-apk, then how to make the new mobile example?

for now, my idea is simply replace the android part with a Android Studio project and use cargo-ndk to generate so files, then build apk with gradle or in the IDE like this

the good side is it can accept both native and game since native doesn't care the app theme.

Litttlefish avatar Mar 01 '24 16:03 Litttlefish

I tried making a minimal android project to use agdk in examples, but this (probably) requires moving assets and resources into gradle, and I have no idea how to do it🤔

Litttlefish avatar Mar 07 '24 04:03 Litttlefish

Sorry for the long wait, I had stuck in school before.

I'm still trying to shove a minimum android project into the mobile example, and I've made some progress.

For some reason, the apk can compile, but if you run it, you'll only hear music playing with pitch black on screen(and sometimes pops an ANR window).

If we can solve this...

Litttlefish avatar Apr 26 '24 17:04 Litttlefish

I figured out why ANR happens the example must build with --release when using cargo ndk strange, but it does run normally now

Litttlefish avatar Apr 27 '24 03:04 Litttlefish

Ok I added working minimal(hope so) android studio project file, and a build.rs to copy libc++_shared.so, but doing so might cause document check fails(also probably need a new readme to indicate what have changed and how to compile android)

Litttlefish avatar May 12 '24 07:05 Litttlefish

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

github-actions[bot] avatar May 12 '24 07:05 github-actions[bot]

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

github-actions[bot] avatar May 12 '24 07:05 github-actions[bot]

hm, I tried cargo run -p build-templated-pages -- update examples but it didn't change anything, so it's stuck for now?

Litttlefish avatar May 12 '24 12:05 Litttlefish

I'll draft this due to this problem, and I'll change the example readme accordingly

Litttlefish avatar May 12 '24 12:05 Litttlefish

Try running cargo fmt --all. It's good to clean up regardless, and there's a warning about incorrect line endings, so fixing that might help fix the check-missing-examples-in-docs workflow too.

alice-i-cecile avatar May 12 '24 13:05 alice-i-cecile

@BD103, are you able to puzzle out why check-missing-examples-in-docs is failing?

alice-i-cecile avatar May 12 '24 13:05 alice-i-cecile

I think it's an issue with newline characters on Windows

mockersf avatar May 12 '24 13:05 mockersf

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

github-actions[bot] avatar May 12 '24 13:05 github-actions[bot]

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

github-actions[bot] avatar May 12 '24 13:05 github-actions[bot]

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

github-actions[bot] avatar May 12 '24 14:05 github-actions[bot]

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

github-actions[bot] avatar May 12 '24 14:05 github-actions[bot]