前言
在前面的章節(jié)學習中我們已經(jīng)掌握了從最基本的
hello flutter
到各種基本W(wǎng)idget扑眉、各種布局的使用再到多頁面切換路由的使用還有各種炫酷的提示跟dialog,還有關(guān)于網(wǎng)絡(luò)請求庫Dio的使用,至此我們完全可以使用flutter去開發(fā)一款獨立可運行的app了笨农,但是基于現(xiàn)階段flutter技術(shù)棧還不是太成熟荠医,純flutter項目上線風險還是比較大,所以跨平臺的混合開發(fā)模式自然還是現(xiàn)階段嘗試flutter的主流方式持隧,今天的分享我就跟大家一塊把我們寫好的flutter項目打包成aar文件嵌入到現(xiàn)有的Android項目中去即硼。
課程目標
- 掌握flutter打包成aar的整體流程
- 利用fat-aar把flutter項目中的三方依賴打入aar資源包中
項目準備:
flutter端項目我采用的是本專欄的實例代碼項目:https://github.com/xiedong11/flutter_app,Android端原生項目為新建的一個hello world項目屡拨,flutter端的相關(guān)配置我會上傳到github倉庫中只酥,原生Android項目比較簡單褥实,我只在本博客中貼出部分關(guān)鍵代碼
1.flutter項目打包成aar
flutter端項目工程目錄
上面截圖的flutter工程需要經(jīng)過我們的改造才能作為一個aar的形式存在,要被打包成aar的flutter端的項目是作為一個獨立的module運行在宿主app(原生Android)中裂允,所以我們需要修改兩個地方损离,讓我們打包出來的產(chǎn)物變成aar而不是獨立運行的apk。
1.1修改Android下的 build.gradle
// 1. 生成aar產(chǎn)物绝编,需要把`application`改為`library`
apply plugin: 'com.android.library'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 28
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// 2. flutter 作為寄存于其他app中的產(chǎn)物僻澎,所以不應(yīng)該存在applicationId,所以注釋掉該行.
//applicationId "com.zhuandian.flutterapp"
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
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
}
}
}
1. 2 修改Androidmanifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zhuandian.flutterapp">
<uses-permission android:name="android.permission.INTERNET" />
<!--1.項目作為子項目寄存于原生app中十饥,不需要icon窟勃、label等屬性,這里直接省去各種配置即可-->
<application>
<!--android:name="io.flutter.app.FlutterApplication"-->
<!--android:icon="@mipmap/ic_launcher"-->
<!--android:label="flutter_app">-->
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:hardwareAccelerated="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:windowSoftInputMode="adjustResize">
<!--2.項目作為子項目寄存于原生app中绷跑,入口acitvity不需要配置LAUNCHER拳恋,不然應(yīng)用集成到宿主app中,啟動會在桌面上生成兩個應(yīng)用圖標-->
<!--<meta-data-->
<!--android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"-->
<!--android:value="true" />-->
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN" />-->
<!--<category android:name="android.intent.category.LAUNCHER" />-->
<!--</intent-filter>-->
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
需要修改的地方砸捏,我都在源碼里留注釋了谬运,這里就不展開贅述了,下面我們來通過gradle獲得編譯好的aar
產(chǎn)物
因為在build.gradle 中垦藏,我們把apply plugin: 'com.android.application'
修改成了梆暖,apply plugin: 'com.android.library'
所以,現(xiàn)在通過Terminal中我執(zhí)行gradlew assembleRelease
編譯出的產(chǎn)物會有原來的apk變成aar文件掂骏,文件輸出目錄為項目根目錄下的/bulid/app/outputs/aar
如下圖所示:
1.3 Android端項目配置接入aar依賴
1.3.1 新建原生Android項目轰驳,我上述打包產(chǎn)出的aar文件作為依賴放入libs文件夾
1.3.2 修改dependencies
節(jié)點下的fileTree
依賴配置,支持引入aar依賴支持
implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
...
}
1.3.3 在原生Android項目中寫一個簡單的按鈕測試flutter項目
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//初始化flutter運行環(huán)境
FlutterMain.startInitialization(this)
tv_test_aar.setOnClickListener {
startActivity(Intent(this, com.zhuandian.flutterapp.MainActivity::class.java))
}
}
}
至此弟灼,把flutter項目打包成aar導入到原生Android項目中的所有工程配置已經(jīng)結(jié)束了级解,讀者可以測試上上述整個過程;但有時候我們的flutter項目并不是單純的flutter官方代碼田绑,開發(fā)過程中少不了引入一些三方依賴勤哗,像上節(jié)課我們講到的Dio網(wǎng)絡(luò)請求庫,或者是通過
pubspec.yaml
引入的其他開源工具類掩驱,這種情況下芒划,通過上邊的配置方式,你會發(fā)現(xiàn)第三方的依賴代碼是打不進aar包里的欧穴,下面我們就講解一下借助fat-aar
的形式把三方依賴代碼打入aar包中去
2.flutter項目中存在三方依賴
通過上邊的配置民逼,我們只能把純flutter項目打包成aar文件,換句話說涮帘,如果我們的flutter項目存在三方依賴是不能正常打包進flutter產(chǎn)物里的拼苍,這個時候我們就需要通過在Android原生項目中引入fat-aar
配置,確保把flutter項目中的三方依賴正常打包進去flutter產(chǎn)物中去调缨。
2.1修改項目工程目錄的build.gradle文件映屋,引入fat-aar支持
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
//引入fat-aar支持
classpath 'com.kezong:fat-aar:1.1.7'
}
}
2.2 在上邊第一部分配置app文件下build.gradle基礎(chǔ)上苟鸯,增加fat-aar相關(guān)配置,這里為了切換aar library運行環(huán)境棚点,我引入isAarLibrary標志位作為切換環(huán)境開關(guān),方便工程配置湾蔓,具體代碼如下:
//是否作為依賴
boolean isAarLibrary = true
if (isAarLibrary) {
apply plugin: 'com.android.library'
} else {
apply plugin: 'com.android.application'
}
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
if (isAarLibrary) {
//引入fat-aar
apply plugin: 'com.kezong.fat-aar'
}
android {
compileSdkVersion 28
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
if (!isAarLibrary) {
applicationId "com.zhuandian.flutterapp"
}
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
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 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation 'androidx.appcompat:appcompat:1.0.0'
if (isAarLibrary) {
//TODO 添加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, _ ->
println name
embed project(path: ":$name", configuration: 'default')
}
}
}
2.3 AndroidManifest.xml清單文件中不需要增加額外配置瘫析,原生Android端喚起flutter項目aar包的方式也不需要修改,整個引入fat-aar的過程只是為了確保能把flutter項目中的三方依賴代碼打入到flutter產(chǎn)物中去默责,所以關(guān)于flutter打包成aar的操作跟第一步保持一致就行贬循,第二步的配置,只是為了確保flutter項目中的三方依賴能正常打包進flutter產(chǎn)物中去
效果圖是我把本專欄的相關(guān)代碼作為一個aar集成到一個新建的原生Android項目中桃序,效果圖如下:
項目的完整代碼配置在https://github.com/xiedong11/flutter_app 中杖虾,讀者可以參考具體配置細節(jié),筆者在寫本篇博文打包aar時的flutter 環(huán)境stable版本為 flutter v1.9.1
媒熊,讀者盡量用官方穩(wěn)定版的代碼做測試奇适。