Matan Lurey 670e54688a Follow various best practices in FlutterRenderer (flutter/engine#49651)
All of these are non-semantics changing and all were auto-applied by
IntelliJ:

- Make always `final` stuff final
- Avoid unnecessary casts
- Annotate `@NonNull` overrides
- Avoid initializing callbacks outside of the constructor, and use Java
lambdas when applicable
- Avoid cases where something is known to be non-null or non-false
- Use `<>` when applicable
2024-01-09 22:12:12 -08:00
..

Android Platform Embedder

Android embedder for Flutter, including Java io.flutter sources and Android specific engine-side C++.

This code provides the glue between the Flutter engine and the Android platform, and is responsible for:

  • Initializing the Flutter engine.
  • Providing a platform view for the Flutter engine to render into.
  • Dispatching events to the Flutter engine.

Caution

This is a best effort attempt to document the Android embedder. It is not guaranteed to be up to date or complete. If you find a discrepancy, please send a pull request!

See also:

Developing

How to edit and contribute to the Android embedder.

![TIP] This guide assumes you already have a working Engine development environment:

You should also have a working Android development environment:

It is also recommended (but not required) to install Visual Studio Code.

Depending on what you are trying to do, you may need to edit the Java code in io.flutter or the C++ code in shell/platform/android, sometimes both. Let's start with the C++ code, as it is more similar to developing for other platforms or platform-agnostic parts of the engine.

Editing C++ code

The C++ code for the Android embedder is located in shell/platform/android and subdirectories.

Some notable files include:

See VSCode with C/C++ Intellisense for how to use the clangd extension to get C++ code completion:

Example

![NOTE] > --compile-commands-dir must point to an Android build output:

{
  /* ... */
  "clangd.path": "buildtools/mac-arm64/clang/bin/clangd",
  "clangd.arguments": ["--compile-commands-dir=out/android_debug_unopt_arm64"]
  /* ... */
}

... but remember to change it back when editing other parts of the engine.

Editing Java code

The Java code for the Android embedder is located in io/flutter/ and subdirectories.

The tests are located in test/io/flutter/, and the test runner in test_runner.

Some notable files include:

It is non-trivial to get a working IDE setup for editing Java code in the Flutter engine. Some developers have had success using VSCode as an IDE for the Android Embedding, but the following instructions are for if that doesn't work, or you want to use Android Studio:

  1. Open shell/platform/android in Android Studio.

  2. Configure the following:

  3. Sync Gradle.

    Example

At this point you should be able to open Java files in Android Studio and get code completion in the io/flutter folder (additional, undocumented work is required for test/io/flutter). For example, FlutterJNI.java:

Example