Skip to content

Commit aa19ec7

Browse files
committed
fix: Load native part of NitroImage in shared OnLoad.kt file
1 parent d72c596 commit aa19ec7

File tree

10 files changed

+78
-80
lines changed

10 files changed

+78
-80
lines changed

packages/nitrogen/src/autolinking/android/createHybridObjectInitializer.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { getBuildingWithGeneratedCmakeDefinition } from './createCMakeExtension.
1313

1414
export function createHybridObjectIntializer(): SourceFile[] {
1515
const cxxNamespace = NitroConfig.getCxxNamespace('c++')
16+
const cppLibName = NitroConfig.getAndroidCxxLibName()
17+
const javaNamespace = NitroConfig.getAndroidPackage('java/kotlin')
1618
const autolinkingClassName = `${NitroConfig.getAndroidCxxLibName()}OnLoad`
1719

1820
const jniRegistrations = getJNINativeRegistrations().map(
@@ -64,7 +66,7 @@ ${createFileMetadataString(`${autolinkingClassName}.hpp`)}
6466
namespace ${cxxNamespace} {
6567
6668
/**
67-
* Initializes the native (C++) part of ${NitroConfig.getAndroidCxxLibName()}, and autolinks all Hybrid Objects.
69+
* Initializes the native (C++) part of ${cppLibName}, and autolinks all Hybrid Objects.
6870
* Call this in your \`JNI_OnLoad\` function (probably inside \`cpp-adapter.cpp\`).
6971
* Example:
7072
* \`\`\`cpp (cpp-adapter.cpp)
@@ -110,7 +112,39 @@ int initialize(JavaVM* vm) {
110112
}
111113
112114
} // namespace ${cxxNamespace}
115+
`.trim()
113116

117+
const kotlinCode = `
118+
${createFileMetadataString(`${autolinkingClassName}.kt`)}
119+
120+
package ${javaNamespace}
121+
122+
import android.util.Log
123+
124+
internal class ${autolinkingClassName} {
125+
companion object {
126+
private const val TAG = "${autolinkingClassName}"
127+
private var didLoad = false
128+
/**
129+
* Initializes the native part of "${cppLibName}".
130+
* This method is idempotent and can be called more than once.
131+
*/
132+
@JvmStatic
133+
fun initializeNative() {
134+
if (didLoad) return
135+
try {
136+
Log.i(TAG, "Loading ${cppLibName} C++ library...")
137+
System.loadLibrary("${cppLibName}")
138+
Log.i(TAG, "Successfully loaded ${cppLibName} C++ library!")
139+
didLoad = true
140+
} catch (e: Error) {
141+
Log.e(TAG, "Failed to load ${cppLibName} C++ library! Is it properly installed and linked? " +
142+
"Is the name correct? (see \`CMakeLists.txt\`, at \`add_library(...)\`)", e)
143+
throw e
144+
}
145+
}
146+
}
147+
}
114148
`.trim()
115149

116150
return [
@@ -128,5 +162,12 @@ int initialize(JavaVM* vm) {
128162
platform: 'android',
129163
subdirectory: [],
130164
},
165+
{
166+
content: kotlinCode,
167+
language: 'kotlin',
168+
name: `${autolinkingClassName}.kt`,
169+
platform: 'android',
170+
subdirectory: ['kotlin', ...javaNamespace.split('.')],
171+
},
131172
]
132173
}

packages/nitrogen/src/syntax/kotlin/KotlinHybridObject.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export function createKotlinHybridObject(spec: HybridObjectSpec): SourceFile[] {
2121
.join('\n\n')
2222

2323
const javaPackage = NitroConfig.getAndroidPackage('java/kotlin')
24-
const cppLibName = NitroConfig.getAndroidCxxLibName()
2524

2625
let kotlinBase = spec.isHybridView ? 'HybridView' : 'HybridObject'
2726
if (spec.baseTypes.length > 0) {
@@ -91,17 +90,6 @@ abstract class ${name.HybridTSpec}: ${kotlinBase}() {
9190
9291
companion object {
9392
private const val TAG = "${name.HybridTSpec}"
94-
init {
95-
try {
96-
Log.i(TAG, "Loading ${cppLibName} C++ library...")
97-
System.loadLibrary("${cppLibName}")
98-
Log.i(TAG, "Successfully loaded ${cppLibName} C++ library!")
99-
} catch (e: Error) {
100-
Log.e(TAG, "Failed to load ${cppLibName} C++ library! Is it properly installed and linked? " +
101-
"Is the name correct? (see \`CMakeLists.txt\`, at \`add_library(...)\`)", e)
102-
throw e
103-
}
104-
}
10593
}
10694
}
10795
`.trim()

packages/react-native-nitro-image/android/src/main/java/com/margelo/nitro/image/NitroImagePackage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ public ReactModuleInfoProvider getReactModuleInfoProvider() {
2929
}
3030

3131
static {
32-
System.loadLibrary("NitroImage");
32+
NitroImageOnLoad.initializeNative();
3333
}
3434
}

packages/react-native-nitro-image/nitrogen/generated/android/kotlin/com/margelo/nitro/image/HybridBaseSpec.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,5 @@ abstract class HybridBaseSpec: HybridObject() {
5454

5555
companion object {
5656
private const val TAG = "HybridBaseSpec"
57-
init {
58-
try {
59-
Log.i(TAG, "Loading NitroImage C++ library...")
60-
System.loadLibrary("NitroImage")
61-
Log.i(TAG, "Successfully loaded NitroImage C++ library!")
62-
} catch (e: Error) {
63-
Log.e(TAG, "Failed to load NitroImage C++ library! Is it properly installed and linked? " +
64-
"Is the name correct? (see `CMakeLists.txt`, at `add_library(...)`)", e)
65-
throw e
66-
}
67-
}
6857
}
6958
}

