The original link

tryenough.com/flutter02


Add Flutter to an existing Android project


Creating an Android project

If you already have an Android project, you can use it directly. Let’s start by creating an empty Android project to simulate an existing one, called TestOne

Create a Flutter module

Enter the same directory as your project, if your project is in… Path1 /path2/yourApp, then you should go to the path2 directory

$ cd. path1/path2/
$ flutter create -t module my_flutter
Copy the code

The command above creates a project module for FLUTTER, one of which is in the Flutter folder. Android hidden folder, which wraps an Android library engineering module.

You can try compiling the library with Gradle, but this is not a necessary step:

$ cd .android/
$ ./gradlew flutter:assembleDebug

Copy the code

Compiled in. Android/Flutter/build/outputs/aar/Flutter under path – the debug. Aar file.

The finished project catalog looks like this:

Add the Flutter module as a dependency to the main project

Open your Android project setting.gradle file and add the following code:

include ':app'                                     // assumed existing content
setBinding(new Binding([gradle: this]))                                 // new
evaluate(new File(                                                      // new
  settingsDir.parentFile,                                               // new
  'my_flutter/.android/include_flutter.groovy'                          // new
))                                                                      // new
Copy the code

After clicking sync, go to your build.gradle file in your app directory and add the dependencies:

dependencies {
  implementation project(':flutter')
}
Copy the code

Complete the synchronization again and add Flutter to your project. Now you can start mixed development.

Call the Flutter module directly in Java code


By using Flutter. CreateView, you can add a Flutter view directly to your Java code in one of two ways:

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = findViewById(R.id.open_text);
        
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                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); }}); }}Copy the code

Or method two:

FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.someContainer, Flutter.createFragment("route1"));
tx.commit();
Copy the code

Use the thermal overload method

Hot reloading refers to seeing the changes without rebooting, similar to the way you see them when you save them during Web page development. Enter the FLUTTER module and run the following command:

$ cd some/path/my_flutter
$ flutter attach
Waiting for a connection from Flutter on Nexus 5X...

Copy the code

Next, launch your app and enter the Page of Flutter. You should see the following message on the console:

🔥 To hot reload changes while running, press “r”. To hot restart (and rebuild state), press “R”. An Observatory debugger and profiler on Android SDK built for x86 is available at: http://127.0.0.1:61513/ For a more detailed help message, press “h”. To detach, press “D “; to quit, press “q”.

You can edit the Dart code in my_flutter and then enter R at the terminal to use hot overloading. You can also type the URL in your browser to view breakpoints, analyze memory, and perform other debugging tasks.

Follow us at TryEnough for more tutorials in this series