Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add features to switch NativeActivity and GameActivity usage #12095

Open
wants to merge 136 commits into
base: main
Choose a base branch
from

Conversation

Litttlefish
Copy link

@Litttlefish Litttlefish commented Feb 24, 2024

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.

Copy link
Contributor

Welcome, new contributor!

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

Copy link
Contributor

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

Copy link
Contributor

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.

@alice-i-cecile alice-i-cecile added O-Android Specific to the Android mobile operating system D-Complex Quite challenging from either a design or technical perspective. Ask for help! labels Feb 24, 2024
@Litttlefish
Copy link
Author

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.

@extrawurst
Copy link
Contributor

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

ca you be more specific about what better means?

@Litttlefish
Copy link
Author

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

@mockersf
Copy link
Member

mockersf commented Feb 25, 2024

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 mockersf added the C-Breaking-Change A breaking change to Bevy's public API that needs to be noted in a migration guide label Feb 25, 2024
@alice-i-cecile alice-i-cecile added the C-Needs-Release-Note Work that should be called out in the blog due to impact label Feb 25, 2024
@Litttlefish
Copy link
Author

Litttlefish commented Feb 26, 2024

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.

Copy link
Contributor

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

@Litttlefish
Copy link
Author

Litttlefish commented Feb 27, 2024

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

Copy link
Contributor

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.

@Litttlefish
Copy link
Author

Litttlefish commented Feb 28, 2024

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)

@mockersf
Copy link
Member

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?

@Litttlefish
Copy link
Author

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
Copy link
Author

Litttlefish commented Mar 1, 2024

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
Copy link
Author

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
Copy link
Author

Litttlefish commented Apr 26, 2024

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

@JMS55 JMS55 added this to the 0.15 milestone May 25, 2024
@Litttlefish
Copy link
Author

Although this is set to 0.15 but I wanna ask is it possible to merge this into 0.14?🤔

@alice-i-cecile
Copy link
Member

Definitely possible, but we need qualified reviewers. I don't have enough experience on Android to validate this myself.

@Litttlefish
Copy link
Author

Litttlefish commented May 25, 2024

Definitely possible, but we need qualified reviewers. I don't have enough experience on Android to validate this myself.

I did do some research on it, but I don’t think I qualify, nor does reviewing my own stuff make sense🤔

If we can't find others then the only way is waiting @mockersf has time to check this (I think)

@beicause
Copy link

Hello, I try this pr but get compilation error if enable android-game-activity:

error: The "game-activity" and "native-activity" features cannot be enabled at the same time
   --> /home/luo/.cargo/registry/src/index.crates.io-6f17d22bba15001f/android-activity-0.5.2/src/lib.rs:133:1
    |
133 | / compile_error!(
134 | |     r#"The "game-activity" and "native-activity" features cannot be enabled at the same time"#
135 | | );
    | |_^

Also, it's better to add these to .gitignore in the example:

.gradle
.idea
.DS_Store
build
.cxx
local.properties

@Litttlefish
Copy link
Author

Litttlefish commented May 26, 2024

Hello, I try this pr but get compilation error if enable android-game-activity:

error: The "game-activity" and "native-activity" features cannot be enabled at the same time
   --> /home/luo/.cargo/registry/src/index.crates.io-6f17d22bba15001f/android-activity-0.5.2/src/lib.rs:133:1
    |
133 | / compile_error!(
134 | |     r#"The "game-activity" and "native-activity" features cannot be enabled at the same time"#
135 | | );
    | |_^

Also, it's better to add these to .gitignore in the example:

.gradle
.idea
.DS_Store
build
.cxx
local.properties

What is your cargo.toml setting? or you simply tried example and got this error
Usually this error only happens if you set android-game-activity and android-native-activity on at the same time

@beicause
Copy link

@Litttlefish My mistake. It conflicts with my other dependencies.

@Litttlefish
Copy link
Author

why there will have some checks cancelled?

@Litttlefish
Copy link
Author

Litttlefish commented Jun 5, 2024

so winit is 0.30 now, does this make android runnable?🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-App Bevy apps and plugins C-Breaking-Change A breaking change to Bevy's public API that needs to be noted in a migration guide C-Docs An addition or correction to our documentation C-Enhancement A new feature C-Examples An addition or correction to our examples C-Needs-Release-Note Work that should be called out in the blog due to impact D-Complex Quite challenging from either a design or technical perspective. Ask for help! O-Android Specific to the Android mobile operating system S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a feature to switch NativeActivity and GameActivity for winit in bevy_winit
7 participants