• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

SwiftJava/swift-android-kotlin: Kotlin/Swift integration example

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

SwiftJava/swift-android-kotlin

开源软件地址(OpenSource Url):

https://github.com/SwiftJava/swift-android-kotlin

开源编程语言(OpenSource Language):

Swift 78.3%

开源软件介绍(OpenSource Introduction):

Kotlin example for the Android Swift toolchain.

An example application for mixing Swift and Kotlin in an Android application. This allows you to reuse model layer code from your iOS application when porting to Android. The "binding" between the Kotlin or Java code and Swift is completely type safe with all JNI code automatically generated using a script. Building the Swift code is performed using the Swift Package manager and a small gradle plugin.

Requires a build of the latest Android toolchain downloadable here. Once you've extracted the toolchain, run swift-install/setup.sh to get started and install the gradle plugin. You then run ./gradlew installDebug or build the project in Android Studio. Make sure the that the ANDROID_HOME environment variable is set to the path to an Android SDK. The phone must be api 21 aka Android v5+ aka Lollipop or better (I used an LG K4.)

To create a new application, decide on a pair of interfaces to connect to and from your Swift code and place them in a Java Source. Use the command ./genswift.sh in the SwiftJava Project to generate Swift (& Java) sources to include in your application or adapt the genhello.sh script. Your app's only Package.swift dependency should be the core JNI interfacing code java_swift.

This example is coded to work with version 7 of the toolchain which has some additional requirements to work around requirements of the Swift port of Foundation. The cache directory used by web operations needs to be setup in the environment variable "TMPDIR". This would usually be the value of Context.getCacheDir().getPath() from the java side. In addition, to be able to use SSL you need to add a CARoot info file to the application's raw resources and copy it to this cache directory to be picked up by Foundation as follows:

setenv("URLSessionCertificateAuthorityInfoFile", cacheDir! + "/cacert.pem", 1)
setenv("TMPDIR", cacheDir!, 1)

If you don't want peer validation you have the following option (not recommended at all)

setenv("URLSessionCertificateAuthorityInfoFile", “INSECURE_SSL_NO_VERIFY”, 1)

Simple demo of Swift code accessed over JNI.

To build, setup the Gradle plugin, then run ./gradlew installDebug

This demo is licensed under the Creative Commons CC0 license: do whatever you want.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap