|
1 | 1 | # Native Activity |
2 | 2 |
|
| 3 | +> [!WARNING] |
| 4 | +> **Most apps should not use the app development model shown in this sample**. |
| 5 | +> Instead, use a Java or Kotlin `AppCompatActivity` and connect your native code |
| 6 | +> using JNI like the other samples in this repository. `NativeActivity` and |
| 7 | +> `GameActivity` attempt to translate the Android [activity lifecycle] into a |
| 8 | +> desktop style `main()` function with a polled event loop. That is not how |
| 9 | +> Android apps work, and while it may help you get your prototype running more |
| 10 | +> quickly, as your app matures you will likely end up retranslating the |
| 11 | +> `native_app_glue` model to again look like `Activity`. |
| 12 | +
|
3 | 13 | This is an Android sample that uses [NativeActivity] with `native_app_glue`, |
4 | 14 | which enables building NDK apps without having to write any Java code. In |
5 | 15 | practice most apps, even games which are predominantly native code, will need to |
6 | | -call some Java APIs or customize their app's activity further. |
| 16 | +call some Java APIs or customize their app's activity further. While it may save |
| 17 | +you a small amount of effort during prototyping, it may result in a difficult |
| 18 | +migration later. It's also worth noting that some of the code in this sample is |
| 19 | +spent undoing the work of `native_app_glue` to create a class very similar to |
| 20 | +`Activity`. |
7 | 21 |
|
8 | 22 | The more modern approach to this is to use [GameActivity], which has all the |
9 | 23 | same benefits as `NativeActivity` and `native_app_glue`, while also making it |
10 | 24 | easier to include Java code in your app without a rewrite later. It's also |
11 | | -source compatible. This sample will likely migrate to `GameActivity` in the |
12 | | -future. |
| 25 | +source compatible. However, it still has all the problems explained in the |
| 26 | +warning above, and in practice neither `NativeActivity` nor `GameActivity` is |
| 27 | +the recommended app development model. |
13 | 28 |
|
14 | 29 | The app here is intentionally quite simple, aiming to show the core event and |
15 | 30 | draw loop necessary for an app using `native_app_glue` without any extra |
16 | 31 | clutter. It uses `AChoreographer` to manage the update/render loop, and uses |
17 | 32 | `ANativeWindow` and `AHardwareBuffer` to update the screen with a simple color |
18 | 33 | clear. |
19 | 34 |
|
| 35 | +[activity lifecycle]: https://developer.android.com/guide/components/activities/activity-lifecycle |
20 | 36 | [GameActivity]: https://developer.android.com/games/agdk/game-activity |
21 | 37 | [NativeActivity]: http://developer.android.com/reference/android/app/NativeActivity.html |
| 38 | + |
| 39 | +## Walkthrough |
| 40 | + |
| 41 | +The interesting sections of code in this sample are in three files: |
| 42 | +[AndroidManifest.xml], [CMakeLists.txt], and [main.cpp]. Each of those files has |
| 43 | +code comments explaining the portions relevant to using `NativeActivity`, but |
| 44 | +the high level details of the app are explained here. |
| 45 | + |
| 46 | +This app uses `NativeActivity` rather than its own child class of `Activity` or |
| 47 | +`AppCompatActivity`. This is specified in the `<activity />` declaration in [the |
| 48 | +manifest]. |
| 49 | + |
| 50 | +Apps which use `NativeActivity` are typically written using `native_app_glue`, |
| 51 | +which adapts the Android activity lifecycle code to look more like a desktop |
| 52 | +program with a `main()` function and an event loop. This is set up in the app's |
| 53 | +[CMakeLists.txt file]. |
| 54 | + |
| 55 | +When using `native_app_glue` with a [version script], you must export |
| 56 | +`ANativeActivity_onCreate`. This sample does this in |
| 57 | +[libnative-activity.map.txt]. |
| 58 | + |
| 59 | +This is a fairly simple application, so all of the code is in a single file, |
| 60 | +[main.cpp]. The entry point for an app using `native_app_glue` is |
| 61 | +`android_main()`. That function is the best place to start reading in this file |
| 62 | +to learn how the sample works, then follow through to the definition of |
| 63 | +`engine_handle_cmd` and `Engine`. |
| 64 | + |
| 65 | +[CMakeLists.txt file]: app/src/main/cpp/CMakeLists.txt |
| 66 | +[libnative-activity.map.txt]: app/src/main/cpp/libnative-activity.map.txt |
| 67 | +[main.cpp]: app/src/main/cpp/main.cpp |
| 68 | +[the manifest]: app/src/main/AndroidManifest.xml |
| 69 | +[version script]: https://developer.android.com/ndk/guides/symbol-visibility |
0 commit comments