Use same model classes (DTO) between flutter and the native code - android

By now we have 2 android apps, one invokes the other one (directional relation). The first passes data via intent, by the compiled and published to our nexus repository model, written in kotlin (serialized to json).
Now, we want to migrate the second (invoked) app to the flutter, and still have the shared model. My question is - is it possible to use kotlin DTOs to the flutter (so far I see that the method channel does not allow for custom classes), or even better - to compile dart package to jar, and use it in the native code? In the first case I think about something like typescript definition files used when you use ts and js in one project.

Related

Kotlin Multiplatform Mobile(KMM) .I want to make my Android application work on iOS using KMM?

eg: details about the questions ..i have a android app project using Kotlin. I want to make my Android application work on iOS using KMM.i haven't found any live solution yet. it would be great if you've any practical example to to make my Android application work on iOS using KMM?
there is no magic tool to do this ...
you can follow this steps ..
first you should convert java code to kotlin code by
Android Studio Menu -> Code -> Convert Java File to Kotlin File
then you should separate your code into business layer and presentation layer you can know about it by reading clean architecture Android
then we will put business layer into Shared Module
then we will put presentation layer into ANDROID APP
then application will be ready to use his business logic into ios
then you can write your swift code into IOS APP
note :
Presentation layer contain view , viewmodel and any Platform dependent tools like workmanger
business layer contain network and caching code like KTOR , SQLDELIGHT and any logic that may be shared between Android and Ios

What is the definition of Android Native Code?

