Technical Articles
Flutter with SAP Cloud Platform SDK
Flutter with SAP Cloud Platform SDK
Abstraction
One single mobile development language for multiple-platforms is always a dream for pass few years. We have gone through a few of different paths, such as Cordova WebView, React Native, and recently NativeScript for generating Native SDK during runtime. And recently we keep hearing the term of Flutter which has been adopted by some big companies as the cross-platform development language.
I believe there are a lot of documents have introduced how can you utilize Flutter for your cross-platform UI development. But there are a lot of areas which required to be handled platform specifically, such as client authentication, secure store, offline storage, push, and etc. SAP Cloud Mobile Service has provided native iOS & Android SDK which will address such kind of platform specific issues. It will provide you the functions of enterprise level security, online / offline OData support, connecting to SAP backend systems. This article will share with you how to enhance your Flutter application with those enterprise functions by utilizing SAP Cloud Mobile SDK on both iOS and Android platforms.
You will find this article will help you when you
- Want to use Flutter language to develop mobile application and connect to SAP backend system
- Want to achieve OData online and offline functionality for your Flutter application.
- Want to have enterprise security storage for your Flutter application
Cross-platforms development is always the hot topic. We believe that to fully address all of the problems will take some time. But we like to share with you whenever we get some ideas and also like hear your feedback.
Introduction
Flutter became a buzz word recently, especially after Google IO this year. It sounds quite promising in term of developing beautiful mobile application, supporting both Android and iOS with one single code line. And the developer stories show us that some big companies begin to develop their applications with Flutter, like BMW, Alibaba, etc.
So, as a mobile development team, what kind of help can we provide if this technology is widely adopted by the community? This will be a question we should prepare and try to answer.
To answer the above question, we look into the features our cloud SDK (Android and iOS) provided, and think OData, especially offline OData, will be our killer features. This blog is not able to answer the question, but we do want to make our hands dirty and do some preparation for the answers.
The code is share here: https://github.wdf.sap.corp/I062692/flutter_online_odata_sample
Let’s Code
Prepare the environment
- Download Android SDK 2.2.0-SNAPSHOT, and install it to make sure we can access the modules in the local maven repository;
- Setup flutter development environment (https://flutter.dev/docs/get-started/install)
- Install Android Studio, or VS Code as the editor.
Create Flutter Plugin
Run the following command to create the flutter plugin project:
flutter create -t plugin –org com.sap.cloud.flutter -a kotlin –androidx online_odata
The command will create a flutter plugin project ‘online_odata’ with ‘Kotlin’ as the Android development language and AndroidX support.
The reason we create the project with -t as ‘plugin’ is because we can add both Android and iOS code so the flutter/dart can call into. (https://flutter.dev/docs/development/packages-and-plugins/developing-packages)
Then we can go into online_odata/example folder, then
flutter run
To launch the example application in the simulator currently running. If more than one simulators are running, we need to provide ‘-d’ with the simulator ID to the above command.
Without changing anything, the generated project can run both on Android and iOS, here is the result.
Folder Structure
Before we go on, let’s look at the folder structure of the generated application to see what we need to change for this example.
ONLINE_ODATA
|__ android //android Java/Kotlin code
|_ ios //iOS code
|_ lib //dart code in here.
|_ example //an example application on how to use the plugin of ‘ONLINE_ODATA’
Since Android SDK 2.2.0-SNAPSHOT is targeting Android Studio 3.5, I did the following version changes:
ext.kotlin_version = ‘1.3.50’
com.android.tools.build:gradle:3.5.0
minSdkVersion 23
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
We can search the keywords above and replace them with corresponding version.
Add Android SDK Dependencies
Now let’s add Android SDK dependencies into build.gradle. The changes are in ‘online_odata/android/build.gradle’.
- add mavenLocal() so Android SDK library can be accessed;
- 2 ‘classpath’ in dependencies so we could generate OData proxy classes
- 2 ‘implementation’ at the end of the file
Add above into the ‘dependencies’ at the bottom of build.gradle. There is property ‘sapAndroidSDKVersion’, we can define it in the gradle.properties.
With the changes above, run ‘flutter run’ again, then we can find the generated proxy classes in ‘online_odata/example/build/online_data/generated/source/odata’
Build the Bridge between Flutter and Java/Kotlin
Now let’s locate ‘OnlineDataPlugin.kt’ in ‘android/src’ folder and add another ‘method call’ support.
The generated version of this file has only ‘getPlatformVersion’ support, now we add another one, ‘getCustomers’, and execute it within an ‘AsyncTask’ to avoid the error saying something like ‘network call in UI thread.’
The AsyncTask basically calls into the online OData library to retrieve the customer list and return the list in a JSON string. To make things simple, we add a ‘BasicAuthenticationHeaderInterceptor’ which hard-coded the authentication header.
Dart Code
Now we need to provide the dart support so code in ‘example’ can access ‘getCustomers’ defined above.
We add a method into ‘online_odata/lib/online_odata.dart’, as below.
Then we can modify the dart code in ‘example’ to show the customers into list view.
Add ‘FloatingActionButton’ inside ‘Scafflod’, when clicking the button, we navigate to ‘Customers’ list page, please see the source of ‘customers_widget.dart’ for details, it will populate the returned JSON data into a list.
The running screen looks like:
Summary
Before we did this test, we also spent some time on trying to re-write some of the online libraries in dart, so both Android and iOS can use the same code line. But, to fully support the features SAP Cloud Platform SDK online OData library has currently, it will need a lot of efforts, and when we talk about offline later, there might be only one option left, that is, leveraging the current offline/online OData libraries.
The example above did not touch the security part, we hard coded in the basic authentication header in a class to make the example work. But security is another killer features of cloud SDK, to make the story more interesting, we may also provide support on security part with the same approach as in the example.
Currently, the areas we touched are:
- Providing ‘flow’ framework with dart;
- Prototype an ‘ObjectCell’ widget to see how to provide fiori style widget in flutter
- Security store plugin to leverage library in Android SDK;
- Basic and OAuth flow in dart
- Firebase messaging support prototype.
Besides helping us better understand the features within our Android SDK, it may also bring more discussions about the possibilities in mobile development in the team.
github updated, iOS side works now.
Hi, I am working with Flutter to build an application for the SAP Innovator Challenge, would I follow the same process above to test this on a MAC with iOS?
Thank You
Larry Duncan
Hi, 'the same process', if you mean the way to develop iOS mobile app with Flutter, I guess it would be 'Yes'. This prototype was one year old, Flutter has more new stuffs by now, but I think we can still use the 'same process' for mobile development.
But 'the same process', you mean for creating a plugin as the bridge for flutter and native code, please note that our prototype is only a tryout, there were many things left out in this blog, for example, how to transfer different data types with the bridge.
Thanks
Qiang
Hi. great job!
The link of https://github.wdf.sap.corp/I062692/flutter_online_odata_sample is broken.Can you check please?
How much effort we need to do a CRUD comparing with SAPUI5 binding properties as we have today?
BR,
Mauricio
Hi,
Thanks for the comment.
The source code is shared in SAP GitHub, it should be accessible for colleagues (the link works for me), and we're now trying to make it pubic. Will update you when that's done.
As for the question on how much effort for CRUD, I think the effort will be spent on exposing SAP online OData APIs in Dart. Here is the document of OData Component document of SAP cloud platform SDK for Android, which is the underlaying support for this prototype, many functionalities defined in OData Spec are supported.
SAP UI5 also has a very solid support for OData, if you're talking about the 'web' ability of Flutter, I'm afraid I cannot comment on that. For Dart, actually we can try to implement OData APIs in Dart so both iOS and Android can use the same code line, but to fully cover the features defined in OData Spec, it will take a lot of efforts to reach what currently SAP OData component provides, that's the reason the prototype goes with Flutter channel support and call into Native OData lib both in Android and iOS. And if we add 'offline OData' support into the picture, from the mobile SDK perspective, this might be the only feasible way to go. (Offline support prototype also added into the source code).
Thanks
Qiang
Thanks Qiang!
For the source code, since the process may take some time, before we can make it public, here are some references for the flutter channel. The prototype is using the very basic part of this, once we add SAP Cloud Platform SDK for Android into the dependency list, passing data through the channel is simple.
https://medium.com/flutter/flutter-platform-channels-ce7f540a104e
Thanks
Qiang
Cool! Thanks buddy!
Hi!
Thanks for the interesting blogpost.
Could you help me? I have a problem, when I run this
flutter create -t plugin –org com.sap.cloud.flutter -a kotlin –androidx online_odata
The error is the following:
Multiple output directories specified.
Try moving -a to be immediately following create
I'm using Windows 10 x64. I tried to run under Android Studio 3.5.1 and Windows PowerShell
Thank you,
Seng
Thanks for comments. The command line should be:
flutter create -t plugin --org com.sap.cloud.flutter -a kotlin --androidx online_odata
You can run 'flutter help create' to see the document.
Thanks
Qiang
Hi Qiang!
I have the same problem, like Mauricio, the link is not working for me:
https://github.wdf.sap.corp/I062692/flutter_online_odata_sample
Could you make it public?
Thanks
Hi, sorry for the late response.
We're in the process of making the source code public, but please be aware that it may take some time...
Before that happens, one way I can think of which might be worth trying. Basically, the ideas of this blog are:
so, if you can follow the steps above and share your git repository with me, then we can talk more easily. What do you think?
Thanks
Hi Qiang!
Any updates on the progress of making the project public? Would be great to get (official) Flutter support.
Thanks!
Hi Dominik,
This blog was more than one year old now, and it was a POC when we did that. Since both Flutter and Google Android SDK have many new stuffs, and SAP Cloud Platform SDK for Android is now about 3.x with many new features, making this public might be off the table now. Getting official Flutter support would be a big topic. I will report to my manager on this.
Thanks
Qiang
Source code please.....
Thanks
Hi,
Thanks for being interested in this topic, Please see the comment above, do you think that way will work?
Thanks
Qiang
i use sdk sap version 2.2.1
the error occurred after i change the gradle
Hi there, Nice to hear from you.
From the picture, it seems OData Android Gradle plugin was not found. The fix could be that
Then it could work.
Thanks.
thanks .
still error
Hi,
Seems the previous error was fixed, now it reports another one. Please see if you have the following defined in your build.gradle:
thanks
i hope it will be the last error 😀
Hi
With error like this I'm afraid I cannot guess what's going wrong. If the previous one was fixed, the generated classes can be found in 'build/generated' folder. Maybe we can remove some dependencies from your project to make it simple?
Could you please provide more information, like when this error happened?
Thanks.
Hello,
very quiet about Flutter and SAP Cloud Platform or should I say SAP BTP, since November 19.
Thanks for updates!
Best regards
Johannes