18

I'm trying to write a Flutter plugin that relies on a third-party library on Android.

The dependency has a build.gradle file, I've got the source and I can build an aar file from the command line using gradle build.

I tried to add the dependency using Android Studio but it ended up in the my_plugin/example/android directory and no matter how many times did I try to move it to the plugin's my_plugin/android directory, I always failed.

The reason why this is an issue is that even though the example app can be built and run, I cannot use the plugin in other Flutter apps (which is the whole reason one would create a Flutter plugin).

How do I need to add a local library module (source code) or the aar (that I can build any time) to the Flutter plugin's Android implementation?

Vince Varga
  • 6,101
  • 6
  • 43
  • 60
  • 1
    In the end, I just copy-pasted the whole library into my app code, fixed the imports, changed the folder structure ¯\_(ツ)_/¯ – Vince Varga Mar 05 '19 at 16:16
  • 2
    agreed. the whole android plugin directory structure is very embedded; reminds me of the old days when everything was very tricky and undocumented. I've been scouring the example projects looking for an answer...none found yet. right now the flutter website is not accurate either, so it seems to be a neglected item – j3g May 13 '19 at 02:19

1 Answers1

2

well, you were halfway through the process...

  1. Go to File -> New module -> Select JAR/AAR, locate the JAR/AAR you want to add, decline creating a gradle wrapper and this will create a folder with your file and a build.gradle file. Copy the library folder name for usage in the next steps.
  2. Move the newly created folder to the android folder.
  3. Open settings.gradle in android folder and to the beginning of the file add this line:
    • include ':android/library_folder_name'
  4. Open build.gradle in android folder and in the dependencies block add this line:
    • implementation fileTree(dir: 'library_folder_name', include: ['*.aar', '*.jar'], exclude: [])
rexxar
  • 1,671
  • 1
  • 21
  • 27