Mobile Development 14 min read

How to Solve Flutter Multi‑Instance Conflicts When Integrating AAR into Android Apps

This article explains why integrating a Flutter AAR into an Android project that already contains a Flutter app causes version, class, native library, and resource conflicts, and presents a comprehensive solution using custom class loading, library renaming, and resource isolation to enable seamless coexistence.

NetEase Smart Enterprise Tech+
NetEase Smart Enterprise Tech+
NetEase Smart Enterprise Tech+
How to Solve Flutter Multi‑Instance Conflicts When Integrating AAR into Android Apps

Introduction

Flutter CLI can package a Flutter module as an Android AAR for external use. Using a Flutter AAR inside an Android project that already contains a Flutter app leads to the “Flutter multi‑instance” problem.

Packaging Options

Two ways to distribute a Flutter SDK: Flutter Package/Plugin (source) and Android AAR. The AAR approach hides source code and avoids requiring the host to use Flutter, but introduces conflicts when both a Flutter app and a Flutter AAR are present.

Typical Conflicts

Flutter version and embedding JAR mismatches.

Plugin and resource version clashes.

GeneratedPluginRegistrant.java duplication.

libapp.so AOT library conflict.

Runtime errors: multiple AOT libraries in one engine, asset/font manifest overrides.

Solution Overview

The proposed solution avoids engine modifications and extra host changes. It uses a custom ClassLoader to load a DEX file containing all AAR‑dependent classes, renames native libraries (libflutter.so, libapp.so) to avoid collisions, and isolates Flutter resources.

Class Conflict Handling

Package the AAR’s plugin classes and embedding JAR into a DEX placed in assets. At runtime copy the DEX to a private directory and load it with a DexClassLoader that overrides findClass to prefer the child loader.

Native Library Conflict Handling

Rename the engine library ( libflutter.so) and the AOT library ( libapp.so) and adjust the loading logic in FlutterLoader and the native engine options ( --aot-shared-library-name) to point to the renamed files.

Resource Conflict Handling

Either merge the JSON manifests at compile time or publish a separate resource package that the host Flutter app depends on, ensuring the final build contains a combined AssetsManifest.json and FontManifest.json.

Pros and Cons

No engine modifications → high maintainability.

Independent Flutter versions for app and AAR.

Increased APK size and memory due to two engine instances.

Conclusion

By isolating class loading, renaming native libraries, and managing resources, a Flutter AAR can coexist with a Flutter app, enabling SDK development with Flutter while preserving the benefits of cross‑platform development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Fluttermobile-developmentAARMulti-Instance
NetEase Smart Enterprise Tech+
Written by

NetEase Smart Enterprise Tech+

Get cutting-edge insights from NetEase's CTO, access the most valuable tech knowledge, and learn NetEase's latest best practices. NetEase Smart Enterprise Tech+ helps you grow from a thinker into a tech expert.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.