I encountered a problem when upgrading the Flutter SDK. I used to use 1.9.x (there is no problem below). Now I upgrade to v1.12.13 + hotfix.9 or 1.17.0 to reproduce this problem (I have only tried These two versions), the problem is as follows:
Now I use the v1.17.0 SDK to create a plug-in, and then use the reflection call method in the plug-in. The code is as follows:
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("personName")) { // Reflection call method
person.execute(call.method);
result.success("success");
}
}
The Person class as follows:
public class Person {
private static final String TAG = "Person";
// This method will be called by reflection
public void personName() {
Log.e(TAG, "personName: call personName method....");
}
public void execute(String methodName) {
try {
Method method = Person.class.getDeclaredMethod(methodName);
method.invoke(Person.this);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Part of Flutter's code is as follows
static Future<void> callMethod() async{
await _channel.invokeMethod("personName");
}
// The method is called when the button is clicked
await Flutterplugin17.callMethod();
Then if it is run directly (Click the debug button to run in Android Studio), it is OK, and the reflection method can be correctly called.
Using Android Studio to package Release APK (release) is also no problem.
Build -> Generate Signed Bundle or APK
But when I use the Flutter command to package the APK (release), the reflection method in the plugin cannot be called at this time. adb logcat is as follows:
Packaging commands : flutter build apk --release
There is also a situation where the above problem occurs. When another library is used in a plugin and this library is obfuscated, this time the above error will occur if the method in this obfuscated library is called.
flutter doctor:
I don’t know why this problem occurs when using the flutter command to package the release APK, thanks.
github repository: https://github.com/liusilong/flutter_plugin_17
I used the following three ways to package apk and then opened these apk in android studio for comparison:
- The flutter version is 1.17, packaged with Android studio (Build -> Generate Signed Bundle or APK), the dex file is parsed as follows:
- The flutter version is 1.17, packaged with Flutter commands(flutter build apk --release), the dex file is parsed as follows:
- The flutter version is 1.9.1+hotfix.6, packaged with Flutter commands(flutter build apk --release), the dex file is parsed as follows: