Skip to content

Commit 8a40c84

Browse files
committed
Add NativeLibrary Kotlin class
1 parent 37a8273 commit 8a40c84

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

Demo/app/src/main/java/com/pureswift/swiftandroid/EmbeddedAndroidViewDemo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fun EmbeddedAndroidViewDemo() {
2828
//widget.ImageView
2929
AndroidView(factory = { ctx ->
3030
ImageView(ctx).apply {
31-
val drawable = ContextCompat.getDrawable(ctx, R.drawable.ic_launcher_background)
31+
val drawable = ContextCompat.getDrawable(ctx, R.drawable.ic_launcher_foreground)
3232
setImageDrawable(drawable)
3333
}
3434
})

Demo/app/src/main/java/com/pureswift/swiftandroid/MainActivity.kt

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ import com.pureswift.swiftandroid.ui.theme.SwiftAndroidTheme
1616
class MainActivity : ComponentActivity() {
1717

1818
init {
19-
loadNativeLibrary()
20-
}
21-
22-
private fun loadNativeLibrary() {
23-
System.loadLibrary("Foundation")
24-
System.loadLibrary("SwiftAndroidApp")
19+
NativeLibrary.shared()
2520
}
2621

2722
override fun onCreate(savedInstanceState: Bundle?) {
@@ -44,11 +39,7 @@ class MainActivity : ComponentActivity() {
4439
class MainActivityHello {
4540

4641
init {
47-
loadNativeLibrary()
48-
}
49-
50-
private fun loadNativeLibrary() {
51-
System.loadLibrary("SwiftAndroidApp")
42+
NativeLibrary.shared()
5243
}
5344

5445
external fun sayHelloName(): String
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.pureswift.swiftandroid
2+
3+
import android.util.Log
4+
5+
class NativeLibrary private constructor() {
6+
7+
companion object {
8+
9+
@Volatile
10+
var shared : NativeLibrary? = null
11+
12+
fun shared(): NativeLibrary {
13+
return shared?: synchronized(this){
14+
val instance = NativeLibrary()
15+
shared = instance
16+
return instance
17+
}
18+
}
19+
}
20+
21+
init {
22+
loadNativeLibrary()
23+
}
24+
25+
private fun loadNativeLibrary() {
26+
try {
27+
System.loadLibrary("SwiftAndroidApp")
28+
} catch (error: UnsatisfiedLinkError) {
29+
Log.e("NativeLibrary", "Unable to load native libraries: $error")
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)