Flutter打包AAR插件之fat-aar使用教程
Google推荐的打包AAR方式
在开始讲解fat-aar插件打包之前,先讲讲Google推荐的打包流程:
-
创建一个 flutter module 工程,创建方式有两种
- 命令行方式创建:
flutter create -t module --org com.example my_flutter
- Android Studio创建
- 命令行方式创建:
-
执行打包命令
flutter build aar
,这个打包AAR方式会包含多种abi,如果想要指定abi可以使用flutter build aar --target-platform xxx平台
-
打包命令执行没有报错的情况下,控制台会有最终打包aar文件的存放地址和如何使用集成到你应经存在的android项目中的提示说明.例如如下提示

在已经存在的Android项目中嵌入Flutter工程,还有一种源码的方式,这里就不在讲解,如果你的英文水平比较好可以直接看官网的文档就可以了,这里讲的很清楚了.
fat-aar打包Flutter工程 (开源的Android打包AAR插件)
看标题我说fat-aar插件是Android打包AAR插件,fat-aar是android工程中打包AAR的一个插件.那个和Flutter有什么关系呢?本质上说:上面讲的Google推荐的打包AAR 和fat-aar插件打包AAR的原理是类似的.他们都是打包Android工程为一个AAR文件
工程模式不同
- Google打包的工程是: flutter-module
- fat-aar打包的是一个纯flutter工程
也就是通过这方式创建的flutter工程.
正事开始之前讲讲flutter项目目录结构

开始集成fat-aar
- 修改.android/build.gradle文件
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
//增加这个配置
classpath "com.kezong:fat-aar:1.2.15"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2.修改.android/app/build.gradle
//新增: 是否作为依赖
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 plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
//新增:引入fat-aar
if (isAarLibrary) {
apply plugin: 'com.kezong.fat-aar'
}
android {
compileSdkVersion 28
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
//新增:需要打包aar时候,不能有applicationId
if (!isAarLibrary) {
applicationId "com.flutter.flutter_aar_demo"
}
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
lintOptions {
disable 'InvalidPackage'
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
//新增:将libflutter.so 和 flutter_embedding.jar ,同时和第三方插件打包到aar中
if (isAarLibrary) {
// 添加 flutter_embedding.jar release 注释①
embed "io.flutter:flutter_embedding_release:1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7"
// 添加各个 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:x86_64_debug:1.0.0-eed171ff3538aa44f061f3768eec3a5908e8e852"
// embed "io.flutter:x86_debug:1.0.0-eed171ff3538aa44f061f3768eec3a5908e8e852"
embed "io.flutter:arm64_v8a_release:1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7"
embed "io.flutter:armeabi_v7a_release:1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7"
//添加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')
}
}
}
注释①:如何查找最新flutter_embedding.jar文件的最新版本,也就是io.flutter:flutter_embedding_release:1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7,release: 后面的版本号.最新版本号是默认保存在这里了,例如我window下的是(mac也是相同的.gradle/xxxx)

.gradle\caches\modules-2\files-2.1\io.flutter 注释②:同样flutter.so也是保存在.gradle\caches\modules-2\files-2.1\io.flutter 文件夹下
修改.android/app/AndroidManifest.xml 文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.flutter.flutter_aar_demo"
xmlns:tools="http://schemas.android.com/tools">
//只保留application标签
<application>
<!--从这里开始注释-->
<!-- android:name="io.flutter.app.FlutterApplication"
android:label="flutter_aar_demo"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!– Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. –>
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<!– Displays an Android View that continues showing the launch screen
Drawable until Flutter paints its first frame, then this splash
screen fades out. A splash screen is useful to avoid any visual
gap between the end of Android's launch screen and the painting of
Flutter's first frame. –>
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>-->
<!--到这里注释结束-->
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
开始编译打包
在Android项目下执行 gradlew assembleRelease
,这是一个比较耗时的任务,静静的等待即可.
获取aar文件.
编译完成后的文件保存在了你的flutter项目目录下的build/app/outputs/arr/app-release.aar
,获取到这个.aar文件后就可以直接扔给你的Android开发同学使用了.注意需要你的Android项目的开发同学开启支持java8
fat-aar 和Google推荐的相同优缺点
优点:
- Android开发的同学不要配置flutter环境
- fat-aar:将flutter所有的需要的包全部打包大一个aar文件下了.包括flutter项目下的第三方插件.
缺点:
- 他们相同的缺点是相对于源码集成的方式缺少调试flutter代码的功能.
最后
有写的不对的地方,不吝赐教
转载自:https://juejin.cn/post/6850037282884452360