简体   繁体   中英

Implement camera lib with Kotlin multiplatform

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.

  1. I would recommend you to have a look at this example
  2. 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.
  3. 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.
  4. 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.
  5. I think so.
  6. I'd also recommend you to have a look at this and this , maybe these examples will be complex enough for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM