I want to use Flutter to create an Android app which depends on a third-party SDK that wrapped in an aar file. So far, I have only found the article Accessing Platform and Third-Party Services in Flutter. It seems that I have to build the Flutter project with additional Java files using Android Studio. This is complicated and not what I want. Is there any way to directly load aar files or *.so files in Dart code? Something like how JNI works.
Option A - Depend on the Android Archive (AAR) This option packages your Flutter library as a generic local Maven repository composed of AARs and POMs artifacts. This option allows your team to build the host app without installing the Flutter SDK.
Creating the application:Step 1: Open the IDE and select Start a new Flutter project. Step 2: Select the Flutter Application as the project type. Then click Next. Step 3: Verify the Flutter SDK path specifies the SDK's location (select Install SDK… if the text field is blank).
Step 1: Open the Android Studio and select Tools from the menu bar and click on SDK Manager. Step 2: In the newly open window click on the plugins and in the search bar search for Flutter and Dart and then install it. Step 4: Now after installing Flutter and Dart we are ready to import a Flutter project.
After learning the Flutter example - hello services, I have successfully built my Flutter project with aar file.
Basic Steps:
I've pushed the source code to GitHub.
Flutter can be embedded into your existing Android application piecemeal, as a source code Gradle subproject or as AARs.
Depend on the Android Archive (AAR)
This option packages your Flutter library as a generic local Maven repository composed of AARs and POMs artifacts. This option allows your team to build the host app without installing the Flutter SDK. You can then distribute the artifacts from a local or remote repository.
Let’s assume you built a Flutter module at some/path/my_flutter
, and then run:
cd some/path/my_flutter
flutter build aar
Then, follow the on-screen instructions to integrate.
More specifically, this command creates (by default all debug/profile/release modes) a local repository, with the following files:
build/host/outputs/repo
└── com
└── example
└── my_flutter
├── flutter_release
│ ├── 1.0
│ │ ├── flutter_release-1.0.aar
│ │ ├── flutter_release-1.0.aar.md5
│ │ ├── flutter_release-1.0.aar.sha1
│ │ ├── flutter_release-1.0.pom
│ │ ├── flutter_release-1.0.pom.md5
│ │ └── flutter_release-1.0.pom.sha1
│ ├── maven-metadata.xml
│ ├── maven-metadata.xml.md5
│ └── maven-metadata.xml.sha1
├── flutter_profile
│ ├── ...
└── flutter_debug
└── ...
To depend on the AAR, the host app must be able to find these files.
To do that, edit app/build.gradle
in your host app so that it includes the local repository and the dependency:
android {
// ...
}
repositories {
maven {
url 'some/path/my_flutter/build/host/outputs/repo'
// This is relative to the location of the build.gradle file
// if using a relative path.
}
maven {
url 'https://storage.googleapis.com/download.flutter.io'
}
}
dependencies {
// ...
debugImplementation 'com.example.flutter_module:flutter_debug:1.0'
profileImplementation 'com.example.flutter_module:flutter_profile:1.0'
releaseImplementation 'com.example.flutter_module:flutter_release:1.0'
}
Tip: You can also build an AAR for your Flutter module in Android Studio using the Build > Flutter > Build AAR
menu.
Your app now includes the Flutter module as a dependency. You can follow the next steps in the Adding a Flutter screen to an Android app.
For more info, see Integrate a Flutter module into your Android project
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With