Skip to content

Commit b424cfa

Browse files
committed
examples: Use single updated android example
Objective - Only use a single modern and better documented android example - Fix #10945 Problems with current examples - We are currently using deprecated functions like for example [setSystemUiVisibility(int)](https://developer.android.com/reference/android/view/View#setSystemUiVisibility(int)). - Most Android dependencies are very out of date. - Kotlin is the [preffered language](https://developer.android.com/kotlin/first) for Android and we should migrate the examples to it. - Most of the code in the examples is not inherently clear and there are very few comments especially in the build scripts. - We should only have a single Android example using `games-activity` and `cargo-ndk`. It is confusing and unnecessary to do it any other way in my opinion. - The crash from #10945 is fixed by migrating to `games-activity` 4, we are using `games-activity` 2. #17122 is apparently also fixed. Solution - Remove all android examples and add `./examples/mobile/android`. - Update dependencies in libs.versions.toml - Add gradle-daemon-jvm.properties from ./gradlew updateDaemonJvm - Update gradle wrapper - Migrate everything to kotlin/kotlin build scripts - Use supported functions instead of deprecated ones - Move `/assets/ic_launcher.png` to `/examples/mobile/android/app/src/main/res/mipmap-mdpi/ic_launcher.png` because that is where it is [meant to be stored](https://developer.android.com/guide/topics/resources/providing-resources).
1 parent ae7af25 commit b424cfa

37 files changed

