• The Flutter version stable 1.17.5
  1. Create a folder HotFlutter and create a new flutter project under this folder:

    flutter create HotFlutter

Because the android terminal of the Flutter project uses KT by default, I am quite familiar with Java, so I first delete the Android folder under the project and regenerate the Android code of Java

flutter create -a java . 
Copy the code

The prep work is done!

2. Create a new class MyFlutterActivity under Android -> app -> SRC -> main-> Java -> com.example.HotFlutter.

3. MyFlutterActivity inherits FlutterActivity and overwrites getFlutterShellArgs() to configure the file we need to hot update, with comments, and directly paste the code:

package com.example.HotFlutter; import android.Manifest; import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager; import android.os.Build; import android.os.Bundle; import android.os.Environment; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import io.flutter.embedding.android.FlutterActivity; import io.flutter.embedding.engine.FlutterShellArgs; public class MyFlutterActivity extends FlutterActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); If (build.version.sdk_int >= build.version_codes.m) {if (ContextCompat.checkSelfPermission(MyFlutterActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ! = PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MyFlutterActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } if (ContextCompat.checkSelfPermission(MyFlutterActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) ! = PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MyFlutterActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); } } } @Override public FlutterShellArgs getFlutterShellArgs() { copyLibAndWrite(this, "hotlibapp.so"); FlutterShellArgs supFA = super.getFlutterShellArgs(); File dir = this.getDir("libs", Activity.MODE_PRIVATE); String libPath = dir.getAbsolutePath() + File.separator + "hotlibapp.so"; File libFile = new File(libPath); if (libFile.exists()) { supFA.add("--aot-shared-library-name=" + libPath); } return supFA; // If hotlibapp is available, configure it. } // Function: Public static void copyLibAndWrite(Context Context) public static void copyLibAndWrite(Context Context) public static void copyLibAndWrite(Context Context) String fileName) { try { String path = Environment.getExternalStorageDirectory().toString(); File destFile2 = new File(path + "/" + fileName); if (destFile2.exists()) { File dir = context.getDir("libs", Activity.MODE_PRIVATE); File destFile = new File(dir.getAbsolutePath() + File.separator + fileName); if (destFile.exists()) { destFile.delete(); } destFile.createNewFile(); FileInputStream is = new FileInputStream(destFile2); FileOutputStream fos = new FileOutputStream(destFile); byte[] buffer = new byte[is.available()]; int byteCount; while ((byteCount = is.read(buffer)) ! = -1) { fos.write(buffer, 0, byteCount); } fos.flush(); is.close(); fos.close(); destFile2.delete(); } catch (IOException e) {}}}Copy the code

Copy the original copy into the line, it is recommended to use as to open, to avoid guide package error, the code does not change.

4. Open the MainActivity class and inherit MyFlutterActivity

package com.example.HotFlutter;
public class MainActivity extends MyFlutterActivity {
}
Copy the code

5. Add it in AndroidManifest

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Copy the code

And register our own classes

<activity android:name=".MyFlutterActivity"></activity>
Copy the code

6. Dart has been simplified to show the effect:

import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Scaffold ', home: Scaffold(body: Center(child: Text(" initialization project ")),); }}Copy the code

Packaging:

flutter build apk --target-platform android-arm  --split-per-abi
Copy the code

Operation effect:

Change Text(” initialize project “) to Text(” hot update “), repackage a copy, and unzip:

Rename app-armeabi-v7a-release\lib\armeabi-v7a\libapp.so to hotlibapp.so and put it in the root directory of your phone

Restart the app and find that the home page has become hot update! The files in the directory will also be deleted, and the new package will be used after the restart

Done!!

First time to write an article, reprint please indicate the source. Attach github address gitee.com/yangruishan…