Testing & Validation

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Your Swift validation logic is integrated with Kotlin - now it’s time to build the app and test it! You’ll verify that Swift correctly validates user input and see the complete system working together.

Building the Application

Build your app using Gradle. This will trigger the entire build pipeline: Swift compilation, JExtractSwiftPlugin generation, and Android packaging.

cd /path/to/Starter

# Build Swift code (triggers JExtractSwiftPlugin)
./gradlew buildSwiftAll

# Build the complete Android app
./gradlew assembleDebug

# Install on device/emulator
./gradlew installDebug

Running the App

Launch the app on your device or emulator:

adb shell am start -n com.kodeco.android.swiftsdkforandroid.taskmanager/.MainActivity

Test Case 1: Valid Task Creation

Let’s verify that valid tasks are accepted.

Test Case 2: Title Too Short

Now test validation failure with an invalid title.

Test Case 3: Title Too Long

Test the maximum title length constraint.

Test Case 4: Description Too Short

Test description validation.

Test Case 5: Description Too Long

Create a task with:

Test Case 6: Boundary Conditions

Test the exact boundaries:

Understanding the Cross-Language Flow

When you tap Save with “Hi” as the title, here’s the complete data flow:

1. User Input (UI)
   CreateTaskDialog → onSave("Hi", "Valid description", Priority.HIGH)
   ↓
2. Kotlin (TaskRepository)
   addTask(title = "Hi", ...)
   if (!TaskValidator.validateTitle("Hi")) { ... }
   ↓
3. Generated Java (TaskValidator.java)
   public static boolean validateTitle(String title) {
       return TaskValidator.$validateTitle(title);  ← Calls native method
   }
   ↓
4. JNI Bridge (TaskValidator+SwiftJava.swift)
   @_cdecl("Java_com_kodeco_...")
   - Converts jstring to Swift String
   - Calls Swift function
   - Converts Swift Bool to jboolean
   ↓
5. Swift (TaskValidator.swift)
   public static func validateTitle(_ title: String) -> Bool {
       let trimmed = title.trimmingCharacters(in: .whitespacesAndNewlines)
       return trimmed.count >= 3 && trimmed.count <= 50  ← Returns false
   }
   ↓
6. Return Path
   Swift (false) → JNI (jboolean: 0) → Java (false) → Kotlin (false)
   ↓
7. Kotlin Handles Result
   if (!false) {  // true
       return Result.failure(Exception("Title must be between 3 and 50 characters"))
   }
   ↓
8. UI Displays Error
   CreateTaskDialog catches Result.failure and shows error message

Viewing Generated Code

Want to see what JExtractSwiftPlugin created? Navigate to the generated files:

cd taskmanager-lib
find .build/plugins/outputs -name "*.java"
package com.kodeco.android.taskmanagerkit;

public final class TaskValidator {
  static final String LIB_NAME = "TaskManagerKit";

  public static boolean validateTitle(String title) {
    return TaskValidator.$validateTitle(title);
  }
  private static native boolean $validateTitle(String title);

  public static boolean validateDescription(String description) {
    return TaskValidator.$validateDescription(description);
  }
  private static native boolean $validateDescription(String description);

  static {
    System.loadLibrary(LIB_NAME);
  }
}

Debugging Tips

If validation isn’t working:

Performance Considerations

You might wonder about the performance of calling Swift from Kotlin. The JNI overhead is minimal:

What You’ve Accomplished

In this lesson, you’ve:

Where to Go From Here?

You can access the completed project files from the Materials section in the right sidebar. Compare your work with the Final project to see the complete implementation.

What’s Next

In the next lesson, Platform Integration: Camera and Image Processing, you’ll expand your Task Manager with photo capture capabilities. You’ll learn:

Additional Resources

Want to learn more about Swift for Android?

Alternative Approach: Manual JNI

For educational purposes, you might want to understand how JNI works under the hood. The manual JNI approach (what swift-java automates) involves:

Congratulations

You’ve successfully built a hybrid Android application that uses Swift for business logic and Kotlin for the UI. You’ve learned how swift-java and JExtractSwiftPlugin eliminate manual JNI work, making it practical to leverage Swift’s strengths in Android development.

See forum comments
Download course materials from Github
Previous: Kotlin Integration & Gradle Build Automation Next: Conclusion