How to use local aar inside flutter plugin?

前端 未结 3 1194
再見小時候
再見小時候 2020-12-29 13:41

I\'ve create a flutter plugin with:

flutter create --template plugin flutter_plugin

I\'ve put my aar file inside flutter_plugin/android/src

相关标签:
3条回答
  • 2020-12-29 13:49

    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. If your aar dependancy is available on gradle, use gradle that in your build file. You can search for it here

    2. 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'
    
    0 讨论(0)
  • 2020-12-29 13:57

    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.

    0 讨论(0)
  • 2020-12-29 13:57

    I created a libs directory in the android directory of my plugin. I placed the aar I generated in the libs directory:

    I updated the build.gradle file for the android code to include the following:

    rootProject.allprojects {
        repositories {
            google()
            jcenter()
            flatDir {
                dirs 'libs'
            }
        }
    }
    

    And in the dependencies section:

    implementation fileTree(dir: 'libs', include: ['*.aar'])

    0 讨论(0)
提交回复
热议问题