如何在flutter插件中使用本地aar? [英] How to use local aar inside flutter plugin?

查看:2091
本文介绍了如何在flutter插件中使用本地aar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用以下方法创建了一个flutter插件:

I've create a flutter plugin with:

flutter create --template plugin flutter_plugin

我已将aar文件放入flutter_plugin/android/src/main/libs文件夹中
我已经修改了flutter_plugin/android/build.gradle

I've put my aar file inside flutter_plugin/android/src/main/libs folder
I've modified flutter_plugin/android/build.gradle

并将rootProject.allprojects部分更改为

and changed rootProject.allprojects section to

rootProject.allprojects {                                                                                               
     repositories {                                                                                                      
         google()                                                                                                        
         jcenter()                                                                                                       
         flatDir {                                                                                                       
             dirs "src/main/libs"                                                                                  
         }                                                                                                               
     }                                                                                                                   
}  

并在android {}之后添加了依赖项部分:

And added dependencies section, after android {}:

dependencies {                                                                                                          
     implementation (name:"mylib",ext:"aar")                                                                     
} 

但是当我尝试运行时:flutter run

but when i try running with: flutter run

我收到了gradle异常,显然它试图在示例目录:example/src/main/libs/mylib.aar中寻找我的mylib.aar并失败了.

I get gradle exception, apparently it tried to look for my mylib.aar inside example directory: example/src/main/libs/mylib.aar and failed.

我可以将lib放在示例dir中,但是我认为这不是正确的方法, 因为我希望我的aar成为插件的一部分.

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.

推荐答案

以我的经验,在另一个插件项目(flutter插件的android项目)中添加直接插件aar要么非常困难,要么不允许,因此在这种情况下,两种选择,

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,

  1. 如果gradle上可以使用aar依赖性,请在构建文件中使用gradle.您可以在此处

或者,如果您是从另一个项目构建自己的aar,则可以将aar发布到本地maven,并通过在插件构建文件中添加"mavenLocal()",在flutter插件中使用该依赖项.

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.

例如,这是我为解决同一问题所做的工作: 我的依赖关系是build.gradle

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)
      }
    }
}

然后只需运行以下命令即可将其发布到本地Maven存储库

Then just run following command to publish it to local maven repository

gradlew publishToMavenLocal

然后只需在flutter插件的存储库部分中添加mavenLocal()并将依赖项添加为

And then just add mavenLocal() in your flutter plugin's repositories section and add dependancy as

compile 'com.companyname.artifactname:libraryname:1.1@aar'

这篇关于如何在flutter插件中使用本地aar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