简体   繁体   English

使用 Kotlin 多平台实现相机库

[英]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).在网上查了很多例子,解释如何制作日志或“hello world”,但缺乏复杂的例子,即使在官方文档中(重要的是要注意我只对移动设备感兴趣)平台,iOS 和 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:现在我按照官方示例创建了一个项目,所以它有一个通用模块(使用期望)和一个用于 Android 和一个用于 iOS(使用实际),现在这些是我的疑问:

  • I've seen that the iOS module is also in Kotlin, Kotlin/Native as I understand.据我所知,我已经看到 iOS 模块也在 Kotlin,Kotlin/Native 中。 Should my project have also an wrapper in Swift, or will the library have no Swift code?我的项目是否应该在 Swift 中也有一个包装器,还是该库没有 Swift 代码? 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?同样在 Android 模块中,我注意到我无法导入 class “活动”或“意图”,我需要打开相机,为什么? is this code restricted to Java without the Android libs?此代码是否仅限于 Java 没有 Android 库? Should it also have a wrapper to Android?它还应该有 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.我知道在创建类时可以使用“expect”键,但据我了解,通用模块和本机模块将始终是分开的类。 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?我的意思是,如果我在 common 模块中创建一个 class ,我可以使用“expect”定义这个 class 的方法并稍后在本机中定义它们吗?

  • 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.结果,iOS 模块生成 Objective-C 框架。 It can be utilized by the Xcode project the same way as any other framework with non-Kotlin origins. Xcode 项目可以像使用任何其他非 Kotlin 起源的框架一样使用它。
  3. It looks like the unavailability to use Android SDK is the result of using jvm("android") target instead of android() one.看起来无法使用 Android SDK 是使用jvm("android")目标而不是android()的结果。 To use the android target, one has to apply the android Gradle plugin in addition to kotlin-multiplatform one.要使用 android 目标,除了 kotlin-multiplatform 之一之外,还必须应用 android Gradle 插件。
  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.我想你想做这样的事情:只是普通的 class 声明中的common和扩展 function 使用expect修饰符。 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.我还建议您看一下thisthis ,也许这些示例对您来说已经足够复杂了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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