jni-zero
できること
このスキルは、JavaとC++を呼び出すコードを生成するツールJNI Zeroについて、AIを導きます。こうしたブリッジを書くAndroidの開発者向けです。
使う場面
- ✓AndroidのコードにgenerateJniのターゲットや@CalledByNativeのメソッドを追加するとき。
- ✓生成されたJNIのヘッダーを通してJavaを呼び出すC++のコードを書くとき。
- ✓生成されたJNIのヘッダーをどうインクルードして使うべきかを確認したいとき。
スキルのリンク
https://github.com/chromium/chromium/blob/main/third_party/jni_zero/skills/jni-zero/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“JavaのメソッドがC++のコードを呼び出せるように、JNI Zeroのバインディングを追加してもらえますか。”
毎週5つの新しいスキル。1分で読めます。
仕組み
このスキルは、命名、ヘッダーのインクルード、安全な型などのJNI ZeroのビルドとコーディングのルールをAIに与えます。AIはそれを、JavaとC++のファイルに適用します。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)を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
よくある質問
JNIとは何ですか?
AndroidでJavaのコードからC++のコードを呼び出すためのブリッジです。手で書くと間違えやすいものです。
特定のビルドシステムが必要ですか?
この案内はGNのビルドのルールを使うので、GNでビルドするプロジェクトに合います。
Chromium専用ですか?
このスキルは、ChromiumにあるJNI Zeroのコピーを参照するため、そのパスの構成に合っています。
詳細
人気
- GitHubのスター
- 25k
- ランキング
- #10065
- ライセンス
- BSD-3-Clause
- 更新日
- 2026/11/2
コメント
- …
このリポジトリの他のスキル
fuzzing
25kエージェントが、多くのランダムな入力をコードに与えてクラッシュやバグを見つけるファジングテストを、C++のコードで書けるよう支援します。
gn-deps-debugging
25kエージェントが、GNのビルドファイルにある依存関係の不足や誤りによるビルドエラーを、広すぎる不要な依存関係を追加せずに直せるよう支援します。
java-memory-leaks
25kエージェントが、LeakCanaryのリークレポートを読んで、Androidアプリの Javaのメモリリークを見つけ、修正できるよう支援します。
jni-type-conversion
25kエージェントが、Androidのコードで、手作業によるJavaからC++への変換のコードを、より簡単な@JniTypeアノテーションに置き換えられるよう支援します。
似ているスキル
android-clean-architecture
280kエージェントがAndroidプロジェクトを効率よく整理できるようにします。コードを構造化して、保守や新機能の開発をしやすくします。
compose-multiplatform-patterns
280kエージェントがKMPプロジェクトでCompose MultiplatformとJetpack Composeのパターンを使えるようにします。状態管理、ナビゲーション、テーマ設定、パフォーマンスを扱います。
dart-flutter-patterns
280kエージェントがDartとFlutterのアプリ向けのコードを書けるようにします。最新のパターンを使い、堅牢で管理しやすいアプリを作ります。