(前略)
/** For apps only. Provides the flutter extension used in app/build.gradle. */classFlutterExtension {
/** Sets the compileSdkVersion used by default in Flutter app projects. */staticint compileSdkVersion = 33/** Sets the minSdkVersion used by default in Flutter app projects. */staticint minSdkVersion = 16/** Sets the targetSdkVersion used by default in Flutter app projects. */staticint targetSdkVersion = 33
(後略)
class_MyAppStateextendsState<MyApp> {
String _platformVersion = 'Unknown';
final _pluginTestPlugin = PluginTest();
@overridevoidinitState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.Future<void>initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.// We also handle the message potentially returning null.try {
platformVersion =
await _pluginTestPlugin.getPlatformVersion() ??'Unknown platform version';
} onPlatformException {
platformVersion = 'Failed to get platform version.';
}
(後略)
こんな感じに、アプリが起動するとプラットフォームのバージョン情報を各プラットフォーム側に問い合わせて取得します。で、上記では省略しましたが、取得した結果を表示するというアプリになってます。実際に実機をつないで Android Studio で実行させると、
publicclass PluginTestPlugin implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android////// This local reference serves to register the plugin with the Flutter Engine and unregister it/// when the Flutter Engine is detached from the Activityprivate MethodChannel channel;
@Overridepublicvoid onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "plugin_test");
channel.setMethodCallHandler(this);
}
@Overridepublicvoid onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else {
result.notImplemented();
}
}
(後略)
『Flutter』->『Open for Editing Android Studio』を選択します。次に、プロジェクトを開く際の確認ダイアログが出てくるので、
『New Window』を選択します。すると、 example/android にあるサンプルプロジェクトと一緒に、プラットフォーム側のプロジェクトファイルが、別ウィンドウで起動した Android Studio に表示されます(この時点で Android Studio が2つ立ち上がっているはずです)。
ここで、あとから起動した Android Studio 上でデバッグしたいプラットフォーム側のコードを開いて、
のようにブレークポイントを設定します。
次に、この Android Stduio 側で画面上部のツールバーの『Attach Debugger to Android Process』を選択します。
もちろん、メニューから
『Run』->『Attach Debugger to Android Process』と選択しても OK です。