- Notifications
You must be signed in to change notification settings - Fork11
SwiftJava/swift-android-kotlin
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
An example application for mixing Swift, OpenGL 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. This branch uses scarcely modified code from Josh A Beam'sCocoaOpenGL-Swift to demonstrate using OpenGL from Swift in an Android application.
This is a simple example of using Swift to create an OpenGL application for macOS, iOS, and tvOS. It features a rotating, normal mapped cylinder that is illuminated by three moving point lights of different colors.
Aside from the vertex and fragment shaders written in GLSL, all of the application code is written in Swift.
This application was developed byJosh Beam and is distributed under the BSD-style license shown below.
Copyright © 2015-2017 Josh A. Beam
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Requires a build of the latest Android toolchain downloadablehere. Once you've extracted the toolchain, runswift-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 theANDROID_HOME
environment variable is set to the path to anAndroid 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 Swiftcode and place them in aJava Source. Use the command./genswift.sh
in theSwiftJava Project to generate Swift (& Java) sources to include in your application or adapt thegenhello.sh script. Your app's onlyPackage.swiftdependency should be the core JNI interfacing codejava_swift.
This example is coded to work with version 7 of the toolchain which has some additional requirementsto work around requirements of the Swift port of Foundation. The cache directory used by web operationsneeds to be setup in the environment variable "TMPDIR". This would usually be the value ofContext.getCacheDir().getPath() from the java side. In addition, to be able to use SSL youneed to add aCARoot info file to the application'sraw 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)
To build, setup the Gradle plugin, then run./gradlew installDebug
About
Kotlin/Swift integration example
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.