Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 559
Vladimir Avatar asked Jun 21 '18 14:06

Vladimir


People also ask

How add AAR to project?

Add your AAR or JAR as a dependencyNavigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Jar Dependency in the dropdown. In the Add Jar/Aar Dependency dialog, first enter the path to your . aar or .


1 Answers

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.

like image 117
Michael Polt Avatar answered Oct 11 '22 09:10

Michael Polt