当前位置:   article > 正文

安卓Native调用Flutter页面

native 打开 flutter page

两种方法

  • 方法一:在已有的项目中集成Flutter
  • 方法二:将flutter工程导出为aar包提供给原项目调用(aar和引用工程统一debug或release版本)

参考资料传送门:

www.imuo.com/a/6773770e1… blog.csdn.net/Kinsomy/art…

事前准备

  • 一个可运行的flutter工程,下文统称为 your_flutter。
  1. 提供routes,your_flutter->main.dart
  1. void main() => runApp(new MyApp());
  2. class MyApp extends StatelessWidget {
  3. @override
  4. Widget build(BuildContext context) {
  5. return new MaterialApp(
  6. title: 'Flutter Demo',
  7. theme: new ThemeData(
  8. primarySwatch: Colors.blue,
  9. ),
  10. // 添加路由
  11. routes: <String, WidgetBuilder>{
  12. 'PAGE_A': (BuildContext context) => new MyHomePage(title: 'Welcome to PAGE_A'),
  13. 'PAGE_B': (BuildContext context) => new MyHomePage(title: 'Welcome to PAGE_B'),
  14. },
  15. home: new MyHomePage(title: 'Flutter Demo Home Page'),
  16. );
  17. }
  18. }
  19. // 需要自己实现 MyHomePage页面内容
  20. 复制代码
  1. 修改your_flutter->AndroidManifest.xml。(注释原来application节点,新建一个简单的)
  1. <!--<application-->
  2. <!--android:name="io.flutter.app.FlutterApplication"-->
  3. <!--android:label="your_flutter"-->
  4. <!--android:icon="@mipmap/ic_launcher">-->
  5. <!--<activity-->
  6. <!--android:name=".MainActivity"-->
  7. <!--android:launchMode="singleTop"-->
  8. <!--android:theme="@style/LaunchTheme"-->
  9. <!--android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"-->
  10. <!--android:hardwareAccelerated="true"-->
  11. <!--android:windowSoftInputMode="adjustResize">-->
  12. <!--&lt;!&ndash; This keeps the window background of the activity showing-->
  13. <!--until Flutter renders its first frame. It can be removed if-->
  14. <!--there is no splash screen (such as the default splash screen-->
  15. <!--defined in @style/LaunchTheme). &ndash;&gt;-->
  16. <!--<meta-data-->
  17. <!--android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"-->
  18. <!--android:value="true" />-->
  19. <!--&lt;!&ndash;<intent-filter>&ndash;&gt;-->
  20. <!--&lt;!&ndash;<action android:name="android.intent.action.MAIN"/>&ndash;&gt;-->
  21. <!--&lt;!&ndash;<category android:name="android.intent.category.LAUNCHER"/>&ndash;&gt;-->
  22. <!--&lt;!&ndash;</intent-filter>&ndash;&gt;-->
  23. <!--</activity>-->
  24. <!--</application>-->
  25. <application>
  26. <activity
  27. android:name=".MainActivity"
  28. android:launchMode="singleTop">
  29. <meta-data android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
  30. android:value="true" />
  31. </activity>
  32. </application>
  33. 复制代码
  1. 修改your_flutter->MainActivity。(最简版)
  1. public class MainActivity extends FlutterActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. // +添加 startInitialization调用
  5. io.flutter.view.FlutterMain.startInitialization(this.getApplicationContext());
  6. super.onCreate(savedInstanceState);
  7. GeneratedPluginRegistrant.registerWith(this);
  8. }
  9. // +提供一个供myApp工程调用的方法,此方法会跳转到your_flutter下的路由
  10. public static void start(Context context, String page) {
  11. Intent intent = new Intent(context, MainActivity.class);
  12. intent.setAction(Intent.ACTION_RUN);
  13. intent.putExtra("route", page);
  14. context.startActivity(intent);
  15. }
  16. 复制代码
  • 一个原生的安卓工程,下文统称为 myApp

