Slide 1

Slide 1 text

Enzo Lizama Adding Flutter to an existing Android/iOS app Mobile Developer at Mandü @enzoftware

Slide 2

Slide 2 text

Previous concepts

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Platform Channels?

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Method Channel The Flutter side sends notifications to the native side, usually used to call a method of native.

Slide 8

Slide 8 text

Event Channel Used for data stream communication, it has the function of monitoring, such as pushing directly to the Flutter terminal after the change of power.

Slide 9

Slide 9 text

Basic Message Channel Data used to pass strings or semi-structured objects.

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Project structure

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Integration with Android

Slide 17

Slide 17 text

Using Android Studio

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Adding flutter module manually

Slide 21

Slide 21 text

flutter create --template module flutter_module

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Depend on the Android Archive (AAR)

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

android { // ... } repositories { maven { url 'some/path/my_flutter/build/host/outputs/repo' // This is relative to the location of the build.gradle file // if using a relative path. } maven { url 'http://download.flutter.io' } } dependencies { // ... debugImplementation 'com.example.flutter_module:flutter_debug:1.0' profileImplementation 'com.example.flutter_module:flutter_profile:1.0' releaseImplementation 'com.example.flutter_module:flutter_release:1.0' }

Slide 26

Slide 26 text

android { // ... } repositories { maven { url 'some/path/my_flutter/build/host/outputs/repo' // This is relative to the location of the build.gradle file // if using a relative path. } maven { url 'http://download.flutter.io' } } dependencies { // ... debugImplementation 'com.example.flutter_module:flutter_debug:1.0' profileImplementation 'com.example.flutter_module:flutter_profile:1.0' releaseImplementation 'com.example.flutter_module:flutter_release:1.0' }

Slide 27

Slide 27 text

Depend on the module’s source code

Slide 28

Slide 28 text

// settings.gradle import javax.naming.Binding include ':.android' include ':app' setBinding(new Binding([gradle: this])) evaluate(new File(settingsDir.parentFile, 'flutter_android_ios/flutter_module/flutter_module/.android/include_flutter.groovy ' ))

Slide 29

Slide 29 text

// settings.gradle import javax.naming.Binding include ':.android' include ':app' setBinding(new Binding([gradle: this])) evaluate(new File(settingsDir.parentFile, 'flutter_android_ios/flutter_module/flutter_module/.android/include_flutter.groovy ' ))

Slide 30

Slide 30 text

apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { ... compileOptions { sourceCompatibility = 1.8 targetCompatibility = 1.8 } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation Dependencies.kotlin ... androidTestImplementation Dependencies.mockk androidTestImplementation Dependencies.junit implementation project(path: ':flutter') }

Slide 31

Slide 31 text

apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { ... compileOptions { sourceCompatibility = 1.8 targetCompatibility = 1.8 } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation Dependencies.kotlin ... androidTestImplementation Dependencies.mockk androidTestImplementation Dependencies.junit implementation project(path: ':flutter') }

Slide 32

Slide 32 text

Integration with iOS

Slide 33

Slide 33 text

Embed with CocoaPods and the Flutter SDK

Slide 34

Slide 34 text

target 'ios_app' do flutter_application_path = 'ROUTE_TO_FLUTTER_MODULE' # /Users/enzoftware/Projects/add_flutter_to_existing_app/flutter_module load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb') install_all_flutter_pods(flutter_application_path) # Comment the next line if you don't want to use dynamic frameworks use_frameworks! # Pods for ios_app target 'ios_appTests' do inherit! :search_paths # Pods for testing end target 'ios_appUITests' do # Pods for testing end end

Slide 35

Slide 35 text

target 'ios_app' do flutter_application_path = 'ROUTE_TO_FLUTTER_MODULE' # /Users/enzoftware/Projects/add_flutter_to_existing_app/flutter_module load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb') install_all_flutter_pods(flutter_application_path) # Comment the next line if you don't want to use dynamic frameworks use_frameworks! # Pods for ios_app target 'ios_appTests' do inherit! :search_paths # Pods for testing end target 'ios_appUITests' do # Pods for testing end end

Slide 36

Slide 36 text

Embed frameworks in Xcode

Slide 37

Slide 37 text

flutter build ios-framework --output=some/path/MyApp/Flutter/

Slide 38

Slide 38 text

Embed application and plugin frameworks in Xcode and Flutter framework with CocoaPods

Slide 39

Slide 39 text

flutter build ios-framework --cocoapods --output=some/path/MyApp/Flutter/ # In your Podfile pod 'Flutter', :podspec => 'some/path/MyApp/Flutter/{build_mode}/Flutter.podspec'

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Adding screens to work with Android

Slide 43

Slide 43 text

myButton.setOnClickListener { startActivity( FlutterActivity.createDefaultIntent(this) ) } The easiest way

Slide 44

Slide 44 text

myButton.setOnClickListener { startActivity( FlutterActivity .withNewEngine() .initialRoute("/my_route") .build(this) ) } The easiest way, with routes

Slide 45

Slide 45 text

lateinit var flutterEngine : FlutterEngine override fun onCreate() { super.onCreate() // Instantiate a FlutterEngine. flutterEngine = FlutterEngine(this) // Start executing Dart code to pre-warm the FlutterEngine. flutterEngine.dartExecutor.executeDartEntrypoint( DartExecutor.DartEntrypoint.createDefault() ) // Cache the FlutterEngine to be used by FlutterActivity. FlutterEngineCache .getInstance() .put("my_engine_id", flutterEngine) } Pre-warm engine

Slide 46

Slide 46 text

myButton.setOnClickListener { startActivity( FlutterActivity .withCachedEngine("my_engine_id") .build(this) ) } Pre-warm engine call

Slide 47

Slide 47 text

var newFlutterFragment = FlutterFragment.createDefault() flutterFragment = newFlutterFragment fragmentManager .beginTransaction() .add( R.id.fragment_container, newFlutterFragment, TAG_FLUTTER_FRAGMENT ) .commit() Fragments? Yes!

Slide 48

Slide 48 text

Adding screens to work with iOS

Slide 49

Slide 49 text

@objc func showFlutter() { let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil) present(flutterViewController, animated: true, completion: nil) } FlutterViewController with your FlutterEngine

Slide 50

Slide 50 text

let flutterEngine = FlutterEngine(name: "my flutter engine") flutterEngine.navigationChannel.invokeMethod("setInitialRoute", arguments:"/onboarding") flutterEngine.run() Working with routes

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Demo! Let’s see the code

Slide 53

Slide 53 text

Limitations ● Running multiple Flutter instances or running in partial screen views may have undefined behavior. ● Using Flutter in background mode is still a WIP. ● Packing a Flutter library into another shareable library or packing multiple Flutter libraries into an application isn’t supported.

Slide 54

Slide 54 text

https://flutter.dev/docs/resources/faq#how-big-is-the-flutter-engine How big is the Flutter engine?

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Summary

Slide 57

Slide 57 text

Enzo Lizama THANKS! Mobile Developer at Mandü @enzoftware https://github.com/enzoftware/add_flutter_to_existing_app