How to use local aar inside flutter plugin?

My solution to add local .aar depencies to a flutter plugin is as follows (based on the instructions here How to add .aar dependency in library module?):

  1. create a libs folder in your android plugin source code (<plugin_name>/android/libs) (on top level of your plugin, not in the example project)

  2. add your .aar files in this folder (e.g. myFirstDependency.aar)

  3. add your .aar dependencies (without the .aar extension) to your plugins build.gradle file (<plugin_name>/android/build.gradle) e.g.

dependencies {
    ...
    implementation (name: 'myFirstDependency', ext: 'aar')
    implementation (name: 'myOtherDependency', ext: 'aar')
}
  1. in order for gradle to be able to find these dependencies you need to instruct gradle to search in the libs folder inside of the plugin. Therefore add the following to your plugins build.gradle (<plugin_name>/android/build.gradle, same as for your dependencies) under rootProject.allprojects repositories section. (NOTE: there is also a buildscripts a repositories section which you should not change for this).
rootProject.allprojects {
    repositories {
        google()
        jcenter()

        //Add this below <plugin_name> is whatever your plugin is called eg. url_launcher
        flatDir {
            dirs project(':<plugin_name>').file('libs')
            // e.g. dirs project(':url_launcher').file('libs')  - don't miss the ':'
        }
    }
}

With that, you don't have the .aar library dependencies directly in your plugin.


In my experience, adding direct plugin aar in another plugin project (flutter plugin's android project) is either very difficult or not allowed, so in that case, you have two options,

  1. If your aar dependancy is available on gradle, use gradle that in your build file. You can search for it here

  2. Or if you are building aar yourself from another project, then you can publish aar to your local maven and use that dependency in your flutter plugin by adding "mavenLocal()" to your plugins build file.

For example here is what I did to fix same issue: My dependancy's build.gradle

group 'com.companyname.artifactname'
version '1.0-SNAPSHOT'

buildscript {
    repositories {
        mavenLocal()
        google()
        jcenter()
        maven {
            url "https://maven.google.com" // Google's Maven repository
        }        
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}
allprojects {
    repositories {
        mavenLocal()
        google()
        jcenter()
        maven {
            url "https://maven.google.com" // Google's Maven repository
        }        
    }
}


apply plugin: 'com.android.library'


uploadArchives {
    repositories {
        mavenLocal()
    }
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 2
        versionName "2.0.1"
    }
    lintOptions {
        abortOnError false
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dependencies {
        compile 'com.android.support:appcompat-v7:23.2.1'
        compile 'com.android.support:support-v4:23.2.1'
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
}


apply plugin: 'maven-publish'
publishing {
    publications {
      library(MavenPublication) {
        version "1.1"
        artifact(bundleRelease)
      }
    }
}

Then just run following command to publish it to local maven repository

gradlew publishToMavenLocal

And then just add mavenLocal() in your flutter plugin's repositories section and add dependancy as

compile 'com.companyname.artifactname:libraryname:1.1@aar'

I created a libs directory in the android directory of my plugin. I placed the aar I generated in the libs directory:

enter image description here

I updated the build.gradle file for the android code to include the following:

rootProject.allprojects {
    repositories {
        google()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

And in the dependencies section:

implementation fileTree(dir: 'libs', include: ['*.aar'])

enter image description here


i managed to make it work by adding

maven { url("${project(':test_flutter_plugin').file('libs').path}/my_lib") }

into rootProject.allprojects

of plugin/android/build.gradle