官方文檔-> 點擊android
本文只針對Android客戶端集成操做,如下是親自試手,按照官方文檔有些小改動。git
已有項目相關環境:github
cd some/yourproject
複製代碼
flutter module
//my_flutter爲你的module名字
flutter create -t module my_flutter
複製代碼
cd .android/
./gradlew flutter:assembleDebug
複製代碼
這樣就生成了flutter-debug.aar(路徑:'.android/Flutter/build/outputs/aar/.')。 4. 確認你的宿主App的build.gradle
配置有:bash
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
複製代碼
settings.gradle
include ':app'
setBinding(new Binding([gradle:this]))
//YourProjecyName 爲你項目的文件名,按照官方文檔的寫法,會找不到對應的路徑,少了一個層級
evaluate(new File(
settingsDir.parentFile,
'YourProjecyName/my_flutter/.android/include_flutter.groovy'
))
複製代碼
build.gradle
文件中添加flutter的依賴。//官網的方式
dependencies{
implementation project(':flutter')
}
//上面的方式各類報錯,沒法找到對應的module
//換了種方式
dependencies{
implementation project(path: ':flutter')
}
複製代碼
終於整個項目跑通了,去app內的文件內,也能引入flutter相關的Api。大功告成。app
有兩種方式能夠將flutter的視圖添加到原生項目中:ide
如下是flutter入口函數main.dart
函數
import 'package:flutter/material.dart';
import 'dart:ui';
import 'package:bear_flutter/ui/WolfPage.dart';
void main() => runApp(_widgetForRoute(window.defaultRouteName));
Widget _widgetForRoute(String route){
switch(route){
case 'route1':
return WolfPage();
default:
return Center(
child: Text("haha"),
);
}
}
複製代碼
fragment
在原生Activity添加FlutterFragment
gradle
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_wolf_flutter)
//route1 爲flutter項目中的葉脈你路由名字
supportFragmentManager.beginTransaction().replace(R.id.container, Flutter.createFragment("route1"))
.commit()
}
複製代碼
//route1 爲flutter項目中的葉脈你路由名字
View flutterView = Flutter.createView(
MainActivity.this,
getLifecycle(),
"route1"
);
FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(600, 800);
layout.leftMargin = 100;
layout.topMargin = 200;
addContentView(flutterView, layout);
複製代碼