Android如何在原生App中嵌入Flutter
本文參考文檔Add Flutter to existing apps。
首先有一個可以運行的原生項目
第一步:新建Flutter moduleTerminal進入到項目根目錄,執(zhí)行flutter create -t module ‘module名字’例如:flutter create -t module flutter-native
執(zhí)行完畢,就會發(fā)現(xiàn)項目目錄下生成了一個module
進入到新生成的Flutter module目錄下的.android目錄下,命令是cd .android/,然后執(zhí)行g(shù)radlew flutter:assembleDebug,mac下./gradlew flutter:assembleDebug
這過程根據(jù)網(wǎng)絡(luò)情況,可能有點長。
結(jié)束之后在.android/Flutter/build/outputs/aar/目錄下會生成flutter-debug.aar
在app的build.gradle文件中加入:
compileOptions { sourceCompatibility 1.8 targetCompatibility 1.8 }
在settings.gradle中加入
include ’:app’setBinding(new Binding([gradle: this]))evaluate(new File( settingsDir.parentFile, ’FlutterNativeApplication/flutter_native/.android/include_flutter.groovy’))
注意:最后一個參數(shù)最好寫全路徑!
在app/build.gradle中
dependencies { …… implementation project(’:flutter’)}
到此準備過程結(jié)束,寫代碼測試一下,我使用的是Fragment方式。當然也有View的方式。
MainActivity.kt ↓
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) supportRequestWindowFeature(Window.FEATURE_NO_TITLE) setContentView(R.layout.activity_main) val tx = supportFragmentManager.beginTransaction() tx.replace(R.id.content, Flutter.createFragment('route')) tx.commit() }}
activity_main.xml ↓
<?xml version='1.0' encoding='utf-8'?><android.support.constraint.ConstraintLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' tools:context='.MainActivity'> <FrameLayout android: android:layout_width='match_parent' android:layout_height='match_parent'></FrameLayout></android.support.constraint.ConstraintLayout>
以上就是Android如何在原生App中嵌入Flutter的詳細內(nèi)容,更多關(guān)于Android 原生App中嵌入Flutter的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進)2. Python 實現(xiàn)勞拉游戲的實例代碼(四連環(huán)、重力四子棋)3. 一款功能強大的markdown編輯器tui.editor使用示例詳解4. java獲取文件編碼,jsoup獲取html純文本操作5. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程6. 利用CSS制作3D動畫7. python 寫函數(shù)在一定條件下需要調(diào)用自身時的寫法說明8. 淺談Android Studio導出javadoc文檔操作及問題的解決9. Java GZip 基于內(nèi)存實現(xiàn)壓縮和解壓的方法10. 存儲于xml中需要的HTML轉(zhuǎn)義代碼
