jni-zero
Was er macht
Dieser Skill führt deine KI durch JNI Zero, ein Werkzeug, das Code für Aufrufe zwischen Java und C++ erzeugt. Er ist für Android-Entwickler gedacht, die solche Brücken schreiben.
Wann du ihn nutzt
- ✓Wenn du ein generate_jni-Ziel oder eine @CalledByNative-Methode zu Android-Code hinzufügst.
- ✓Wenn du C++-Code schreibst, der über erzeugte JNI-Header nach Java aufruft.
- ✓Wenn du prüfen musst, wie erzeugte JNI-Header eingebunden und genutzt werden.
Link zum Skill
https://github.com/chromium/chromium/blob/main/third_party/jni_zero/skills/jni-zero/SKILL.mdKopiere ihn und füge ihn in deine KI ein (Claude, ChatGPT oder Cursor) mit der Bitte, den Anweisungen zu folgen.
So nutzt du ihn
Am einfachsten: Kopiere die Nachricht und füge sie in deine KI ein. Sie liest den Skill und folgt den Anweisungen.
Fertige Nachricht zum Einfügen
Für Claude Code, Codex oder Cursor: Führe den Befehl in deinem Projektordner aus, dann wird der Skill installiert.
Lade die SKILL.md herunter und lege sie in den Skills-Ordner deines Agenten.
Beispielanfrage an deine KI
“Kannst du eine JNI-Zero-Bindung hinzufügen, damit meine Java-Methode den C++-Code aufrufen kann?”
5 neue Skills pro Woche. 1 Minute Lesezeit.
So funktioniert es
Der Skill gibt der KI die Build- und Codierregeln für JNI Zero, etwa Namensgebung, Header-Einbindung und sichere Typen. Die KI wendet sie dann auf die Java- und C++-Dateien an.
Technischer Inhalt (originale SKILL.md)
Originaltext des Autors, auf Englisch, direkt von 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
Häufige Fragen
Was ist JNI?
JNI ist die Brücke, mit der Java-Code auf Android C++-Code aufrufen kann. Von Hand ist sie leicht falsch zu schreiben.
Brauche ich ein bestimmtes Build-System?
Die Anleitung nutzt GN-Build-Regeln, passt also zu Projekten, die mit GN bauen.
Gilt das nur für Chromium?
Der Skill bezieht sich auf die Chromium-Version von JNI Zero, sodass seine Pfade zu diesem Aufbau passen.
Details
Originalbeschreibung des Autors
Guidance for working with JNI Zero to call between C++ and Java.
repo: chromium/chromium
Beliebtheit
- GitHub-Sterne
- 25k
- Platz im Ranking
- #10065
- Lizenz
- BSD-3-Clause
- Aktualisiert
- 2.11.2026
Kommentare
- …
Weitere Skills aus diesem Repository
fuzzing
25kHilft deinem Agenten, Fuzz-Tests zu schreiben, die viele zufällige Eingaben in Code einspeisen, um Abstürze und Fehler in C++-Code zu finden.
gn-deps-debugging
25kHilft deinem Agenten, Build-Fehler zu beheben, die durch fehlende oder falsche Abhängigkeiten in GN-Build-Dateien entstehen, ohne breite, unnötige Abhängigkeiten hinzuzufügen.
java-memory-leaks
25kHilft deinem Agenten, Java-Speicherlecks in Android-Apps zu finden und zu beheben, indem er Lecks-Berichte von LeakCanary liest.
jni-type-conversion
25kHilft deinem Agenten, manuellen Java-zu-C++-Umwandlungscode durch einfachere @JniType-Annotationen in Android-Code zu ersetzen.
Ähnliche Skills
android-clean-architecture
280kLässt deinen Agenten Android-Projekte effizient strukturieren. Der Code wird so organisiert, dass Wartung und neue Features leichter werden.
compose-multiplatform-patterns
280kHilft deinem Agenten, Compose Multiplatform und Jetpack Compose Patterns in KMP-Projekten einzusetzen. Abgedeckt sind State, Navigation, Theming und Performance.
dart-flutter-patterns
280kLässt deinen Agenten Code für Dart- und Flutter-Apps schreiben. Er nutzt moderne Patterns für robuste und gut wartbare Apps.