Mobile Development 9 min read

Integrating AAR Libraries into Flutter Plugins and Resolving Build Errors

This tutorial explains how to incorporate third‑party AAR files into a Flutter plugin, configure the Android host project, address the bundleReleaseAar build error, and successfully build a Flutter application APK.

360 Smart Cloud
360 Smart Cloud
360 Smart Cloud
Integrating AAR Libraries into Flutter Plugins and Resolving Build Errors

When developing Flutter projects, you may need to use a Flutter plugin that depends on a third‑party AAR library; compiling the Flutter application can then fail. This article demonstrates a solution by creating four related projects—an Android native app, a Flutter application, a Flutter module, and a Flutter plugin—organized under a common FlutterProject directory.

First, create the projects using the following commands:

cd Desktop/FlutterProject/
# create Flutter application
flutter create --org com.tmt.flutterapp flutter_app
# create Flutter module
flutter create --org com.tmt --template=module flutter_module
# create Flutter plugin for AAR packaging
flutter create --org com.tmt.flutter_plugin_aar --template=plugin flutter_plugin_aar

In the flutter_plugin_aar project, add a libs folder, place the AAR file there, and import it in android/build.gradle :

dependencies {
    implementation fileTree(dir: "libs", include: ['*.aar'])
}

Expose the AAR’s API from the plugin by modifying onMethodCall :

public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
        result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else if (call.method.equals("aarPrint")) {
        // call AAR API
        AarTest.print();
        result.success("success");
    } else {
        result.notImplemented();
    }
}

The AAR source code used above is:

package com.tmt.mylibrary;
import android.util.Log;
public class AarTest {
    public static void print() {
        Log.d("AarTest", "this is aar output log");
    }
}

To integrate the plugin with the Android native project, modify settings.gradle to include the Flutter module and add the plugin’s AAR to the host app, and change MainActivity to extend FlutterActivity :

package com.tmt.androidapp;
import android.os.Bundle;
import io.flutter.embedding.android.FlutterActivity;
public class MainActivity extends FlutterActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

When building the host app with ./gradlew assembleRelease , the following error appears because a direct local AAR dependency is not allowed in an AAR:

> Task :flutter_plugin_aar:bundleReleaseAar FAILED
Execution failed for task ':flutter_plugin_aar:bundleReleaseAar'.
Direct local .aar file dependencies are not supported when building an AAR...

The cause is that Flutter’s build process packages each plugin’s Android code into an AAR, and an AAR cannot itself depend on another local AAR. Two work‑arounds are provided:

Declare the AAR as compileOnly in the plugin so it is ignored during the plugin’s AAR build, and let the host Android app include the AAR as a normal dependency.

Unzip the AAR into JAR and resource files and place them directly in the plugin (not demonstrated).

The plugin’s build.gradle is updated as follows:

apply plugin: 'com.android.library'
apply from: './aar_tools.gradle'
android {
    compileSdkVersion 30
    defaultConfig { minSdkVersion 16 }
}
dependencies {
    //implementation fileTree(dir: "libs", include: ['*.aar'])
    compileOnly fileTree(dir: "libs", include: ['*.aar'])
}

A custom aar_tools.gradle script copies the AAR from the plugin’s libs directory into the host app’s libs folder and adds it as an implementation dependency:

static void aarFileCopy(String srcPathStr, String desPathStr) { ... }
copyAar2Host('com.tmt.flutter_plugin_aar')
void copyAar2Host(String pluginGroup) { ... }
repositories { flatDir { dirs 'libs' } }

After applying these changes, the flutter_app runs correctly and flutter build apk succeeds, and the Android host project also builds without errors. The article concludes that moving the AAR dependency to the Android application and using compileOnly in the plugin resolves the build‑time conflict.

FlutterAndroidPluginGradlemobile-developmentAARbuild-error
360 Smart Cloud
Written by

360 Smart Cloud

Official service account of 360 Smart Cloud, dedicated to building a high-quality, secure, highly available, convenient, and stable one‑stop cloud service platform.

0 followers
Reader feedback

How this landed with the community

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