Lines changed: 556 additions & 1045 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+
10+
# Binary files should be left untouched
11+
*.jar binary
12+
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# This is part of a hack to convince gradle to insert libc++_shared.so
2-
cmake_minimum_required(VERSION 3.4.1)
3-
project(cppshared_dummy)
4-
set (SRC_DIR ./src/main/cpp)
5-
add_library (dummy SHARED ${SRC_DIR}/dummy.cpp)
1+
# This is part of a hack to convince gradle to insert libc++_shared.so
2+
cmake_minimum_required(VERSION 3.4.1)
3+
project(cppshared_dummy)
4+
set(SRC_DIR ./src/main/cpp)
5+
add_library(dummy SHARED ${SRC_DIR}/dummy.cpp)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
plugins {
2+
alias(libs.plugins.android.application)
3+
}
4+
5+
kotlin {
6+
compilerOptions {
7+
languageVersion = org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_3
8+
jvmToolchain(8)
9+
}
10+
}
11+
12+
android {
13+
namespace = "org.bevyengine.example"
14+
compileSdk = 36
15+
16+
// https://developer.android.com/reference/tools/gradle-api/8.13/com/android/build/api/dsl/DefaultConfig
17+
defaultConfig {
18+
applicationId = "org.bevyengine.example"
19+
minSdk = 31
20+
targetSdk = 36
21+
// NOTE: Increase by 1 on each release
22+
versionCode = 1
23+
// NOTE: Update with full semantic version on each release
24+
versionName = "0.0.0"
25+
// https://developer.android.com/reference/tools/gradle-api/8.13/com/android/build/api/variant/ExternalNativeBuild
26+
// NOTE: We need this, otherwise libc++_shared.so will not be inserted
27+
@Suppress("UnstableApiUsage")
28+
externalNativeBuild {
29+
cmake {
30+
arguments("-DANDROID_STL=c++_shared")
31+
}
32+
}
33+
// https://developer.android.com/reference/tools/gradle-api/8.13/com/android/build/api/dsl/Ndk
34+
ndk {
35+
abiFilters.addAll(listOf("arm64-v8a", "armeabi-v7a", "x86_64"))
36+
}
37+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
38+
}
39+
// https://developer.android.com/reference/tools/gradle-api/8.13/com/android/build/api/dsl/ExternalNativeBuild
40+
externalNativeBuild {
41+
cmake {
42+
path = file("CMakeLists.txt")
43+
}
44+
}
45+
// https://developer.android.com/reference/tools/gradle-api/8.13/com/android/build/api/dsl/BuildType
46+
buildTypes {
47+
getByName("release") {
48+
// https://developer.android.com/topic/performance/app-optimization/enable-app-optimization
49+
isMinifyEnabled = true
50+
isShrinkResources = true
51+
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"))
52+
}
53+
}
54+
// https://developer.android.com/reference/tools/gradle-api/8.13/com/android/build/api/dsl/CompileOptions
55+
compileOptions {
56+
sourceCompatibility = JavaVersion.VERSION_1_8
57+
targetCompatibility = JavaVersion.VERSION_1_8
58+
}
59+
// https://developer.android.com/reference/tools/gradle-api/8.13/com/android/build/api/dsl/BuildFeatures
60+
buildFeatures {
61+
prefab = true
62+
}
63+
packaging {
64+
// https://developer.android.com/reference/tools/gradle-api/8.13/com/android/build/api/dsl/JniLibsPackaging
65+
jniLibs.excludes.add("lib/*/libdummy.so")
66+
jniLibs.pickFirsts.add("lib/*/libc++_shared.so")
67+
}
68+
// https://developer.android.com/reference/tools/gradle-api/8.13/com/android/build/api/dsl/AndroidSourceSet
69+
sourceSets {
70+
getByName("main") {
71+
assets {
72+
directories += "../../../../assets"
73+
}
74+
}
75+
}
76+
}
77+
78+
dependencies {
79+
implementation(libs.appcompat)
80+
implementation(libs.core)
81+
implementation(libs.material)
82+
implementation(libs.games.activity)
83+
implementation(libs.core.ktx)
84+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application android:icon="@mipmap/ic_launcher" android:label="Bevy Example" android:roundIcon="@mipmap/ic_launcher" android:theme="@style/Theme.AppCompat.NoActionBar" tools:targetApi="36">
6+
<activity android:name=".MainActivity" android:exported="true" android:configChanges="layoutDirection|locale|orientation|keyboardHidden|screenSize|smallestScreenSize|density|keyboard|navigation|screenLayout|uiMode" android:theme="@style/Theme.AppCompat.NoActionBar">
7+
<meta-data android:name="android.app.lib_name" android:value="bevy_mobile_example" />
8+
<intent-filter>
9+
<action android:name="android.intent.action.MAIN" />
10+
<category android:name="android.intent.category.LAUNCHER" />
11+
</intent-filter>
12+
</activity>
13+
</application>
14+
15+
</manifest>

examples/mobile/android_example/app/src/main/cpp/dummy.cpp renamed to examples/mobile/android/app/src/main/cpp/dummy.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
// Intentionally blank -- this is a dummy code!
2-
// This is part of a hack to convince gradle to insert libc++_shared.so
1+
// Intentionally blank -- this is a dummy code!
2+
// This is part of a hack to convince gradle to insert libc++_shared.so
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.bevyengine.example
2+
3+
import androidx.core.view.WindowCompat
4+
import androidx.core.view.WindowInsetsCompat
5+
import androidx.core.view.WindowInsetsControllerCompat
6+
import com.google.androidgamesdk.GameActivity
7+
8+
/**
9+
* Loads the rust library and handles android specific to integrate with it.
10+
*
11+
*
12+
* The library is loaded at class initialization and provided by jniLibs.
13+
*/
14+
class MainActivity : GameActivity() {
15+
/**
16+
* Called when the current Window of the activity gains or loses focus.
17+
*
18+
*
19+
* This just hides the system UI if the app window is focused.
20+
*/
21+
override fun onWindowFocusChanged(hasFocus: Boolean) {
22+
// Call parent class implementation of onWindowFocusChanged to make sure that we are updating correctly.
23+
super.onWindowFocusChanged(hasFocus)
24+
25+
// If the window has focus, hide system UI.
26+
if (hasFocus) {
27+
hideSystemUi()
28+
}
29+
}
30+
31+
/**
32+
* Hides system UI.
33+
*
34+
*
35+
* This will make the app content fill the entire screen.
36+
*/
37+
private fun hideSystemUi() {
38+
val windowInsetsController =
39+
WindowCompat.getInsetsController(window, window.decorView)
40+
41+
// Show bars if swiping
42+
windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
43+
// Hide both the status bar and the navigation bar.
44+
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
45+
}
46+
47+
companion object {
48+
// Load rust library
49+
init {
50+
System.loadLibrary("bevy_mobile_example")
51+
}
52+
}
53+
}

assets/android-res/mipmap-mdpi/ic_launcher.png renamed to examples/mobile/android/app/src/main/res/mipmap-mdpi/ic_launcher.png

File renamed without changes.
File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# https://docs.gradle.org/current/userguide/build_environment.html
2+
org.gradle.configuration-cache=true
3+
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
4+
5+
# https://developer.android.com/topic/libraries/support-library/androidx-rn
6+
android.useAndroidX=true
7+
8+
# Enables namespacing of each library's R class so that its R class includes only the
9+
# resources declared in the library itself and none from the library's dependencies,
10+
# thereby reducing the size of the R class for that library
11+
android.nonTransitiveRClass=true
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#This file is generated by updateDaemonJvm
2+
toolchainUrl.FREE_BSD.AARCH64=https\://api.foojay.io/disco/v3.0/ids/73c462e34475aeb6509ab7ba3eda218f/redirect
3+
toolchainUrl.FREE_BSD.X86_64=https\://api.foojay.io/disco/v3.0/ids/9e87f9444e29ce8efb3f66e8435d94b4/redirect
4+
toolchainUrl.LINUX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/73c462e34475aeb6509ab7ba3eda218f/redirect
5+
toolchainUrl.LINUX.X86_64=https\://api.foojay.io/disco/v3.0/ids/9e87f9444e29ce8efb3f66e8435d94b4/redirect
6+
toolchainUrl.MAC_OS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/1050b2216f8beaaecc1289b17d30b586/redirect
7+
toolchainUrl.MAC_OS.X86_64=https\://api.foojay.io/disco/v3.0/ids/2208feeb3d4e12f412e9a450db1a842a/redirect
8+
toolchainUrl.UNIX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/40b4344c056b4284246d176d9701577f/redirect
9+
toolchainUrl.UNIX.X86_64=https\://api.foojay.io/disco/v3.0/ids/9e87f9444e29ce8efb3f66e8435d94b4/redirect
10+
toolchainUrl.WINDOWS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/a86b70d151aa8c1a054e80f417232fa2/redirect
11+
toolchainUrl.WINDOWS.X86_64=https\://api.foojay.io/disco/v3.0/ids/69a793dd932268c7d1ae9d8b855de8ed/redirect
12+
toolchainVersion=17

0 commit comments

Comments
 (0)