How to build Flutter project with Android aar file?

11,704

Solution 1

After learning the Flutter example - hello services, I have successfully built my Flutter project with aar file.

Basic Steps:

  1. Download the Flutter source code.
  2. Open flutter/examples/hello_services/android in Android Studio.
  3. Click File > New > New Module and choose Import .JAR/.AAR Package.
  4. Press F4 to open Project Structure, and then add the dependent module.
  5. Write Java code to invoke APIs that defined in aar file.
  6. Import flutter/examples/hello_services to Intellij IDEA.
  7. Build and run the Flutter app.

I've pushed the source code to GitHub.

Solution 2

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.

enter image description here

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.

enter image description here

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

Share:
11,704

Related videos on Youtube

yushulx
Author by

yushulx

{ "About me": { "roles": ["father", "developer", "marketer", "manager"], "interests": ["learning", "cooking", "coding", "marketing", "gaming"], "email": "[email protected]", "twitter": "twitter.com/yushulx" } }

Updated on June 04, 2022

Comments

  • yushulx
    yushulx almost 2 years

    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.

  • Ted Henry
    Ted Henry over 4 years
    Unfortunately, flutter/examples/hello_services no longer exists. There is flutter/examples/platform_channel which may the way to do it now?
  • Jaswant Singh
    Jaswant Singh over 3 years
    @TedHenry. You dont need the example app. You can simply include the dependency by opening your "Android" folder inside the android studio and following the basic steps to include an aar file. And after that use method channels to invoke the native functions.
  • corn on the cob
    corn on the cob over 3 years
    can you add context around your link in case it ever breaks, please? thank you!
  • Didier Prophete
    Didier Prophete over 2 years
    oh man, I getting the exact same error... Actually you get this when trying to add an aar to a brand new flutter project... what a mess...