How to use local aar inside flutter plugin?
Asked Answered
S

6

25

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.

Scathing answered 21/6, 2018 at 14:28 Comment(2)
For me the .aar file is compiled, and Balazs Banyai's answer is helpful.Zacarias
Did anyone try importing an android module to a flutter plugin?Confect
U
7

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'
Unarm answered 21/6, 2018 at 16:47 Comment(1)
Can I use local maven repository in CI/CD?Liege
C
43

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.

Cafard answered 17/7, 2019 at 12:34 Comment(1)
Thanks for your answer , it was helpful. If anyone gets "Project with path <plugin_name> could not be found in root project <project_name>" error you can fix it by adding include ':<plugin_name>' to settings.gradle in your android folder.Kinaesthesia
C
10

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

Christcross answered 25/5, 2020 at 0:25 Comment(0)
U
7

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'
Unarm answered 21/6, 2018 at 16:47 Comment(1)
Can I use local maven repository in CI/CD?Liege
C
1

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

Cranford answered 3/11, 2021 at 7:22 Comment(0)
A
1

With the flatDir solution you will get a warning when you try to sync your project with the Gradle files:

using flatDir should be avoided because it doesn't support any meta-data formats.

You can put this in your build.gradle instead to get rid of the warning:

android {
    sourceSets {
        main.jniLibs.srcDirs += project(':<plugin_name>').file('libs')
    }
}

dependencies {
    implementation files('libs/<dependency_name>.aar')
}

Unfortunately, with both solutions you'll get this error when you try to build the project:

Direct local .aar file dependencies are not supported when building an AAR. The resulting AAR would be broken because the classes and Android resources from any local .aar file dependencies would not be packaged in the resulting AAR. Previous versions of the Android Gradle Plugin produce broken AARs in this case too (despite not throwing this error).

The accepted answer seems to be correct. I put the library inside the app's project instead because both options of the accepted answer were not possible in my case.

For this I had to create a new directory in the android directory of my app (i.e. example/android/<dependency_name>) and put my .aar file and a new build.gradle in that directory.

android/build.gradle:

dependencies {
    implementation project(':<dependency_name>')
}

example/android/<dependency_name>/build.gradle:

configurations.maybeCreate("default")
artifacts.add("default", file('<dependency_name>.aar'))

example/android/settings.gradle:

include ':<dependency_name>'
Ambulator answered 18/11, 2022 at 14:54 Comment(2)
You put the dependency in the example folder??Strung
In the android folder of each app that uses the dependency. The solution of the accepted answer is better if you can build the aar yourself.Ambulator
R
0

I don't know why but I did a bit different to add aar file to flutter.

1: Open then android folder of your flutter project in new android studio window 2: Create a libs folder at android\app\libs -> add aar file in this 3: Modify anroid\app\build.gradle -> add dependencies and repositories at the end Ex my gradle:

dependencies {
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation files('libs/AAR_FILE_NAME.aar')
}

repositories {
    flatDir {
        dirs 'libs'
    }
}

4: run sync, and aar file will be add as extend library to use in native android

P/S: Make sure to check if there is a conflit of Java and flutter. In my case, the implementation 'androidx.appcompat:appcompat:1.7.0'conflict with flutter so if make 180+ error. After delete it, every things become ok

P/S 2: at android\gradle\wrapper\gradle-wrapper.properties check distributionUrl version, I use 1.5 which make error, only fix after upgrade to 8.0

Riel answered 9/7 at 4:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.