Bishal
Bishal

Reputation: 99

How to Import AAR file in the flutter plugin?

I am working in a flutter plugin and want to import an .aar project in android part of the plugin. I have tried opening the android project and importing the .aar project by importing the module, including it in setting.gradle and adding it in dependency of build.grade (like any other native android project). However, when I run the flutter project, the .aar project is not found.

The error I get is

A problem occurred evaluating project ':flutter_plugin_andriod'

Project with path ':commonlib' could not be found in project ':flutter_plugin_andriod'.

enter image description here

Anybody with the fix ?

Upvotes: 8

Views: 13056

Answers (5)

Bart
Bart

Reputation: 1

This took me some time to figure out.

Create an empty folder in your Flutter android and add your aar file in my case the folder was called ftsdk and excute this command (you might need to brew install maven)

mvn install:install-file -Dfile=facetec-sdk-9.6.51-minimal.aar \
-DgroupId=com.facetec.sdk \
-DartifactId=default \
-Dversion=1.0.0 \
-Dpackaging=aar \
-DlocalRepositoryPath=./repository

This will generate a local maven repository in the same folder (if you leave out '-DlocalRepositoryPath' then it will default to your main maven folder usually '/Users/myUser/.m2'

In my Flutter Android build. gradle I included this directory under repositories (make sure its the allprojects repositories)

allprojects {
repositories {
    google()
    mavenCentral()
    //mavenLocal() use this line if you let it generate in the default maven folder in /Users/myUser/.m2
    maven {
        url "$projectDir/ftsdk/repository"
    }
}

}

Then in your build gradle dependencies you can add it as follows

android{
...
    dependencies {
        implementation 'com.facetec.sdk:default:1.0.0';
    }
...
}

Answer inspired by this king: https://alessiobianchi.eu/blog/obfuscated-aar-local-maven/

Upvotes: 0

empiresdev
empiresdev

Reputation: 389

There is an open issue at Flutter's Github which at this day is still open.

The only way I could solve and import an AAR library into a Flutter Plugin was following these steps:

  1. Rename .aar file to .zip
  2. Extract zip files and copy classes.jar into libs folder of you Flutter plugin
  3. Rename it to the same .aar file, so you won't get lost when importing it
  4. Add the .jar to your dependencies:
dependencies {
   implementation fileTree(include: ['*.jar'], dir: 'libs')
}
  1. Build the plugin normally.

Upvotes: 1

Bishal
Bishal

Reputation: 99

I finally found the answer.

  • create a lib folder in directory where the build.gradle is and place your aar file in the folder.
  • Then add flatDir { dirs project(':plugin_name').file('libs')} in your rootProject.allprojects
  • Then add dependency in the build. file in the dependencies section as:
    api(name: 'your_aar_file_name', ext: 'aar')

    Note: Make sure to add all the dependencies included in gradle.build file (if your aar file depends on any gradle.build file of its own) in the dependencies section of your application build.gradle file . This was the main issue in my case.

Upvotes: 1

Murat Kurbanov
Murat Kurbanov

Reputation: 837

Follow these steps:

Create folder libs in plugin_folder/android/ and put your *.aar file there.

In plugin_folder/android/build.gradle

rootProject.allprojects {
    repositories {
        google()
        mavenCentral()
        flatDir {
            dirs project(':plugin_name').file('libs')
        }
    }
}
...
dependencies {
    implementation (name: 'file-sdk-name', ext: 'aar')
    ...
}

Good luck!

Upvotes: 3

Sergio Clemente
Sergio Clemente

Reputation: 888

Put your .aar in Android/app/libs In Android/app/build.gradle you import the aar :

dependencies {
   implementation fileTree(include: ['*.jar'], dir: 'libs')
   implementation files('libs/myaarlibr.aar')
}

After this your .aar is ready for use

Upvotes: 3

Related Questions