I always see the term android native code here and there but not sure what exactly it is. So which part of android is android native code? Are Application, Activity, Fragment, View, Looper android native code?
More context: I'm trying to understand the shadow in robolectric tests, and in the doc it is said that "Android native code cannot execute on your development machine", so I'm wondering are classes like Application, Activity etc android native code referred here?
http://robolectric.org/extending/
The word native in Android development is overloaded.
Going through link you provided for Robolectric:
There are limitations however:
Native code - Android native code cannot execute on your development machine.
Out of process calls - There are no Android system services running on your development machine.
Inadequate testing APIs - Android includes next to no APIs suitable for testing
Roboletric fills these gaps with a set of classes known as Shadows...
So in this case the Android native code quote either refers to:
The Android Framework classes like Activity,Fragment,View where with only the Android SDK apps needs an emulator or device to run. But Roboletric bring its own Android Framework code which can be 'enhanced' by 'Shadows' for testing apps.
OR
The Android Native Development Kit (NDK) which uses Java Native Interface (JNI) to allow Java/Kotlin code to access C/C++ code for performance or compatibility reasons. So with Roboletric you can't call into JNI code, or at least not without some effort.
Later that same page:
Using byte code instrumentation Robolectric is able to weave in cross platform fake implementations to substitute for native code and add additional APIs to make testing possible.
The substitute for native code refers to Java/Kotlin APIs belonging to the Android Framework which Roboletric is substituting for to provide a test environment. Again, those would be the Activity, Fragment, View, etc. you were referring to.
The usage of the term 'native' in this case is similar a developer using a third-party app building framework like 'React Native', 'Ionic', 'Flutter', 'Xamarian', or 'Cordova/Phonegap', they may use a custom component written in Java/Kotlin as a native component to achieve some function which can only be done by direct interaction with the Android Framework but not in the language/API of that third-party framework like Javascript, Dart, or C#.
Java and its kin (Kotlin, Scala, etc.) refers to calling C/C++ code as native via the Java Native Interface (JNI) and on Android is facilitated by the Native Development Kit (NDK). Third party app development frameworks that sit on top of the mobile framework will refer to calls into the original mobile framework as "native".
Sadly as this is part of the terminology used in mobile development so a careful reading of the use of the word "native" is required.
Personally I would prefer if documentation using the word native included the language like native Java/Kotlin APIs or native C/C++ APIs as the first instance in the linked page had me going back and forth about the author's meaning.
Follow up to questions in comments
You mentioned that "they may use a custom component written in Java/Kotlin as a native component", you are referring to Activity, Fragment etc when saying custom component, right?
In that particular section I was referring to third-party app frameworks calling into classes that are Android Framework or directly call parts of it. Normally those third-party app frameworks already wrap/expose Activity, View, etc. but you as a developer may need a library or other custom Java/Kotlin code which can be invoked by the third-party app framework language (Javascript, Dart, C#). From the perspective of the third party app framework the 'wrapped Java/Kotlin library' is a native component as it is "native" to the mobile environment. That wrapped library code isn't written in Javascript, Dart or C#. Again the meaning of "native" is overloaded.
In the first paragraph of link, the author is emphasizing that we will run "real Android code" in robolectric. But as we analyzed, robolectric is shadowing the basic building block like Activity, Fragment, which seems contradictory to me, so the only explanation I can think of is that the ShadowActivity is wrapping the original implementation of real Activity, do you think that is the case?
Yes, the ShadowActivity is "wrapping" the original implementation of real Activity, I would take note that the author states: Shadow objects are not quite Proxies, not quite Fakes, not quite Mocks or Stubs.
It is important that shadow methods are implemented on the corresponding shadow of the class in which they were originally defined. Otherwise Robolectric’s lookup mechanism will not find them (even if they have been declared on a shadow subclass.)
and
Shadow class inheritance hierarchy does not always mirror that of their associated Android classes, it is sometimes necessary to make calls through these real objects so that the Robolectric runtime will have the opportunity to route them to the correct Shadow class based on the actual class of the object
So regular Java inheritance isn't quite the correct mental model for Shadows.
Android native code is not Java or Kotlin. It is not some classes like Activity or Fragment. Android native code is C/C++. Here is a bit of info about SDK(NDK).
And here you can find general overview of NDK(native development kit).
Hope it helps.

Reusable components with Flutter

The problem
My idea is to create some simple components using Flutter so that they can be used in multiple native iOS and Android projects in our firm.
For example, consider a generic login view. This view can be coded once in Flutter and then can be included later on into projects that have both iOS and Android parts(native) . So such common components can be written once using Flutter instead of twice as native iOS and Android components.
What I have tried so far
I have followed the steps here https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps and it works, but it seems very restricted in functionality.
My questions are
How can I add multiple flutter projects like this, with each project representing a reusable component? Is that even the way to go?
Can I have smaller components- for example in iOS, UIViews instead of FlutterViewController without native hacking ?
How do I handle passing data between the native and Flutter part and also the navigation?
What do you mean by "it seems very restricted in functionality" ?
https://flutter.dev/docs/development/add-to-app lists these 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 sharable library or packing multiple Flutter libraries into an application isn’t supported.
Your 1st question is answered by the 3rd limitation. If you were considering splitting your simple components up into a separate Flutter library each, then no. You would need to package all your components into a single Flutter library, then let the native app pick from the pool.
For your 2nd question, can you be more specific what you mean by "without native hacking" ? Is there a reason you don't/can't use FlutterViewController?
3rd question about passing data between native and Flutter will involve https://flutter.dev/docs/development/platform-integration/platform-channels
From Flutter to native, here's an Android example https://stackoverflow.com/a/49308193/6668797, passing the value "text":
// flutter side, send the data
final String result = await platform.invokeMethod('getBatteryLevel', {"text":text});
// native side, retrieve it
String text = call.argument("text");
From native to Flutter, you use result.success(batteryLevel); to send it back to final String result.

Create SDK (or Library) from existing Android App (APK) that can be included in other Android Apps

I have an existing Android App that we now want to leverage as an SDK (or whatever is the equivalent on Android, a library?), so that the application could be included in another Android App as a library.
The concept is that we provide a "wrapper" class that customers make calls to, which would then interface with the existing code to do the functions, display stuff and do the work our App generally does.
My hope is to be able to not have to move code around and just create a wrapper/SDK/Library interface which I can just build differently in gradle, and the result of that (a Library object?) would be given to the customer to include into their App.
Hope I am making sense. If you need more info I can give a high level example of what the App is doing.

Are there any simple way to use c/c++ source code directly on my android application?

I am currently working on an android application that evaluate images in different aspects, and I found that there are lots great open source algorithms can be used.
Problem 1: Most of the algorithms are designed on c/c++/matlab languages that cannot be applied directly.
I've search that NDK is a tool that allows us develop android application by other languages, but the setup procedures are quite complicated that I stuck for days. So before I go further on it, I would like to first ask whether I can include other's c/c++ source code directly like calling java library?
Problem 2: For example, I would like to use Point Matching algorithm's source code in my application, but there are lots files inside as it's just source code but not library/plugin. What are the steps to apply the require functions in my android application?
(the most desired way is to blindly input some images to the alogrithm, and it returns the results like calling functions, which I dont have to understand the working principle of it.)
You can't directly use other C++ libraries, you have to build them for Android first using NDK. If there is a version of the library built for Android, then, of course you can use it directly by linking to it using NDK.
You have two options here. First, you create a regular Java application for Android, write a C++ wrapper for handling calls to native side and build the necessary source files using NDK. Your java application will make calls to wrapper using JNI and the wrapper will call your actual C++ functions as in Java->JNI wrapper on C++->Your actual C++ source.
Second option is going fully native, which will leave out Java, JNI calls and the wrapper. You can access your source files directly as if you were writing a desktop C++ application. In the end you will have to build using NDK but this step is required in any case.
As a suggestion, you can also take a look at OpenCV for image processing purposes. It has libraries built for Android, all you will have to do is to link them.
Short version.
Download opencv4android library. Import it in eclipse and see if everything is fine (compile errors, output, etc.).
Secondly, try to import face detection application and try to understand how it works.
It has a java part and a native part.
In order to understand how it works you must understand how java interacts with C++, more or less how NDK works.
One important aspect is to learn how to create your interfaces in C++, based on the native ones defined in java. When you got there then you can try creating your own application.
After that you can come here and ask specific questions.

Categories

Resources