5

I've written a Flutter plugin to use an SDK that requires the inclusion of some .aar modules. It builds and runs perfectly in the example app for the plugin, but when I import the plugin in a different app and try to build it, the build immediately fails with a message saying that one of the .aar modules could not be found in the plugin. This makes no sense because the module is definitely there - the platform channels to use the SDK would fail in the example app if the module wasn't there.

Why would the example app build and run without any problems but a different app won't? The only thing I can think of is that I import the plugin from path in my pubspec but it seems unlikely to me that this is the culprit.

Any advice or assistance here would be appreciated. TIA!

GroovinChip
  • 339
  • 4
  • 17
  • By referring to the error you mentioned probably the IDE is not able to find the path for the module,Make sure you are importing the aar module correctly and also check for the path check if this helps https://stackoverflow.com/a/23326397/8253662 – Mahesh Jamdade Dec 02 '20 at 01:42
  • Also this question requires more info for others to help you out,if you could attach some screenshots of the error and where exactly is the aar module placed and how it's being included people would help you out better. – Mahesh Jamdade Dec 02 '20 at 01:43
  • @maheshmnj Thanks for your response. I did make extra sure as I was importing the aar modules that I was following the instructions provided by the SDK to the letter. And as I've said, since the example app runs without issue, it is clear that I did it right. The problem occurs when importing the plugin from path in another app. The specific message is: `Project with path ':module' could not be found in project ':plugin'.` (not real names). I did follow the instructions in the answer you linked to as well, and that changed nothing. – GroovinChip Dec 02 '20 at 16:14
  • @maheshmnj The steps I took to import the aar modules were: 1) File > New Module > Import .aar package > choose .aar from files > Finish 2) Project structure > modules > + the .aar module I just selected 3) Project structure > dependencies > my project > + the module I just added 4) Ensure dependencies are listed in build.gradle – GroovinChip Dec 02 '20 at 16:16
  • Something that occurs to me is using `api` instead of `implementation` for adding the dependencies in build.gradle, since https://developer.android.com/studio/build/dependencies says `implementation` keeps the modules from being accessed by other modules whereas `api` allows other modules to access. However, doing so yields no change. – GroovinChip Dec 02 '20 at 17:56

1 Answers1

8

I got it!!!!

The answer is as found here: How to add .aar dependency in library module?

The way this adapts to a Flutter plugin is as follows:

  1. Add a libs folder at the root of the android project in the plugin. Add the .aar files there.
  2. In the plugin's build.gradle file, update rootProject.allProjects to look as follows:
    repositories {
        google()
        jcenter()
        flatDir {
            dirs 'libs'
            dirs project(':your_plugin_name_here').file('libs')
        }
    }
}
  1. Still in the same build.gradle, add your .aar file(s) as dependencies as follows:
implementation(name:'aar_name_here', ext:'aar')
  1. In the Flutter app that you want to use the plugin for, open the app-level build.gradle file and add the plugin itself as a dependency, like so:
android {
  ...
  
    dependencies {
        implementation project(':your_flutter_plugin');
    }
}
  1. In the settings.gradle file for the app that us using the plugin, change
include ':app'

to

include ':app', ':your_flutter_plugin'

And this should do it!!

GroovinChip
  • 339
  • 4
  • 17
  • For me this didn't work. Instead of the `settings.gradle` adjustment I've added `implementation(name:'aar_name_here', ext:'aar')` to the consuming project's `build.gradle`. – SEG.Veenstra Jul 16 '21 at 07:25