方法一:在已有的项目中集成Flutter

  1. 修改your_flutter->build.gradle。(android.defaultConfig.minSdkVersion 与myApp工程保持一致)
  1. -apply plugin: 'com.android.application'
  2. +apply plugin: 'com.android.library'
  3. apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
  4. 复制代码
  1. 修改myAPP->setting.gradle 。
  1. -include ':app'
  2. +include ':app', ':your_flutter'
  3. +project(':your_flutter').projectDir = new File(settingsDir, 'your_flutter的路径/android/app')
  4. 复制代码
  1. 修改myAPP->local.properties。
  1. ndk.dir=/Users/andy/Desktop/NDK_ANT/android-ndk-r14b
  2. sdk.dir=/Users/andy/Library/Android/sdk
  3. +flutter.sdk=/Users/andy/flutter/flutter
  4. +flutter.buildMode=dehug
  5. +flutter.versionName=1.0.0
  6. +flutter.versionCode=1
  7. 复制代码
  1. 修改myAPP->build.gradle。
  1. dependencies {
  2. ...
  3. +implementation project(":your_flutter")
  4. }
  5. 复制代码
  1. 在myApp工程中提供一个按钮,调用flutter页面。
  1. flutter包名.MainActivity.start(this, "PAGE_A");
  2. 复制代码
  1. 到此完成第一种方法。

方法二:将flutter工程导出为aar包提供给原项目调用

  1. 制作aar,有坑,需要手动处理。(原因参考:blog.csdn.net/Kinsomy/art…

1.1 修改your_flutter->build.gradle,先使用application打出 apk包(android.defaultConfig.minSdkVersion 与myApp工程保持一致)

  1. ...
  2. +apply plugin: 'com.android.application'
  3. -apply plugin: 'com.android.library'
  4. apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
  5. ...
  6. 复制代码

1.2 打包apk

方法1:用Android Studio打apk。

方法2:在 your_flutter根目录下shell执行:flutter build apk --debug /flutter build apk --release

  1. 解压 apk,拷贝 /assets/flutter_shared/icudtl.dat文件到your_flutter/app/src/main/assets/flutter_shared目录。(如果用用Android Studio打开的your_flutter,有问题可以重新打开此工程试试)
  2. 修改your_flutter->build.gradle,使用library
  1. ...
  2. -apply plugin: 'com.android.application'
  3. +apply plugin: 'com.android.library'
  4. apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
  5. ...
  6. 复制代码
  1. 在 your_flutter根目录下shell执行:flutter build apk --debug /flutter build apk --release,会在 your_flutter/build/app/outputs/aar 文件夹下生成目标aar。
  2. 修改myAPP->setting.gradle 。
  1. +include ':app'
  2. -include ':app', ':your_flutter'
  3. -project(':your_flutter').projectDir = new File(settingsDir, 'your_flutter的路径/android/app')
  4. 复制代码
  1. 修改myAPP->local.properties。
  1. ndk.dir=/Users/andy/Desktop/NDK_ANT/android-ndk-r14b
  2. sdk.dir=/Users/andy/Library/Android/sdk
  3. -flutter.sdk=/Users/andy/flutter/flutter
  4. -flutter.buildMode=dehug
  5. -flutter.versionName=1.0.0
  6. -flutter.versionCode=1
  7. 复制代码
  1. 修改myAPP->build.gradle,使用aar。
  1. dependencies {
  2. ...
  3. -implementation project(":your_flutter")
  4. +compile(name: 'app-release', ext: 'aar')
  5. }
  6. 复制代码
  1. 在myApp工程中提供一个按钮,调用flutter页面。
  1. your_flutter包名.MainActivity.start(this, "PAGE_A");
  2. 复制代码
  1. 到此完成第二种方法。

转载于:https://juejin.im/post/5c1c46bee51d452c560f3b63

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/810298
推荐阅读
相关标签
  

闽ICP备14008679号