Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build Flutter project with Android aar file?

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.

like image 731
yushulx Avatar asked Jan 16 '17 03:01

yushulx


People also ask

What is build AAR in Flutter?

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.

Can I develop Flutter app from Android Studio?

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).

How do I create a Flutter project in Android Studio?

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.


2 Answers

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.

like image 74
yushulx Avatar answered Oct 14 '22 11:10

yushulx


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

like image 28
Paresh Mangukiya Avatar answered Oct 14 '22 11:10

Paresh Mangukiya