Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ xcuserdata/
*.moved-aside
*.xccheckout
*.xcscmblueprint
*.DS_Store

## Obj-C/Swift specific
*.hmap
Expand Down Expand Up @@ -70,4 +71,9 @@ AndroidBluetooth.xcodeproj/*
Package.resolved

# VS Code
.vscode
.vscode

# Android
*.so
.gradle
.idea
44 changes: 44 additions & 0 deletions Demo/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// swift-tools-version: 6.2
import PackageDescription

let package = Package(
name: "SwiftAndroidApp",
platforms: [
.macOS(.v15),
],
products: [
.library(
name: "SwiftAndroidApp",
type: .dynamic,
targets: ["SwiftAndroidApp"]
),
],
dependencies: [
.package(
path: "../"
),
.package(
url: "https://github.com/PureSwift/Android.git",
branch: "master"
),
],
targets: [
.target(
name: "SwiftAndroidApp",
dependencies: [
.product(
name: "AndroidBluetooth",
package: "AndroidBluetooth"
),
.product(
name: "AndroidKit",
package: "Android"
)
],
path: "./app/src/main/swift",
swiftSettings: [
.swiftLanguageMode(.v5)
]
)
]
)
1 change: 1 addition & 0 deletions Demo/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
89 changes: 89 additions & 0 deletions Demo/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.compose)
}

android {
namespace = "com.pureswift.swiftandroid"
compileSdk = 35

defaultConfig {
applicationId = "com.pureswift.swiftandroid"
minSdk = 29
targetSdk = 35
versionCode = 1
versionName = "1.0"
ndk {
//noinspection ChromeOsAbiSupport
abiFilters += listOf("arm64-v8a")
}
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
buildFeatures {
compose = true
}
packaging {
resources {
excludes += listOf("/META-INF/{AL2.0,LGPL2.1}")
}
jniLibs {
keepDebugSymbols += listOf(
"*/arm64-v8a/*.so",
"*/armeabi-v7a/*.so",
"*/x86_64/*.so"
)
}
}
}

// Compile native Swift code for the demo app with `skip android build`.
val buildSwift by tasks.registering(Exec::class) {
group = "build"
description = "Build native Swift sources for Android"
workingDir(rootProject.projectDir)
commandLine("bash", "build-swift.sh")
}

tasks.named("preBuild") {
dependsOn(buildSwift)
}

dependencies {

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.recyclerview)
implementation(libs.androidx.navigation.runtime)
implementation(libs.androidx.material3)
implementation(libs.material)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
}
21 changes: 21 additions & 0 deletions Demo/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.pureswift.swiftandroid

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.pureswift.swiftandroid", appContext.packageName)
}
}
45 changes: 45 additions & 0 deletions Demo/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<!-- Android 12+ (API 31+) -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

<!-- Legacy support for Android 11 and below -->
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="30" />

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />

<application
android:name=".Application"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SwiftAndroid"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.SwiftAndroid">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
27 changes: 27 additions & 0 deletions Demo/app/src/main/java/com/example/swift/HelloSubclass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

package com.example.swift;

public class HelloSubclass extends HelloSwift {
private String greeting;

public HelloSubclass(String greeting) {
this.greeting = greeting;
}

public void greetMe() {
super.greet(greeting);
}
}
64 changes: 64 additions & 0 deletions Demo/app/src/main/java/com/example/swift/HelloSwift.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

package com.example.swift;

import java.util.function.Predicate;

public class HelloSwift {
public double value;
public static double initialValue = 3.14159;
public String name = "Java";

static {
System.loadLibrary("SwiftAndroidApp");
}

public HelloSwift() {
this.value = initialValue;
}

public native int sayHello(int x, int y);
public native String throwMessageFromSwift(String message) throws Exception;

// To be called back by the native code
public double sayHelloBack(int i) {
System.out.println("And hello back from " + name + "! You passed me " + i);
return value;
}

public void greet(String name) {
System.out.println("Salutations, " + name);
}

public Predicate<Integer> lessThanTen() {
Predicate<Integer> predicate = i -> (i < 10);
return predicate;
}

public String[] doublesToStrings(double[] doubles) {
int size = doubles.length;
String[] strings = new String[size];

for(int i = 0; i < size; i++) {
strings[i] = "" + doubles[i];
}

return strings;
}

public void throwMessage(String message) throws Exception {
throw new Exception(message);
}
}
22 changes: 22 additions & 0 deletions Demo/app/src/main/java/com/example/swift/ThreadSafe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

package com.example.swift;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface ThreadSafe {
}
Loading