### Setup JNI Base Library and Generated Package Source: https://pub.dev/packages/jnigen/example Build native libraries for the JNI base library and JNIgen generated package using the jni:setup command. ```bash dart run jni:setup ``` -------------------------------- ### Build Android APK for Plugin Source: https://pub.dev/packages/jnigen/example Build the Android example project for an FFI plugin using the flutter build apk command. ```bash flutter build apk ``` -------------------------------- ### Programmatic API Setup Source: https://pub.dev/packages/jnigen/versions/0.10.0 Import the jnigen package and construct a Config object to generate JNI bindings programmatically. This offers an alternative to YAML configuration. ```dart import 'package:jnigen/jnigen.dart'; // Construct a Config object and pass it to generateJniBindings // Example parameters would be similar to YAML options. ``` -------------------------------- ### Dart Script for Programmatic Configuration Source: https://pub.dev/packages/jnigen/versions/0.9.2 Example of how to create a tool script in Dart to use jnigen's programmatic API for configuration. This approach allows for dynamic generation of JNI bindings. ```dart import 'package:jnigen/jnigen.dart'; void main() { generateJniBindings( // Configuration options similar to YAML ); } ``` -------------------------------- ### jnigen YAML Configuration Example Source: https://pub.dev/packages/jnigen/versions/0.10.0 A sample YAML configuration file for jnigen. This specifies input sources, output paths, and the classes for which to generate Dart bindings, including Gradle dependencies for Android. ```yaml android_sdk_config: add_gradle_deps: true output: dart: path: lib/android_utils.dart structure: single_file source_path: - 'android/app/src/main/java' classes: - 'com.example.in_app_java.AndroidUtils' ``` -------------------------------- ### Example Java File for Android Source: https://pub.dev/packages/jnigen/versions/0.10.0 A simple Java utility class designed for use within a Flutter Android application. It includes a method to display a toast message. ```java package com.example.in_app_java; import android.app.Activity; import android.widget.Toast; import androidx.annotation.Keep; @Keep public abstract class AndroidUtils { // Hide constructor private AndroidUtils() {} public static void showToast(Activity mainActivity, CharSequence text, int duration) { mainActivity.runOnUiThread(() -> Toast.makeText(mainActivity, text, duration).show()); } } ``` -------------------------------- ### Configure jnigen for Built-in Types Source: https://pub.dev/packages/jnigen/versions/0.14.2 Example YAML configuration to generate bindings for built-in Java types like java.lang.Math. It requires providing the path to the extracted 'java.base' folder from OpenJDK source code. ```yaml source_path: - '/path/to/extracted/java.base' classes: - 'java.lang.Math' ``` -------------------------------- ### Dart Script for Generating JNI Bindings Source: https://pub.dev/packages/jnigen/versions/0.12.1 Example of a Dart script that uses the JNIGen programmatic API to generate JNI bindings. This approach is an alternative to using a YAML configuration file. ```dart import "package:jnigen/jnigen.dart"; void main() { final config = Config( // Example configuration parameters (replace with actual values) // excludeMethods: ['com.example.MyClass#myMethod'], // excludeFields: ['com.example.MyClass#myField'], ); generateJniBindings(config: config); } ``` -------------------------------- ### Run JNIGen with YAML Configuration Source: https://pub.dev/packages/jnigen/versions/0.10.0 Invoke JNIGen using a specified YAML configuration file. ```bash dart run jnigen --config jnigen.yaml ``` -------------------------------- ### Generate C Bindings for AndroidUtils Source: https://pub.dev/packages/jnigen/versions/0.8.0 This C code snippet demonstrates how to call a static Java method 'showToast' from the 'com.example.in_app_java.AndroidUtils' class using JNI. It includes boilerplate for loading the JNI environment, class, and method, along with error checking. ```c // Some boilerplate is omitted for clarity. // com.example.in_app_java.AndroidUtils jclass _c_AndroidUtils = NULL; jmethodID _m_AndroidUtils__showToast = NULL; FFI_PLUGIN_EXPORT JniResult AndroidUtils__showToast(jobject mainActivity, jobject text, int32_t duration) { load_env(); load_class_gr(&_c_AndroidUtils, "com/example/in_app_java/AndroidUtils"); if (_c_AndroidUtils == NULL) return (JniResult){.result = {.j = 0}, .exception = check_exception()}; load_static_method(_c_AndroidUtils, &_m_AndroidUtils__showToast, "showToast", "(Landroid/app/Activity;Ljava/lang/CharSequence;I)V"); if (_m_AndroidUtils__showToast == NULL) return (JniResult){.result = {.j = 0}, .exception = check_exception()}; (*jniEnv)->CallStaticVoidMethod(jniEnv, _c_AndroidUtils, _m_AndroidUtils__showToast, mainActivity, text, duration); return (JniResult){.result = {.j = 0}, .exception = check_exception()}; } ``` -------------------------------- ### Generate JNI Bindings (Dart Package) Source: https://pub.dev/packages/jnigen/example Generate JNI bindings for a Dart package by running the jnigen command with a configuration file. ```bash dart run jnigen --config jnigen.yaml ``` -------------------------------- ### Setting JAVA_HOME Path on Windows Source: https://pub.dev/packages/jnigen/versions/0.13.0 This snippet demonstrates how to append the JVM DLL path to the system's PATH environment variable on Windows using PowerShell. This is necessary for Java tooling to function correctly. ```powershell $env:Path += ";${env:JAVA_HOME}\bin\server" ``` -------------------------------- ### Enable Pure Dart Bindings Source: https://pub.dev/packages/jnigen/versions/0.8.0 To generate bindings that do not rely on an intermediate layer of C code, specify this configuration. Any C output configuration will be ignored. ```yaml output: bindings_type: dart_only ``` -------------------------------- ### Override JNIGen Configuration via Command Line Source: https://pub.dev/packages/jnigen/versions/0.13.0 Override specific configuration properties using the -D or --override flags. ```bash dart run jnigen -Dlog_level=warning ``` ```bash dart run jnigen --override summarizer.backend=asm ``` -------------------------------- ### Override Configuration with Command Line Flags Source: https://pub.dev/packages/jnigen/versions/0.15.0 Override specific configuration settings from the YAML file using command-line flags. Use the '-D' or '--override' switch followed by the configuration property and its new value. ```bash dart run jnigen --override log_level=warning dart run jnigen --override summarizer.backend=asm ``` -------------------------------- ### Setting JAVA_HOME Environment Variable (Powershell) Source: https://pub.dev/packages/jnigen/versions/0.10.0 This snippet demonstrates how to append the JDK's server directory to the PATH environment variable in Powershell. This is often necessary for Java tooling to function correctly. ```powershell $env:Path += ";${env:JAVA_HOME}\bin\server". ``` -------------------------------- ### Add jnigen to Flutter Project Source: https://pub.dev/packages/jnigen/install Use this command to add jnigen as a dependency in your Flutter project. ```bash $ flutter pub add jnigen ``` -------------------------------- ### jnigen Package License Source: https://pub.dev/packages/jnigen/license This is the standard BSD 3-Clause license for the jnigen package. It outlines the terms for redistribution and use in source and binary forms. ```text Copyright 2022, the Dart project authors. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Google LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Code generated by JNIgen is owned by the owner of the input file used when generating it. This code is not standalone and requires a support library to be linked with it. This support library is itself covered by the above license. ``` -------------------------------- ### Add jnigen to Dart Project Source: https://pub.dev/packages/jnigen/install Use this command to add jnigen as a dependency in your Dart project. ```bash $ dart pub add jnigen ``` -------------------------------- ### jnigen Configuration for Android SDK Source: https://pub.dev/packages/jnigen/versions/0.8.0 This YAML configuration specifies settings for jnigen, including adding Gradle dependencies, defining output paths for C and Dart code, and listing the Java classes to be processed. It's used to generate JNI bindings for an Android project. ```yaml android_sdk_config: add_gradle_deps: true output: c: library_name: android_utils path: src/android_utils/ dart: path: lib/android_utils.dart structure: single_file source_path: - 'android/app/src/main/java' classes: - 'com.example.in_app_java.AndroidUtils' ``` -------------------------------- ### Configure jnigen Bindings Generation Source: https://pub.dev/packages/jnigen This Dart script configures jnigen to generate bindings for Java code. It specifies the output path for the generated Dart file, the location of the Java source files, and the classes to generate bindings for. It also configures jnigen to search for Android SDK libraries. ```dart import 'dart:io'; import 'package:jnigen/jnigen.dart'; void main(List args) { final packageRoot = Platform.script.resolve('../'); generateJniBindings( Config( outputConfig: OutputConfig( dartConfig: DartCodeOutputConfig( // Required. Output path for generated bindings. path: packageRoot.resolve('lib/android_utils.g.dart'), // Optional. Write bindings into a single file (instead of one file per class). structure: OutputStructure.singleFile, ), ), // Optional. Configuration to search for Android SDK libraries. androidSdkConfig: AndroidSdkConfig(addGradleDeps: true), // Optional. List of directories that contain the source files for which to generate bindings. sourcePath: [packageRoot.resolve('android/app/src/main/java')], // Required. List of classes or packages for which bindings should be generated. classes: ['com.example.in_app_java'], ), ); } ```