After the FlutterSDK upgrade, we found that we could not integrate Flutter into existing Android projects in the same way as before because there was no Flutter class. I found a new way to use Flutter by reading the source code.
Create a Flutter Module
To integrate flutter into existing Android projects, you can use AndroidStudio’s New Flutter Module for quick and easy implementation.
setBinding(new Binding([gradle:this]))
evaluate(
new File(
settingsDir,
'flutter_module/.android/include_flutter.groovy'
)
)
include ':flutter_module'
Copy the code
Finally we just need to reference the Flutter module in our app build.gradle
implementation project(path: ':flutter')
Copy the code
2. Use Flutter
Two ways to use FlutterView and FlutterFragment.
It is first initialized in Application
public class MVApplication extends Application {
private static MVApplication instance;
public static MVApplication getInstance(a){
return instance;
}
@Override
public void onCreate(a) {
super.onCreate();
FlutterMain.Settings settings=new FlutterMain.Settings();
settings.setLogTag("MyFlutter");
FlutterMain.startInitialization(this,settings);
String[] args = {"info"."data"};
FlutterMain.ensureInitializationComplete(this,args);
instance=this; }}Copy the code
1. Use FlutterFragment
The new version of FlutterSDK no longer supports the Flutter class and uses of Flutter.createView(), Flutter.createFragment(), etc. The code is as follows:
public class MyFlutterActivity extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flutter_layout);
//FlutterFragment fragment=new FlutterFragment();
FlutterFragment fragment=FlutterFragment.withNewEngine().initialRoute("home").build(); getSupportFragmentManager().beginTransaction().add(R.id.flutter_container, fragment).commit(); }}Copy the code
FlutterFragment creation in code can also use the default constructor directly by loading the default route in main.dart, “/”, The sample code given us by calling FlutterFragment. WithNewEngine () initialRoute (” home “). The build () to create a new NewEngineFragmentBuilder FlutterFr to construct the designated route Agment.
2. Use FlutterView
FlutterView creation can not use the old version of the method, the new version of the use of the code is as follows:
public class MyFlutterActivity extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flutter_layout);
FrameLayout frameLayout=findViewById(R.id.flutter_container);
ProgressBar progress=findViewById(R.id.progress);
/ / create FlutterView
FlutterView flutterView=new FlutterView(this);
// Create FlutterView first frame render complete listener
flutterView.addFirstFrameListener(new FlutterView.FirstFrameListener() {
@Override
public void onFirstFrame(a) {
// Hide the progress bar to display the FlutterViewprogress.setVisibility(View.GONE); frameLayout.setVisibility(View.VISIBLE); }});// Create the DART code executor
DartExecutor executor=flutterView.getDartExecutor();
Dart executes the main function in main.dart
executor.executeDartEntrypoint(
new DartExecutor.DartEntrypoint(FlutterMain.findAppBundlePath(),
"main"));
// Add FlutterView to the layout
ViewGroup.LayoutParams layoutParams=newLinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT); frameLayout.addView(flutterView); }}Copy the code
The layout file is as follows:
<?xml version="1.0" encoding="utf-8"? >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/flutter_container"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ProgressBar
android:id="@+id/progress"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:visibility="visible"
>
</ProgressBar>
</RelativeLayout>
Copy the code
The effect is shown below: