I'm trying to understand what structure should have a multiplatform library. Checking on the Internet I've seen a huge number of examples explaining how to make a log or a "hello world" but there's a lack of complex examples, even in the official documentation (important to note that I'm only interested in mobile platform, iOS and Android).
So I want to create an example that simply opens the camera (as a lib, not as a multiplatform app) just to have an idea of how to work with a real feature which, also, is native. Right now I have created a project following the official example, so it has a common module (using expect) and one for Android and one for iOS (using actual), and now these are my doubts:
I've seen that the iOS module is also in Kotlin, Kotlin/Native as I understand. Should my project have also an wrapper in Swift, or will the library have no Swift code? And if it should, where should it be in the project structure?
Also in the Android module I've noticed I cannot import the class "Activity" nor the "Intent", which I will need to open the camera, why? is this code restricted to Java without the Android libs? Should it also have a wrapper to Android? If so, how can I configure this wrappers?
I know I can use the "expect" key when creating classes but, as I understand, the common and the native modules will always be separated classes. I mean, if I create a class in the common module, can I define methods of this class using "expect" and define them later in the native?
Can my lib have a Manifest?
Finally, does anyone knows a real example that really explain a more complex situation?
Thanks
Okay, let's go through your questions one-by-one.
I would recommend you to have a look at this example
The
iOS module produces an Objective-C framework as a result. It can be utilized by the Xcode project the same way as any other framework with non-Kotlin origins.
It looks like the unavailability to use
Android SDK is the result of using jvm("android") target instead
of android() one. To use the android target, one has to apply the android Gradle plugin in addition to kotlin-multiplatform one.
I
think you want to do something like that: just ordinary class
declaration in the common and extension function for it with an
expect modifier. And then actualize it in the platform-specific
code.
I think so.
I'd also recommend you to have a look at
this and this, maybe these examples will be complex enough for you.😁
Related
I have built a code in Android Studio with Kotlin. I initially haven't set it up as a Kotlin Multiplatform Mobile project and I would like to bring it now also on iOS. Is there a way to "convert" it to a Kotlin Multiplatform Mobile at this stage? Thanks
This is a multi-step process:
You'll need to adapt your build configuration - change build.gradle.kts to a KMM setup. Examples you can find at PeopleInSpace and KaMPKit repos
You have to make your Android code platform agnostic in order to share pieces with iOS
The changes you need to make depends on how many Android dependencies you have, how much platform-specific code you wrote and how much you want to share with iOS.
I wouldn't underestimate how big of a refactor this might need, as there are loads of platform-specific things that as an Android devs can overlook (JUnit, File, Dates are just a few examples, where we commonly used JVM libs)
I'd recommend starting small, creating a shared module with a KMM configuration, then step-by-step moving things to shared
Reach out if you have more questions
Checkout this detailed video that explains this process https://www.youtube.com/watch?v=X6ckI1JWjqo
You would need to switch using Kotlin native libraries for common shared code, so depending on that, you might have more or less work to do.
On top of that, you would need to use strategies around dependency injection, common view model layer etc.
What i want to achieve is something like creating my own SDK like what react-native-fbsdk or rn-onesignal-sdk provides , so that those functionalities can be implemented in the react native project.
Basic questions :
Where to start from? Like what are the pre-requisites?
Native coding is required i believe for this functionality?
Any explanation or any such experience in such would be of great help .
Where to start from? Like what are the pre-requisites?
You can use a template like this one. It includes an example project which is basically an app that you can use to test integration of your native module and to quickly iterate on it.
Native coding is required i believe for this functionality?
Only if you need to use platform-specific functionality, or provide bindings to some pre-existing native library. Both examples you mentioned do exactly that – because they have native SDKs that can be used in non-RN apps, and they provide React Native bindings for them.
If your library is just business logic + some networking, and you don't intend it to be used in non-RN apps, you can do it all in JS. No need for native code. If you choose to go with the template that I have linked above, there are instructions how to use it to create a pure JS React Native library.
I'll be happy to update my answer if you can share some concrete requirements of what you want your SDK to do.
Are there any advancements in this area? I want to be able to write purely functional code on Android in Haskell or similar languages. I've tried some examples with Scala but it seems to be a pain to get started. Are there any other functional JVM languages which I can use to write Android applications?
Edit: functional languages that write native android applications. My mistake about the JVM.
I doubt that you can find anything mature for writing Haskell-like code for Android. You do need to implement Java abstractions which are required by Android API (implement activity, etc.).
But if you really want to write for Android in a purely functional style you can try to implement your business logic in a pure functional language that compiles to JVM and call it from your Java classes. That approach would be much simpler than trying to implement it entirely in pure functional style.
As your language choice, you can try
Frege, it even has a library for android - froid
Eta lang, it is very new and probably nobody has tried to use it for Android yet
I you want a painless solution in terms of Gradle builds etc., you have only two options: Java and Kotlin, of which of course you should choose Kotlin ;)
Kotlin has most of the things you need to write in functional style:
functions as first class citizen
higher-order functions
immutable collections
var and val like in Scala
if-else as a statement
elements of pattern matching (where statement)
tail recursion
and more...
If you also include funKTionale and kotlinx.collections.immutable, you'll have all the functional goodies like: Option, Try, currying, memoization, persistent data structures and so on...
To start with Kotlin just install the latest Android Studio 3 Preview, which already has built-in Kotlin support.
BTW, don't be so polarized into "pure" functional ;) After all, being 100% "pure" means no side-effects, which means your app can't interact with the user ;)
Hope this helps :)
I've never tried it personally, but you can do F# programming using Xamarin.Android (and, I believe, with Xamarin Forms too). You can see the guide here. (It also includes sample code).
As some background, F# is the .NET Framework's functional language. It is derived from ML; in fact, many ML scripts can be compiled almost "directly" as F# (with the caveat that you may have to do some renaming because F# has some additional keywords that ML doesn't have in order to support several .NET-specific extensions).
Xamarin allows for native development for Android, iOS, and Windows phone. Xamarin Forms allows for a single code-base for all three platforms (it's a competitor to Ionic).
One more quick point: Android does not use the JVM, even if you're writing apps in Java. (In fact, Android does not even support all Java 8 features yet). Through Android 4.4 it used Dalvik; after that, it started using Android Runtime.
You could also try using a JVM language like Scala to create a JAR file and create a bindings library for it.
Please also note that you'll end up using at least somewhat of a mixed paradigm - for example, things like Activities are objects, and the XML files used to define an Android screen is, for all practical purposes, declarative. Edit: This last point is slightly debatable - see the comments.
One final possibility: I haven't checked this out too closely, but try also this link for a site claiming you can do Scala in Android.
Between Pure functional and Java, there is a way which is IMO Pragmatic functional. For instance Redux achieves that in the React arena.
My goal is to write an app (Activity) having an immutable state that advanced as a result of interactions is functional.
In the browser you can see that done with elm (Haskel like language which is also web platform)
Since we want an Android app, I opened the Android Studio used the wizard to create and app with Navigaton Drawer Action bar (with Drawer, FloatingActionBar),
Then converted it to use Elm concepts of immutable model functional approach into a working POC based on a small ElmBase class and idioms.
The code is written in Kotlin (JetBrains tool of choice for the JVM).
You can find the app at my GitHub https://github.com/saffih/ElmDroid The sweet spot of that approach is that it leverages Kotlin being strongly typed and the editor does code completions very well,
making lot's of the code completed for me in a way I have never seen before - amazing experience (But it require using idioms like the sealed class and when properly).
Does anyone have experience with NativeScript and can compare it to developing native apps, especially for Android?
I have read all these articles:
FIRST THOUGHTS ON NATIVESCRIPT
SECOND THOUGHTS ON NATIVESCRIPT
Introduction to Native Script – Is It Worth Your Time?
My Experience Developing with Telerik NativeScript
I know especially three of them may be outdated. But I want to ask all of you developers:
How is your experience with NativeScript?
Are there any Android-Components you cannot use? Which are these ones?
Is styling really so limited?
Do apps really look so different at runtime as in the mockup as in the pictures of the first article referenced above?
Does loading of native Android objects into JavaScript Code always work correctly?
Does NativeScript generate Java-Code for Android-Platform out of the NativeScript code I write?
Is it possible to modifiy this code if I want to use some native-only features? What if I want to make UI changes then? Do I have to regenerate the code and do I miss my native extensions then?
Very glad to see that you are evaluating NativeScript to eventually use it in present and future projects.
I'll try to condense answers to a few of the questions into one, as they really are mostly related.
Skipped.*
That depends on what has already been exposed through a custom view/plugin or module. The core-modules that every NativeScript app comes with contains the most basic of wrappers for both Android and iOS under a common API. There are plugins (nativescript npm modules) that provide additional wrappers on native android views (nativescript-telerik-ui for one, nativescript-carousel), most of which are created by the NS community.
As RexSplode mentioned before me - it's mostly the platform that imposes certain limitations. NS uses CSS to declare style, but you can also access the native components and manage their style and appearance programatically if what you need isn't readily available out of the box.
First I'd like to note that the first 3 articles you've linked are over a year old now, and trust me, NativeScript has evolved a lot since then. With all the available components (remember the npm modules I mentioned earlier?) there's a good chance that you will get a close to 1:1 similarity to a well-styled native Android mockup.
At build time metadata is generated for the Android/Java public API used in the project. When the JavaScript Engine (V8) fires up, that metadata is loaded into memory, prototype chains are constructed, and callbacks are attached, so that when you call new android.widget.Button(); in your JavaScript code, the proper virtual machine instructions will be called, and a native button will be created. Static methods are accessed similarly, check out the official docs to get a better understanding of how it all works.https://docs.nativescript.org/runtimes/android/advanced-topics/execution-flow
and 7., and a cont. of 2. Java code, or rather compiled Java code is generated whenever you wish to extend a native Android class that isn't available already in a module or in the native Framework. Extending classes is very similar to how you would do it in Java - you extend a class, and create new implementations of interfaces. That means that you won't have to open Android Studio to create a new class, build it into a native plugin and then add it to your project, since you can do it all in your NativeScript code using JavaScript/TypeScript. https://docs.nativescript.org/runtimes/android/generator/extend-class-interface
Disclaimer: I am on the NativeScript Engineering team
I investigated the Native Script a little and my colleague at work writes an app with it, so I can offer you a bit of information that I have.
1. skipped
There are limited amount of components you can use with native script out of the box. However, if you have a native-java developer who can write a wrapper for you - you can use everything.
It is limited to the platform you are using. Android itself has a lot of style limitations which cannot be easily overwhelmed.
don't know
It works a little different. Your JS object, or rather widgets are translated to java code. So with the items from the box - yes, they are okey. If you write a wrapper for your custom component, then all is up to you.
Yes it does.
No, the code is generated, how are you going to modify it? Changes will be undone on the next build. However, you can write a native module for your application and use any features you want. It is like defining an interface, which you can use in JS code afterwards.
Is there a handy-dandy equivalent to org.apache.commons.beanutils.PropertyUtils on Android?
I can't seem to use bean utils in my android app due to some dependencies on PropertyDescriptor,and IndexedPropertyDescriptor. So I'm wondering if there are any alternatives?
Basically all I want to do is use a method name as a string "someMethod" and feed that into setMethod(anObject, "someMethod", value), much like PropertyUtils does; but without having to resort to the nasties of reflection...
Or are my hands tied and I need to use Reflection?
There is bridge library which works on Android: android-java-air-bridge.jar. Just include into project path and use all apache beanutils features in your Android project as you could use in ordinary java application. Moreover, there are lot of other classes which moved to this Android supporting library. Look at the list.
There is a possibilty to use libraries or write own code depending on the PropertyUtils. But it sure isn't dandy. You can get the general idea about what has to be done in this thread.
There are apparently some projects who have successfully solved the issue, so you can study thier solution. Take a look at Drawingpad-base and libgdx. You can find PropertyUtils in the package com.madrobot.beans in the first project and com.badlogic.gdx.beans in the second.