androidflutterflutter-plugin

How to use local aar inside flutter plugin?


I've create a flutter plugin with:

flutter create --template plugin flutter_plugin

I've put my aar file inside flutter_plugin/android/src/main/libs folder
I've modified flutter_plugin/android/build.gradle

and changed rootProject.allprojects section to

rootProject.allprojects {                                                                                               
     repositories {                                                                                                      
         google()                                                                                                        
         jcenter()                                                                                                       
         flatDir {                                                                                                       
             dirs "src/main/libs"                                                                                  
         }                                                                                                               
     }                                                                                                                   
}  

And added dependencies section, after android {}:

dependencies {                                                                                                          
     implementation (name:"mylib",ext:"aar")                                                                     
} 

but when i try running with: flutter run

I get gradle exception, apparently it tried to look for my mylib.aar inside example directory: example/src/main/libs/mylib.aar and failed.

I can put my lib inside example dir, but i don't think it's the right way, as i want my aar to be part of the plugin.


Solution

  • 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'