jni-zero
What it does
This skill guides your AI through JNI Zero, a tool that generates code for calling between Java and C++. It is for Android developers who write these bridges.
When to use it
- ✓When you add a generate_jni target or a @CalledByNative method to Android code.
- ✓When you write C++ code that calls into Java through generated JNI headers.
- ✓When you need to check how generated JNI headers should be included and used.
Skill link
https://github.com/chromium/chromium/blob/main/third_party/jni_zero/skills/jni-zero/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agent’s skills folder.
Example of what to ask your AI
“Can you add a JNI Zero binding so my Java method can call the C++ code?”
5 new skills a week. 1 minute to read.
How it works
The skill gives the AI the build and coding rules for JNI Zero, such as naming, header includes and safe types. The AI then applies them to the Java and C++ files.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
JNI Zero Guidance
JNI Zero is a code generator that lives at //third_party/jni_zero. It's
written in Python and has both C++ and Java APIs. The main reference is in
//third_party/jni_zero/README.md.
Guidance for GN
- Use
_jnias a suffix for allgenerate_jni()andgenerate_jar_jni()targets. generate_jni()targets should be in the sameBUILD.gnas theandroid_library()that owns the sources.- Ensure that all
android_library()targets that have a.javafile that is listed in agenerate_jni()target also contain adepsentry onto thefoo_jni_javatarget.
Guidance for C++
Generated headers:
- Are generated at
{OUTPUT_DIR}/gen/jni_headers/{BUILD_GN_SUBDIR}/{TARGET_NAME}/{JavaClass}_jni.h - Omit the
gen/jni_headersprefix when #include'ing them. - #includes of
_jni.hheaders from.ccfiles should come after all other #includes and be preceded with the comment:// Must come after headers that provide symbols used by @JniType.
- #includes of
_jni.hheaders from.hfiles should always use the_shared_jni.hvariants.
API Usage:
- When returning or passing a null
JavaRef, usenullptrrather than calling a constructor. - Prefer type-safe
JArray<JFoo>overjobjectArray/JArray<jobject>, as well as for other array types (JArray<T>overjTArray). - Use the
jni_zero::namespace instead of thebase::android::aliases (e.g. for*JavaRef, andAttachBaseContext()) - To call static Java methods, do not call the
Java_Clazz_method()functions directly. Call them through their typed wrappers:ClazzJni::method() - To call constructors methods, use
ClazzJni::New() - To call member functions given a
JavaRef<jobject>, useJava_Clazz_method() - To call member functions given a
JavaRef<JFoo>, usefoo->method()
Creation and ownership of Java counterparts:
- A native object owns a Java class if it creates it via JNI.
- If a native object owns a Java object, ensure that its destructor sets the Java object's native pointer field to 0 from its destructor.
- Add a comment explaining the ownership and lifecycle of the class and its Java
counterpart.
- E.g.: "This class creates and owns its Java counterpart, and holds a strong JNI global reference to it. When this is destroyed, it must call clearNativePtr to prevent Java from calling back into destroyed memory."
Defining native entry point methods:
- Use the signatures from the examples within the generated
_jni.hfiles - Omit the
JNIEnv*parameter if it will be unused.
Guidance for Java
- Name all @NativeMethods interfaces "Natives", and make them package-private.
- Use
@JniType()on as many parameters and return types as possible. - Parameters and return types that may be
nullmust be annotated as@Nullableto avoid a runtime null-check. - A Java class owns a native object if it creates it via JNI.
- If a Java class owns a native object, ensure that there is an
onDestroy()method that callsdeletevia JNI, and sets the field0. - Add a comment explaining the ownership and lifecycle of the class and its C++
counterpart.
- E.g.: "This class is created and owned by its C++ counterpart. It holds a reference to it in the field mNativePtr, which the C++ counterpart must set to 0 when it is destroyed by calling clearNativePtr."
Guidance for @JniType
- Search all .h files for
FromJniTypeorToJniTypeto discover which types are currently supported. - Strings are either
std::stringorstd::u16string. - Although
std::optionalcan be used for@Nullabletypes, it always leads to larger binary size. Prefer empty containers and empty strings, and usestd::optionalonly when empty and null are semantically different.nullwill convert to an empty string or an empty container, so use@JniType("container or string") @Nullable List/Stringwhen applicable.
- Prefer to use
List<>over typed object arrays.
Converting From jobject to Correctly-Typed jobject Subclasses
Core Steps
1. Type Conversion
- Change
JavaRef<jobject>andScopedJavaLocalRef<jobject>to use generated classes that extendjobject. - E.g.,
JavaRef<jobject>for aFooBarclass becomesJavaRef<JFooBar>. - Special Cases:
java.lang.Objectmaps tojobjectjava.lang.Stringmaps tojstringjava.lang.Throwablemaps tojthrowablejava.lang.Classmaps tojclass
- Use
.As<JFoo>()to con
FAQ
What is JNI?
JNI is the bridge that lets Java code call C++ code on Android. It is easy to get wrong when written by hand.
Do I need a specific build system?
The guidance uses GN build rules, so it fits projects that build with GN.
Is this only for Chromium?
The skill refers to Chromium's copy of JNI Zero, so its paths match that setup.
Details
Original description by the author
Guidance for working with JNI Zero to call between C++ and Java.
repo: chromium/chromium
Popularity
- GitHub stars
- 25k
- Ranking position
- #10065
- License
- BSD-3-Clause
- Updated
- 11/2/2026
Comments
- …
More skills from this repository
fuzzing
25kHelps your agent write fuzz tests that feed many random inputs into code to find crashes and bugs in C++ code.
gn-deps-debugging
25kHelps your agent fix build errors caused by missing or wrong dependencies in GN build files, without adding broad, unneeded ones.
java-memory-leaks
25kHelps your agent find and fix Java memory leaks in Android apps by reading leak reports from LeakCanary.
jni-type-conversion
25kHelps your agent replace manual Java-to-C++ conversion code with simpler @JniType annotations in Android code.
Similar skills
android-clean-architecture
280kMakes your agent organize Android projects efficiently. It structures the code to make maintenance and new feature development easier.
compose-multiplatform-patterns
280kHelps your agent use Compose Multiplatform and Jetpack Compose patterns in KMP projects, covering state, navigation, theming, and performance.
dart-flutter-patterns
280kMakes your agent write code for Dart and Flutter apps. It uses modern patterns to create robust and manageable apps.