jni-zero
Skill này làm gì
Skill này hướng dẫn AI của bạn qua JNI Zero, một công cụ sinh mã để gọi giữa Java và C++. Skill dành cho lập trình viên Android viết các cầu nối này.
Khi nào nên dùng
- ✓Khi bạn thêm target generate_jni hoặc phương thức @CalledByNative vào mã Android.
- ✓Khi bạn viết mã C++ gọi vào Java qua các header JNI được sinh ra.
- ✓Khi bạn cần kiểm tra cách các header JNI được sinh ra nên được include và dùng.
Liên kết skill
https://github.com/chromium/chromium/blob/main/third_party/jni_zero/skills/jni-zero/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Bạn có thể thêm một binding JNI Zero để phương thức Java của tôi gọi được mã C++ không?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill cung cấp cho AI các quy tắc build và viết mã của JNI Zero, như đặt tên, include header và các kiểu an toàn. Sau đó AI áp dụng chúng vào các file Java và C++.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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
Câu hỏi thường gặp
JNI là gì?
JNI là cầu nối cho phép mã Java gọi mã C++ trên Android. Dễ sai khi viết bằng tay.
Tôi có cần một hệ thống build cụ thể không?
Hướng dẫn dùng quy tắc build GN, nên phù hợp với các dự án build bằng GN.
Đây có chỉ dành cho Chromium không?
Skill tham chiếu đến bản sao JNI Zero của Chromium, nên đường dẫn của nó khớp với thiết lập đó.
Chi tiết
Mô tả gốc của tác giả
Guidance for working with JNI Zero to call between C++ and Java.
repo: chromium/chromium
Độ phổ biến
- Sao GitHub
- 25k
- Vị trí xếp hạng
- #10065
- Giấy phép
- BSD-3-Clause
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
fuzzing
25kGiúp agent của bạn viết các bài kiểm thử fuzz, đưa nhiều đầu vào ngẫu nhiên vào mã để tìm lỗi crash và bug trong mã C++.
gn-deps-debugging
25kGiúp agent của bạn sửa lỗi build do phụ thuộc thiếu hoặc sai trong file build GN, mà không thêm các phụ thuộc rộng và không cần thiết.
java-memory-leaks
25kGiúp agent của bạn tìm và sửa rò rỉ bộ nhớ Java trong ứng dụng Android bằng cách đọc báo cáo rò rỉ từ LeakCanary.
jni-type-conversion
25kGiúp agent của bạn thay thế mã chuyển đổi Java sang C++ thủ công bằng các chú thích @JniType đơn giản hơn trong mã Android.
Skill tương tự
android-clean-architecture
280kGiúp agent của bạn tổ chức dự án Android hiệu quả, cấu trúc code để việc bảo trì và phát triển tính năng mới dễ dàng hơn.
compose-multiplatform-patterns
280kGiúp agent của bạn dùng các mẫu Compose Multiplatform và Jetpack Compose trong dự án KMP, gồm trạng thái, điều hướng, giao diện và hiệu năng.
dart-flutter-patterns
280kGiúp agent của bạn viết code cho ứng dụng Dart và Flutter. Agent dùng các mẫu hiện đại để tạo ứng dụng vững chắc và dễ quản lý.