jni-zero
它能做什么
这个技能指导你的 AI 使用 JNI Zero,这是一个为 Java 与 C++ 之间的调用生成代码的工具。适合编写这类桥接代码的 Android 开发者。
适用场景
- ✓当你在 Android 代码中添加 generate_jni 目标或 @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。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“能添加一个 JNI Zero 绑定,让我的 Java 方法调用 C++ 代码吗?”
每周 5 个新技能,1 分钟读完。
工作原理
技能为 AI 提供 JNI Zero 的构建和编码规则,例如命名、头文件包含和安全类型。然后 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?
JNI 是让 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帮你的智能体用更简单的 @JniType 注解,替换 Android 代码中手动的 Java 到 C++ 转换代码。