Study/Dart,Flutter

12. Flutter[플러터] 안드로이드 화면띄우기 method channel invokeMethod(1)

코딩 잘 할거얌:) 2022. 6. 18. 19:57
반응형

이번 포스팅에서는 Flutter에서 Android(Java)로 데이터 받기를 포스팅해보겠다. 

현재 배터리상태를 가져오는 것은 공식문서에도 잘 되어있으므로, 그대로 따라 하시면 된다. 그래도 내가 삽질하면서 알게 된 정보들을 적어보겠다.

 

플러터야 받아라(안드로이드)

 

얼마 전 플러터를 개발하면서 플러터를 지원해주지 않는 API들을 보며, '과연 플러터로만 모든걸 개발을 할 수 있을까?'라는 생각이 들었다. 그래서 결국은 네이티브와 연동을 할 수밖에 없다는 결론을 내렸다. 심지어 플러터에서 사용하는 라이브러리들, 특히 핸드폰 하드웨어적인 부분은 네이티브와 연동이 굉장히 많이 되어있다.

 

하지만 내가 연동을 하기전 궁금한 게 몇 가지 있었다.

 

  • 네이티브와 연동을 할 때 안드로이드와 IoS에 나누어진 언어들 모두 지원하는가?
  • 네이티브와 연동을 하는 과정은 어떻게 되는가?

이 두가지였다 물론 첫 번째는 당연하게도 모두 지원하고 있었다. 안드로이드의 코틀린과 자바, IoS에서 오브젝트-c와 스위프트 모두 가능하다. 

 

두 번째가 정말 궁금해서 알아보았다.

 

https://www.raywenderlich.com/30342553-platform-specific-code-with-flutter-method-channel-getting-started

 

Platform-Specific Code With Flutter Method Channel: Getting Started

Learn how to communicate with some platform-specific code with Flutter method channels and extend the functionality of the Flutter application.

www.raywenderlich.com

https://www.alibabacloud.com/blog/flutter-analysis-and-practice-native-capability-based-plug-in-extension_596578

 

Flutter Analysis and Practice: Native Capability-Based Plug-In Extension

This article introduces the Flutter plug-ins and how they work, explains the platform channels that the plug-ins depend on.

www.alibabacloud.com

 

플러터와 네이티브 앱 간의 데이터는 메서드 채널(MethodChannel)을 통해서 데이터 교환이 이루어진다. 이 메서드 채널은 데이터 메시지를 바이너리 데이터 직렬화(Binary Data Serialized)를 통해서 JSON형태로 보낸다. 그리고 화면이 보이는 것은 Android는 FlutterActivity를 이용해서, iOS경우는 FlutterAppDelegate를 이용해서 NativeAPI에 접근한다. FlutterActivity는 안드로이드 플러그인 매니저이고 FlutterView를 바인딩하는 역할을 한다. FlutterAppDelegate도 iOS에서 비슷한 역할을 한다.

 

만약 좀 더 자세한 내용을 알고 싶다면 위의 두 링크를 본다면 아주 자세하게 알 수 있을 것이다.  이 글도 안 봐도 될 정도로...

 


Flutter 공식 링크

 

https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects

 

GitHub - flutter/flutter: Flutter makes it easy and fast to build beautiful apps for mobile and beyond

Flutter makes it easy and fast to build beautiful apps for mobile and beyond - GitHub - flutter/flutter: Flutter makes it easy and fast to build beautiful apps for mobile and beyond

github.com

기존 공식홈페이지에서 제공하는 invokemethod에 관련된 내용은 아래 링크에 있다. 하지만 내가 진행했을 당시 (22년 4월)기준으로는 오류가 많이 발생했고 컴파일이 안 되었다. 많은 구글링 후 Github에 있는 Flutter Wiki에서 업데이트 내용이 있어서 적용하니 잘 되었다. 혹시 공식 홈페이지에서도 안 되는 경우에는 위의 링크를 참고하면 좋다.

 

https://flutter-ko.dev/docs/development/platform-integration/platform-channels

 

 

플랫폼 별 코드 작성

앱에서 커스텀하게 플랫폼 별 코드를 작성하는 방법을 배워보세요.

flutter-ko.dev

https://api.flutter.dev/flutter/services/MethodChannel/invokeMethod.html

 

invokeMethod method - MethodChannel class - services library - Dart API

Future invokeMethod (String method, [dynamic arguments] ) Invokes a method on this channel with the specified arguments. The static type of arguments is dynamic, but only values supported by the codec of this channel can be used. The same applies to the re

api.flutter.dev

https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects

 

GitHub - flutter/flutter: Flutter makes it easy and fast to build beautiful apps for mobile and beyond

Flutter makes it easy and fast to build beautiful apps for mobile and beyond - GitHub - flutter/flutter: Flutter makes it easy and fast to build beautiful apps for mobile and beyond

github.com


버전 정보

 

Flutter 3.0.1 • channel stable • https://github.com/flutter/flutter.git  
Framework • revision fb57da5f94 (2 weeks ago) • 2022-05-19 15:50:29 -0700
Engine • revision caaafc5604
Tools • Dart 2.17.1 • DevTools 2.12.2

 


우선 플러터 새로운 프로젝트를 만든다면 kotlin으로 만들어질 것이다. 나도 여기서부터 삽질을 했으니까 여기서부터 어떻게 하는지 포스팅하겠다.

 

 

 

 

 

Kotlin to Java

 

우선 플러터 새로은 프로젝트를 만든다면 kotlin으로 만들어질것이다. 나도 여기서부터 삽질을 했으니까 여기서부터 어떻게하는지 포스팅하겠다.

flutter create -a java .

-a java : 안드로이드를 자바로 만들겠다.

. : 현재 프로젝트에

현재 내가 사용하고 있는 위치에서 콘솔 창을 켜서 위와 같이 명령어를 입력하면 안드로이드가 Java언어로 설정이 되고 파일도 생성이 된다. 그 후 Android-app-main에 있는 Kotlin을 전부 삭제하면 된다.

이렇게 java파일이 생성되었다.

 

VSCode에서 AndroidStudio로 넘어가기

간단하다 android 폴더에서 우측 클릭하여 'Open in Android Studio'를 클릭하면 된다.

 


원래 이 포스팅 글에 안드로이드 화면도 띄우는 걸 포스팅하려 했는데, 너무나도 길어짐에 따라 다음 글에 포스팅해보도록 하겠다. 

 

궁금한 거 혹은 잘못된 게 있다면 댓글 달아주세요.

728x90