在開始講解fat-aar插件打包以前,先講講Google推薦的打包流程:java
建立一個 flutter module 工程,建立方式有兩種android
- 命令行方式建立:
flutter create -t module --org com.example my_flutter
- Android Studio建立
執行打包命令 flutter build aar
,這個打包AAR方式會包含多種abi,若是想要指定abi可使用 flutter build aar --target-platform xxx平臺
bash
打包命令執行沒有報錯的狀況下,控制檯會有最終打包aar文件的存放地址和如何使用集成到你應經存在的android項目中的提示說明.例如以下提示markdown
在已經存在的Android項目中嵌入Flutter工程,還有一種源碼的方式,這裏就不在講解,若是你的英文水平比較好能夠直接看官網的文檔就能夠了,這裏講的很清楚了.app
看標題我說fat-aar插件是Android打包AAR插件,fat-aar是android工程中打包AAR的一個插件.那個和Flutter有什麼關係呢?本質上說:上面講的Google推薦的打包AAR 和fat-aar插件打包AAR的原理是相似的.他們都是打包Android工程爲一個AAR文件oop
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.gradlegradle
//新增: 是否做爲依賴 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)ui
.gradle\caches\modules-2\files-2.1\io.flutter 註釋②:一樣flutter.so也是保存在.gradle\caches\modules-2\files-2.1\io.flutter 文件夾下this
<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
,這是一個比較耗時的任務,靜靜的等待便可.google
編譯完成後的文件保存在了你的flutter項目目錄下的build/app/outputs/arr/app-release.aar
,獲取到這個.aar文件後就能夠直接扔給你的Android開發同窗使用了.注意須要你的Android項目的開發同窗開啓支持java8
優勢:
缺點:
有寫的不對的地方,不吝賜教