logo头像
Snippet 博客主题

android Flutter 10.Native 项目中添加Flutter

本文于375天之前发表,文中内容可能已经过时。

Flutter android 项目中添加Flutter

官方文档

官方 给出 是不建议在原项目中集成 flutter

目前还是测试 实验阶段

已有android 项目中集成 Flutter

The Flutter project template

使用flutter create xxx创建的Flutter项目包括用于Flutter / Dart代码的非常简单的 应用程序(Android单个Activity  和 iOS 单个 ViewController )。您可以修改这些主机应用程序以满足您的需求并从那里构建。

The Flutter module template

但是,如果您开始使用任一平台的现有应用程序,您可能希望将Flutter项目作为某种形式的库包含在该应用程序中。

这就是Flutter模块模板提供的内容。执行 flutter create -t​​ module xxx会生成一个Flutter项目,其中包含一个Android库和一个Cocoapods pod,专为现有主机应用程序使用而设计。

android 项目中集成 Flutter

Create a Flutter module

命令行 创建

flutter create -t module flutter_mode

也可以使用android Studio 创建

flutter_mode 应用转化为库

修改flutter_mode 中 android/app/build.gradle

1
2
3
apply plugin: 'com.android.application'
修改为
apply plugin: 'com.android.library'

删除 defaultConfig 中 applicationId

1
2
3
4
5
6
7
8
9
    defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
// applicationId "com.example.fluttermode"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

删除 flutter_mode 中 AndroidManifest.xml

删除 flutter_mode 中 android/app/src/main/AndroidManifest.xml
中 多余的代码 intent-filter application 属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 <application >
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in @style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />

</activity>
</application>

修改 MainActivity.java

1
2
3
4
5
6
7
8
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
io.flutter.view.FlutterMain.startInitialization(this.getApplicationContext()); // <= New.
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}
}

android Native 项目 引用

修改 Native 工程目录中 settings.gradle 文件

1
2
include ':flutter_mode'
project(':flutter_mode').projectDir = new File( 'flutter_mode/flutter_mode/android/app')

修改 Native 项目目录中 build.gradle 文件

1
implementation project(":flutter_mode")

修改 Native 工程项目中 local.properties 文件

1
2
3
ndk.dir=/Users/kylinhuang/androidStudioSDK/ndk-bundle
sdk.dir=/Users/kylinhuang/androidStudioSDK
flutter.sdk=/Users/kylinhuang/software/flutter

调用

1
2
3
4
5
6
7
8
9
public void startFlutterActivity(View view) {

// ComponentName componentName = new ComponentName(this, "com.example.fluttermode.MainActivity");
// Intent intent = new Intent().setComponent(componentName);
// startActivity(intent);

Intent intent = new Intent(getActivity(), com.example.fluttermode.MainActivity.class);
startActivity(intent);
}

启动过程有点慢
先白屏 再渲染出来

使用release 版本 运行起来比较快

1
2
3
4
5
6
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.sengled.Snap-1/base.apk"],nativeLibraryDirectories=[/data/app/com.sengled.Snap-1/lib/arm, /data/app/com.sengled.Snap-1/base.apk!/lib/armeabi, /system/lib, /vendor/lib]]] couldn't find "libflutter.so"
at java.lang.Runtime.loadLibrary0(Runtime.java:984)
at java.lang.System.loadLibrary(System.java:1530)
at io.flutter.view.FlutterMain.startInitialization(FlutterMain.java:163)
at io.flutter.view.FlutterMain.startInitialization(FlutterMain.java:140)
at com.example.fluttermode.MainActivity.onCreate(MainActivity.java:10)
1
2
3
No application found for TargetPlatform.android_arm.
Is your project missing an android/app/src/main/AndroidManifest.xml?
Consider running "flutter create ." to create one.

第一次遇到 更新 flutter 就可以了
又遇见了 怎么都不好使 无奈之下 删除 android 目录 ios目录

直接运行

flutter create .

再git 恢复配置问题

支付宝打赏 微信打赏

打赏