Skip to content

Commit 4015916

Browse files
committed
Add example Application
1 parent 4dd2d65 commit 4015916

File tree

7 files changed

+67
-40
lines changed

7 files changed

+67
-40
lines changed

Demo/app/src/main/java/com/example/swift/HelloSwift.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public HelloSwift() {
2929
this.value = initialValue;
3030
}
3131

32-
native int sayHello(int x, int y);
33-
native String throwMessageFromSwift(String message) throws Exception;
32+
public native int sayHello(int x, int y);
33+
public native String throwMessageFromSwift(String message) throws Exception;
3434

3535
// To be called back by the native code
3636
public double sayHelloBack(int i) {

Demo/app/src/main/java/com/example/swift/JavaKitSampleMain.java

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.pureswift.swiftandroid;
2+
3+
import android.annotation.SuppressLint;
4+
import com.example.swift.HelloSubclass;
5+
6+
public class Application extends android.app.Application {
7+
8+
@SuppressLint("MissingSuperCall")
9+
@Override
10+
public native void onCreate();
11+
12+
@SuppressLint("MissingSuperCall")
13+
@Override
14+
public native void onTerminate();
15+
16+
public void sayHello() {
17+
int result = new HelloSubclass("Swift").sayHello(17, 25);
18+
System.out.println("sayHello(17, 25) = " + result);
19+
}
20+
}

Demo/swift/Package.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ let package = Package(
7676
swiftSettings: [
7777
.swiftLanguageMode(.v5),
7878
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
79-
],
80-
plugins: [
81-
.plugin(name: "JavaCompilerPlugin", package: "swift-java"),
82-
.plugin(name: "JExtractSwiftPlugin", package: "swift-java"),
83-
.plugin(name: "Java2SwiftPlugin", package: "swift-java"),
8479
]
8580
),
8681
]

Demo/swift/Sources/SwiftAndroidApp/App.swift

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// SwiftApp.swift
3+
// SwiftAndroidApp
4+
//
5+
// Created by Alsey Coleman Miller on 6/8/25.
6+
//
7+
8+
import AndroidKit
9+
10+
@JavaClass("com.pureswift.swiftandroid.Application")
11+
class Application: AndroidApp.Application {
12+
13+
@JavaMethod
14+
func sayHello()
15+
16+
@JavaMethod
17+
override func onCreate() {
18+
super.onCreate()
19+
20+
print("\(#function)")
21+
22+
sayHello()
23+
}
24+
25+
@JavaMethod
26+
override func onTerminate() {
27+
super.onTerminate()
28+
29+
print("\(#function)")
30+
}
31+
}
32+
33+
@JavaImplementation("com.pureswift.swiftandroid.Application")
34+
extension Application { }
35+
36+
internal extension Application {
37+
38+
func print(_ string: String) {
39+
let log = try! JavaClass<AndroidUtil.Log>()
40+
_ = log.v("Application", string)
41+
}
42+
}

Demo/swift/Sources/SwiftAndroidApp/swift-java.config

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"classes" : {
33
"com.example.swift.HelloSwift" : "HelloSwift",
44
"com.example.swift.HelloSubclass" : "HelloSubclass",
5-
"com.example.swift.ThreadSafeHelperClass" : "ThreadSafeHelperClass"
5+
"com.example.swift.ThreadSafeHelperClass" : "ThreadSafeHelperClass",
6+
"com.pureswift.swiftandroid.Application": "SwiftApplication"
67
},
7-
"classpath" : "android.jar:kotlin-classes",
8+
"classpath" : "android.jar:java-classes:kotlin-classes",
89
}

0 commit comments

Comments
 (0)