Demonstrates a minimal Kotlin library to share logic in Android, iOS and JVM Backend projects using Kotlin Multiplatform.
This repository contains the following folders:
- minimal-android-ios-backend-lib
- android-sample-app
- ios-sample-app
- backend-sample-app
Contains the Kotlin code to share across platforms.
You'll find this simple class:
class Example {
fun hello(who: String) = "Hello $who!"
}-
Execute the Gradle Task:
publishToMavenLocalusing./gradlew publishToMavenLocal. This task will publish in your~/.m2/repositoryfolder. -
Open the Android/JVM/Backend client project and make sure you have
mavenLocal()in your repositories configuration. -
Add the dependency:
- For Android:
implementation("{group}:{rootProject.name}-android:{version}"). For this example, it'simplementation("com.github.fernandospr:minimal-android-ios-backend-lib-android:1.0.0"). - For JVM:
implementation("{group}:{rootProject.name}-jvm:{version}"). For this example, it'simplementation("com.github.fernandospr:minimal-android-ios-backend-lib-jvm:1.0.0").
- Now in your Android/JVM app code you can import the library classes.
- Execute the Gradle Task:
assemble{libName}ReleaseXCFrameworkusing./gradlew assemble{libName}ReleaseXCFramework. For this example, it's./gradlew assembleMAIBLibReleaseXCFramework. This task will generate the framework inbuild/XCFrameworks/releaseof the library project. - Copy the framework to your iOS client project.
- Open your iOS client project using Xcode, go to the project properties, open
Generaltab and add the framework in theFrameworks, Libraries and Embedded Contentsection. - Now in your iOS app code you can import the library classes.
Contains a sample Android App that uses the library.
To build this app you'll need to compile the library locally first, as explained above.
MainActivity.kt contains the following code that uses the Example class from the library:
import com.github.fernandospr.maiblib.Example
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
...
val example = Example()
findViewById<TextView>(R.id.textView).text = example.hello("Fernando")
}
}Contains a sample iOS App that uses the library.
To build this app you'll need to compile the library locally and copy it to the iOS project folder, as explained above.
ViewController.swift contains the following code that uses the Example class from the library:
import MAIBLib
class ViewController: UIViewController {
...
override func viewDidLoad() {
...
let example = Example()
label.text = example.hello(who: "Fernando")
}
}Contains a sample Kotlin Backend App that uses the library.
To build this app you'll need to compile the library locally first, as explained above.
Controller.kt contains the following code that uses the Example class from the library:
import com.github.fernandospr.maiblib.Example
@RestController
@RequestMapping("/web")
class WebController {
@GetMapping("/hello")
fun hello(@RequestParam("who") who: String) = Example().hello(who)
}
@RestController
@RequestMapping("/api")
class ApiController {
@GetMapping("/hello")
fun hello(@RequestParam("who") who: String) = ApiResponse(Example().hello(who))
}Using the Example class in a Web server that returns a String:
Using the Example class in an API that returns JSON:



