Flutter Module 打包aar

Flutter Module 打包aar

Flutter 版本1.17.1

Google 方式打包aar

flutter build aar

android {
  // ...
}

repositories {
  maven {
    url 'some/path/my_flutter/build/host/outputs/repo'
    // This is relative to the location of the build.gradle file
    // if using a relative path.
  }
  maven {
    url 'https://storage.googleapis.com/download.flutter.io'
  }
}

dependencies {
  // ...
  debugImplementation 'com.example.flutter_module:flutter_debug:1.0'
  profileImplementation 'com.example.flutter_module:flutter_profile:1.0'
  releaseImplementation 'com.example.flutter_module:flutter_release:1.0'
}

这种方式打出来的aar包并没有把第三方插件打入aar中,只是将第三方插件依赖到本地的仓库,所以不太适用共同开发

Fat-aar 插件打包

使用Fat-aar插件可以将第三方的插件和flutter 相关的东西合并到一个aar中

集成fat-aar
  • 根目录gradle添加依赖

    dependencies {
        classpath "com.kezong:fat-aar:1.2.15"
    }
    
  • app目录下配置

    //打包时设置为Library
    boolean isAarLibrary = true
    
    def localProperties = new Properties()
    def localPropertiesFile = rootProject.file('local.properties')
    if (localPropertiesFile.exists()) {
        localPropertiesFile.withReader('UTF-8') { reader ->
            localProperties.load(reader)
        }
    }
    
    def flutterRoot = localProperties.getProperty('flutter.sdk')
    if (flutterRoot == null) {
        throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
    }
    
    def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
    if (flutterVersionCode == null) {
        flutterVersionCode = '1'
    }
    
    def flutterVersionName = localProperties.getProperty('flutter.versionName')
    if (flutterVersionName == null) {
        flutterVersionName = '1.0'
    }
    //新增:当需要打包aar 时,修改项目为library模式
    if (isAarLibrary) {
        apply plugin: 'com.android.library'
    } else {
        apply plugin: 'com.android.application'
    }
    apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
    
    //新增:引入fat-aar
    if (isAarLibrary) {
        apply plugin: 'com.kezong.fat-aar'
    }
    
    
    
    defaultConfig {
            // 注意Library方式不需要applicationId
            if (!isAarLibrary) {
                applicationId "com.ecook.zuofan_flutter"
            }
            minSdkVersion 16
            targetSdkVersion 28
            versionCode flutterVersionCode.toInteger()
            versionName flutterVersionName
        }
    
    
    

  • 使用fat-aar

    注意: io.flutter:flutter_embedding_release 版本号可以从本地 .gradle/caches/modules-2/files-2.1/io.flutter 目录下获取

    dependencies {
        if (isAarLibrary) {
                embed "io.flutter:flutter_embedding_release:1.0.0-6bc433c6b6b5b98dcf4cc11aff31cdee90849f32"
            // 添加各个 cpu 版本 flutter.so
    //        embed "io.flutter:arm64_v8a_debug:1.0.0-eed171ff3538aa44f061f3768eec3a5908e8e852"
    //        embed "io.flutter:armeabi_v7a_debug:1.0.0-eed171ff3538aa44f061f3768eec3a5908e8e852"
            embed "io.flutter:arm64_v8a_release:1.0.0-6bc433c6b6b5b98dcf4cc11aff31cdee90849f32"
            embed "io.flutter:armeabi_v7a_release:1.0.0-6bc433c6b6b5b98dcf4cc11aff31cdee90849f32"
    
            //添加fat-aar处理flutter打包成aar中三方依赖
            def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()
            def plugins = new Properties()
            def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
            if (pluginsFile.exists()) {
                pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
            }
            plugins.each { name, _ ->
    
                embed project(path: ":$name", configuration: 'default')
    
            }
        }
    }
    
  • 修改.android/app/AndroidManifest.xml 文件

    我们的项目时Flutter项目 所以打包成aar 需要注释掉无用的标签 只保留application标签

    
      //只保留application标签
        
      //这个需要保留
       
      
    
    
  • 编译打包

    在 android 目录下面打包 执行 gradlew assembleRelease

    等待即可获取aar

你可能感兴趣的:(Flutter Module 打包aar)