You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[jnigen] Improve the UX of Jni.spawn for Dart standalone (#1916)
* Clarify setup for jnigen on Windows - Add section for generating java types
* Do not require setting `dylibDir` explicitly when running `Jni.spawn`
* Improve docs and error messages
Co-authored-by: Hossein Yousefi <[email protected]>
Copy file name to clipboardExpand all lines: pkgs/jnigen/README.md
+23-3Lines changed: 23 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -135,7 +135,9 @@ More advanced features such as callbacks are not supported yet. Support for thes
135
135
136
136
On Flutter targets, native libraries are built automatically and bundled. On standalone platforms, no such infrastructure exists yet. As a stopgap solution, running `dart run jni:setup` in a target directory builds all JNI native dependencies of the package into `build/jni_libs`.
137
137
138
-
The build directory has to be passed to `Jni.spawn` call. It's assumed that all dependencies are built into the same target directory, so that once JNI is initialized, generated bindings can load their respective C libraries automatically.
138
+
To start a JVM, call `Jni.spawn`. It's assumed that all dependencies are built into the same target directory, so that once JNI is initialized, generated bindings can load their respective C libraries automatically.
139
+
140
+
If a custom build path has been set for the dynamic libraries built by `dart run jni:setup --build-path path/to/dylib`, the same path must be passed to `Jni.spawn`. Also anytime a new Dart isolate is spawned, the directory must be set again using `Jni.setDylibDir`.
139
141
140
142
## Requirements
141
143
### SDK
@@ -154,9 +156,11 @@ __On windows, append the path of `jvm.dll` in your JDK installation to PATH.__
154
156
For example, on Powershell:
155
157
156
158
```powershell
157
-
$env:Path += ";${env:JAVA_HOME}\bin\server".
159
+
$env:Path += ";${env:JAVA_HOME}\bin\server"
158
160
```
159
161
162
+
Note: The above will only add `jvm.dll` to `PATH` for the current Powershell session, use the Control Panel to add it to path permanently.
163
+
160
164
If JAVA_HOME not set, find the `java.exe` executable and set the environment variable in Control Panel. If java is installed through a package manager, there may be a more automatic way to do this. (Eg: `scoop reset`).
161
165
162
166
### C tooling
@@ -179,6 +183,22 @@ If the classes are in JAR file, make sure to provide path to JAR file itself, an
179
183
#### `jnigen` is unable to parse sources.
180
184
If the errors are similar to `symbol not found`, ensure all dependencies of the source are available. If such dependency is compiled, it can be included in `class_path`.
181
185
186
+
#### Generate bindings for built-in types
187
+
To generate bindings for built-in Java types (like those in `java.*` packages), you need to provide the OpenJDK source code to the generator. Here's how:
188
+
189
+
1. Locate the `src.zip` file in your Java installation directory
190
+
2. Extract the `java.base` folder from this zip file
191
+
3. Add the path to the extracted `java.base` folder in your `source_path` list in the YAML configuration
192
+
193
+
For example, to generate bindings for `java.lang.Math`, your configuration might look like:
194
+
195
+
```yaml
196
+
source_path:
197
+
- '/path/to/extracted/java.base'
198
+
classes:
199
+
- 'java.lang.Math'
200
+
```
201
+
182
202
#### How are classes mapped into bindings?
183
203
Each Java class generates a subclass of `JObject` class, which wraps a `jobject` reference in JNI. Nested classes use `_` as separator, `Example.NestedClass` will be mapped to `Example_NestedClass`.
184
204
@@ -248,7 +268,7 @@ Currently we don't have an automatic mechanism for using these. You can unpack t
248
268
However there are 2 caveats to this caveat.
249
269
250
270
* SDK stubs after version 28 are incomplete. OpenJDK Doclet API we use to generate API summaries will error on incomplete sources.
251
-
* The API can't process the `java.**` namespaces in the Android SDK stubs, because it expects a module layout. So if you want to generate bindings for, say, `java.lang.Math`, you cannot use the Android SDK stubs. OpenJDK sources can be used instead.
271
+
* The API can't process the `java.**` namespaces in the Android SDK stubs, because it expects a module layout. So if you want to generate bindings for, say, `java.lang.Math`, you cannot use the Android SDK stubs. OpenJDK sources can be used instead. See [Generate bindings for built-in types](#generate-bindings-for-built-in-types) above for instructions on how to use OpenJDK sources.
252
272
253
273
The JAR files (`$SDK_ROOT/platforms/android-$VERSION/android.jar`) can be used instead. But compiled JARs do not include JavaDoc and method parameter names. This JAR is automatically included by Gradle when `android_sdk_config` >> `add_gradle_deps` is specified.
0 commit comments