Skip to content

fernandospr/minimal-android-ios-backend-lib

Repository files navigation

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

minimal-android-ios-backend-lib

Contains the Kotlin code to share across platforms.

You'll find this simple class:

class Example {
  fun hello(who: String) = "Hello $who!"
}

Compiling the library for Android/JVM apps

  1. Execute the Gradle Task: publishToMavenLocal using ./gradlew publishToMavenLocal. This task will publish in your ~/.m2/repository folder.

  2. Open the Android/JVM/Backend client project and make sure you have mavenLocal() in your repositories configuration.

  3. Add the dependency:

  • For Android: implementation("{group}:{rootProject.name}-android:{version}"). For this example, it's implementation("com.github.fernandospr:minimal-android-ios-backend-lib-android:1.0.0").
  • For JVM: implementation("{group}:{rootProject.name}-jvm:{version}"). For this example, it's implementation("com.github.fernandospr:minimal-android-ios-backend-lib-jvm:1.0.0").
  1. Now in your Android/JVM app code you can import the library classes.

Compiling the library for iOS apps

  1. Execute the Gradle Task: assemble{libName}ReleaseXCFramework using ./gradlew assemble{libName}ReleaseXCFramework. For this example, it's ./gradlew assembleMAIBLibReleaseXCFramework. This task will generate the framework in build/XCFrameworks/release of the library project.
  2. Copy the framework to your iOS client project.
  3. Open your iOS client project using Xcode, go to the project properties, open General tab and add the framework in the Frameworks, Libraries and Embedded Content section.
  4. Now in your iOS app code you can import the library classes.

android-sample-app

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")
  }
}

Screenshots

android-maiblib-sample

ios-sample-app

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")
  }
}

Screenshots

ios-maiblib-sample

backend-sample-app

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))
}

Screenshots

Using the Example class in a Web server that returns a String:

backend-web-maiblib-sample

Using the Example class in an API that returns JSON:

backend-api-maiblib-sample

About

Minimal Kotlin library to share logic in Android, iOS and JVM Backend projects.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published