安卓已有项目集成Flutter

1.创建一个目录,把已有的安卓项目复制到里面

2.用命令行,cd进来这个目录,flutter create -t module my_flutter,就会创建一个my_flutter的工程

如下图所示



3.cd /Users/mac/Desktop/Flutter_Mix_Android 

cd my_flutter 

cd .android 

./gradlew flutter:assembleDebug

准备等待很久...等它处理完毕


4.打开setting.gradle 文件,加入如下代码

include ':app' // 注意,一定要是app,不能改为其它的,之前项目里面的app目录名字改了之后,一直报错

setBinding(new Binding([gradle: this]))                                // new

evaluate(new File(                                                      // new

  settingsDir.parentFile,                                              // new

  'my_flutter/.android/include_flutter.groovy'                          // new

))

提示:

5.app/build.gradle 增加对flutter 工程依赖

dependencies {

  implementation project(':flutter')

}

6.app的AndroidManifest.xml文件中增加以下代码:

android:name="io.flutter.embedding.android.FlutterActivity"

android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"

android:hardwareAccelerated="true"

android:windowSoftInputMode="adjustResize"

/>

7.不指定页面(黑屏延迟很短,可以使用)

public class NativeApplication extends Application {

    FlutterEngine flutterEngine;

    @Override

    public void onCreate() {

        super.onCreate();

        flutterEngine = new FlutterEngine(this);

        flutterEngine.getDartExecutor().executeDartEntrypoint(

                DartExecutor.DartEntrypoint.createDefault()

        );

        FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine);

    }

}

//页面跳转

 Intent intent= FlutterActivity.withCachedEngine("my_engine_id").build(this);

startActivity(intent);

8.指定页面(黑屏延迟很短,可以使用)

public class NativeApplication extends Application {

    FlutterEngine flutterEngine;

    @Override

    public void onCreate() {

        super.onCreate();

        flutterEngine = new FlutterEngine(this);

        flutterEngine.getNavigationChannel().setInitialRoute("your/route/here");

        flutterEngine.getDartExecutor().executeDartEntrypoint(

                DartExecutor.DartEntrypoint.createDefault()

        );

        FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine);

    }

}

//页面跳转

 Intent intent= FlutterActivity.withCachedEngine("my_engine_id").build(this);

startActivity(intent);


参考链接:

1.https://blog.csdn.net/LosingCarryJie/article/details/105950621

2.https://juejin.im/post/6844903705955794958

你可能感兴趣的:(安卓已有项目集成Flutter)