6

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 ?

Anna Gevel
  • 1,103
  • 1
  • 11
  • 20
Bishal
  • 81
  • 1
  • 5

4 Answers4

2

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

Sergio Clemente
  • 809
  • 8
  • 12
2

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.
Bishal
  • 81
  • 1
  • 5
  • This doesn't work for me. Got the following errror "* What went wrong: A problem occurred evaluating project ':myPlugin'. > Could not find method api() for arguments [{name=my_arr_file, ext=aar}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. " – Kosmonaft Nov 29 '22 at 21:46
  • Where is rootProject.allprojects ? – Camilo Ortegón Jan 26 '23 at 22:15
  • @CamiloOrtegón its a block in you build.gradle file if its not in your file just create one – Bishal Apr 04 '23 at 03:00
1

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!

Murat Kurbanov
  • 589
  • 7
  • 11
  • 1
    This doesn't work either for me. Got the following error `* What went wrong: A problem occurred evaluating project ':my_plugin_name'. > Project with path ':plugin_name' could not be found in root project 'android'.` – Kosmonaft Nov 29 '22 at 21:58
  • try replacing "implementation" with "api", it worked for me . – Bishal Nov 30 '22 at 05:45
  • 1
    You can solve this `Project with path could not be found in root project ` error by adding `include ':'` to settings.gradle in your android folder. @Kosmonaft – yazicifatihcan Dec 05 '22 at 13:53
0

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.
empiresdev
  • 153
  • 1
  • 7