packages/react-native-nitro-image/nitrogen/generated/android/kotlin/com/margelo/nitro/image/HybridChildSpec.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,5 @@ abstract class HybridChildSpec: HybridBaseSpec() {
5454

5555
companion object {
5656
private const val TAG = "HybridChildSpec"
57-
init {
58-
try {
59-
Log.i(TAG, "Loading NitroImage C++ library...")
60-
System.loadLibrary("NitroImage")
61-
Log.i(TAG, "Successfully loaded NitroImage C++ library!")
62-
} catch (e: Error) {
63-
Log.e(TAG, "Failed to load NitroImage C++ library! Is it properly installed and linked? " +
64-
"Is the name correct? (see `CMakeLists.txt`, at `add_library(...)`)", e)
65-
throw e
66-
}
67-
}
6857
}
6958
}

packages/react-native-nitro-image/nitrogen/generated/android/kotlin/com/margelo/nitro/image/HybridImageFactorySpec.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,5 @@ abstract class HybridImageFactorySpec: HybridObject() {
6666

6767
companion object {
6868
private const val TAG = "HybridImageFactorySpec"
69-
init {
70-
try {
71-
Log.i(TAG, "Loading NitroImage C++ library...")
72-
System.loadLibrary("NitroImage")
73-
Log.i(TAG, "Successfully loaded NitroImage C++ library!")
74-
} catch (e: Error) {
75-
Log.e(TAG, "Failed to load NitroImage C++ library! Is it properly installed and linked? " +
76-
"Is the name correct? (see `CMakeLists.txt`, at `add_library(...)`)", e)
77-
throw e
78-
}
79-
}
8069
}
8170
}

packages/react-native-nitro-image/nitrogen/generated/android/kotlin/com/margelo/nitro/image/HybridImageSpec.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,5 @@ abstract class HybridImageSpec: HybridObject() {
7575

7676
companion object {
7777
private const val TAG = "HybridImageSpec"
78-
init {
79-
try {
80-
Log.i(TAG, "Loading NitroImage C++ library...")
81-
System.loadLibrary("NitroImage")
82-
Log.i(TAG, "Successfully loaded NitroImage C++ library!")
83-
} catch (e: Error) {
84-
Log.e(TAG, "Failed to load NitroImage C++ library! Is it properly installed and linked? " +
85-
"Is the name correct? (see `CMakeLists.txt`, at `add_library(...)`)", e)
86-
throw e
87-
}
88-
}
8978
}
9079
}

packages/react-native-nitro-image/nitrogen/generated/android/kotlin/com/margelo/nitro/image/HybridTestObjectSwiftKotlinSpec.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -389,16 +389,5 @@ abstract class HybridTestObjectSwiftKotlinSpec: HybridObject() {
389389

390390
companion object {
391391
private const val TAG = "HybridTestObjectSwiftKotlinSpec"
392-
init {
393-
try {
394-
Log.i(TAG, "Loading NitroImage C++ library...")
395-
System.loadLibrary("NitroImage")
396-
Log.i(TAG, "Successfully loaded NitroImage C++ library!")
397-
} catch (e: Error) {
398-
Log.e(TAG, "Failed to load NitroImage C++ library! Is it properly installed and linked? " +
399-
"Is the name correct? (see `CMakeLists.txt`, at `add_library(...)`)", e)
400-
throw e
401-
}
402-
}
403392
}
404393
}

packages/react-native-nitro-image/nitrogen/generated/android/kotlin/com/margelo/nitro/image/HybridTestViewSpec.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,5 @@ abstract class HybridTestViewSpec: HybridView() {
7373

7474
companion object {
7575
private const val TAG = "HybridTestViewSpec"
76-
init {
77-
try {
78-
Log.i(TAG, "Loading NitroImage C++ library...")
79-
System.loadLibrary("NitroImage")
80-
Log.i(TAG, "Successfully loaded NitroImage C++ library!")
81-
} catch (e: Error) {
82-
Log.e(TAG, "Failed to load NitroImage C++ library! Is it properly installed and linked? " +
83-
"Is the name correct? (see `CMakeLists.txt`, at `add_library(...)`)", e)
84-
throw e
85-
}
86-
}
8776
}
8877
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
///
2+
/// NitroImageOnLoad.kt
3+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4+
/// https://github.com/mrousavy/nitro
5+
/// Copyright © 2025 Marc Rousavy @ Margelo
6+
///
7+
8+
package com.margelo.nitro.image
9+
10+
import android.util.Log
11+
12+
internal class NitroImageOnLoad {
13+
companion object {
14+
private const val TAG = "NitroImageOnLoad"
15+
private var didLoad = false
16+
/**
17+
* Initializes the native part of "NitroImage".
18+
* This method is idempotent and can be called more than once.
19+
*/
20+
@JvmStatic
21+
fun initializeNative() {
22+
if (didLoad) return
23+
try {
24+
Log.i(TAG, "Loading NitroImage C++ library...")
25+
System.loadLibrary("NitroImage")
26+
Log.i(TAG, "Successfully loaded NitroImage C++ library!")
27+
didLoad = true
28+
} catch (e: Error) {
29+
Log.e(TAG, "Failed to load NitroImage C++ library! Is it properly installed and linked? " +
30+
"Is the name correct? (see `CMakeLists.txt`, at `add_library(...)`)", e)
31+
throw e
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)