问题
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.
回答1:
In my experience, adding direct plugin aar in another plugin project (flutter plugin's android project) is either very difficult or not allowed, so in that case, you have two options,
If your aar dependancy is available on gradle, use gradle that in your build file. You can search for it here
Or if you are building aar yourself from another project, then you can publish aar to your local maven and use that dependency in your flutter plugin by adding "mavenLocal()" to your plugins build file.
For example here is what I did to fix same issue: My dependancy's build.gradle
group 'com.companyname.artifactname'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenLocal()
google()
jcenter()
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
allprojects {
repositories {
mavenLocal()
google()
jcenter()
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
apply plugin: 'com.android.library'
uploadArchives {
repositories {
mavenLocal()
}
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 2
versionName "2.0.1"
}
lintOptions {
abortOnError false
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:support-v4:23.2.1'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
}
apply plugin: 'maven-publish'
publishing {
publications {
library(MavenPublication) {
version "1.1"
artifact(bundleRelease)
}
}
}
Then just run following command to publish it to local maven repository
gradlew publishToMavenLocal
And then just add mavenLocal() in your flutter plugin's repositories section and add dependancy as
compile 'com.companyname.artifactname:libraryname:1.1@aar'
回答2:
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 (/android/libs) (on top level of your plugin, not in the example project)
2) add your .aar files in this folder
3) add your .aar dependencies to your plugins build.gradle file (/android/build.gradle) e.g.
dependencies {
...
implementation (name: 'myFirstDependency', ext: 'aar')
implementation (name: 'myOtherDependency', ext: 'aar')
}
3) 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 (/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.
来源:https://stackoverflow.com/questions/50971022/how-to-use-local-aar-inside-flutter-plugin