diff --git a/docs/getStarted.html b/docs/getStarted.html index f247837..8cfbb38 100644 --- a/docs/getStarted.html +++ b/docs/getStarted.html @@ -50,7 +50,7 @@

Prerequisites

-

How to Install Swift for Android

+

Install Swift for Android

For macOS

Before setting up Swift for Android, ensure that you have Android Studio installed on your macOS. Follow these steps: @@ -93,7 +93,7 @@

For Linux

-

Creating new Android project

+

Create new Android project

  1. Open Android Studio.
  2. @@ -110,44 +110,75 @@

    Creating new Android project

  3. Click "Next" to proceed.
-

Initializing Swift Package Manager (SPM) Project

+

Create Swift Package Manager (SPM) Project

-

To create a new Swift Package Manager (SPM) project within the app/src/main/swift subdirectory, run the following commands in the terminal:

- -
-
- -
-
-cd SwiftAndroidExample/app/src/main/swift
+            
    +
  1. +

    + Create a new SPM project within the app/src/main/swift subdirectory, run the following + commands in the terminal from the root project directory: +

    +
    +
    + +
    +
    +mkdir -p app/src/main/swift
    +cd app/src/main/swift
     swift package init --name SwiftAndroidExample
     
    -
    +
+

+ It will create a new SPM project named SwiftAndroidExample within the + app/src/main/swift subdirectory.This project follows the standard structure of + an SPM project and will be automatically built by Gradle as part of the Android project's + build process with custom build tasks configured in the build.gradle file. +

+ + +
  • +

    + Change the product type to a dynamic library in the SPM project manifest. To do that, + add the type: .dynamic parameter in the library description in the + Package.swift file located in the SPM project directory: +

    +
    +
    + +
    +
    +products: [
    +    .library(
    +        name: "SwiftAndroidExample",
    +        type: .dynamic,                     // add this line
    +        targets: ["SwiftAndroidExample"]),
    +],
    +
    +
    +
  • + -
    -It will set up a new SPM project named SwiftAndroidExample inside the swift subdirectory.
    -This project structure allows you to efficiently manage Swift dependencies and build your Swift code as a separate module within your Android Studio project.
    -        
    -

    Configure Gradle in build.gradle

    +

    Configure Swift SPM project in build.gradle

    + +

    + To configure SPM project build, make the following changes in the build.gradle file: +

      -
    1. In your Android project, access the app/build.gradle file.
    2. - Enhance the android section by adding: + Add the following code in the android section:
      -android {
      -    sourceSets {
      -        main {
      -            jniLibs.srcDirs = ["lib"]
      -        }
      +sourceSets {
      +    main {
      +        jniLibs.srcDirs = ["lib"]
           }
       }
       
      @@ -156,14 +187,14 @@

      Configure Gradle in build.gradle

    3. - In the dependencies section, include: + In the dependencies section, add:
      -In the dependencies section, include:
      +    implementation fileTree(dir: 'lib', include: ['*.jar'])
       
      @@ -196,45 +227,38 @@

      Configure Gradle in build.gradle

    -

    Initialize Swift Runtime and Load JNI Library

    -
      -
    1. - In MainActivity.java, add Swift runtime initialization in the onCreate method: +

      Initialize Swift Runtime and Load Swift Library

      -
      -
      - -
      -
      +            

      + In MainActivity.java, add the following code in the onCreate method + which performs required Swift runtime initialization and loads the SwiftAndroidExample + JNI library containing Swift code from the SPM project: +

      + +
      +
      + +
      +
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
       
      +    // Initialize Swift runtime and Swift Foundation library
           try {
               org.swift.swiftfoundation.SwiftFoundation.Initialize(this, false);
           } catch (Exception err) {
               android.util.Log.e("Swift", "Can't initialize Swift Foundation: " + err.toString());
           }
       
      -    // Continue with your activity code
      -}
      -
      -
      -
    2. -
    3. - Load the JNI library using: + // Load JNI library containing Swift code + System.loadLibrary("SwiftAndroidExample"); -
      -
      - -
      -
      -System.loadLibrary("SwiftAndroidExample");
      +    // other activity initialization code
      +}
       
      -
      -
    4. -
    +

    Java Methods Calling Swift Implementation

    In your Android application, you can declare Java methods that serve as bridges to Swift code. The implementation of these methods will be written in Swift, allowing seamless integration between the two languages. Here's an example: