reisxd/tizentubecobalt/jni-zero
- community
Checking that the SKILL.md file is still in the repository.
Guidance for working with JNI Zero to call between C++ and Java.
Install
npx vskill@latest install reisxd/tizentubecobalt/jni-zeroSKILL.md
The same SKILL.md was published earlier as youtube/cobalt/jni-zero.
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 the
android_library() that owns the sources.
- Ensure that all
android_library()targets that have a.javafile that is
listed in a generate_jni() target also contain a deps entry onto the foo_jni_java target.
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.h variants.
API Usage:
- When returning or passing a null
JavaRef, usenullptrrather than calling
a constructor.
- Prefer
JArray<jobject>overjobjectArray, as well as for other array types
(JArray<*> over j*Array)
- Use the
jni_zero::namespace instead of thebase::android::aliases (e.g.
for *JavaRef, and AttachBaseContext())
- 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()
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
@Nullable to avoid a runtime null-check.
- A Java class owns a native object if it creates it via JNI.
- A native object owns a Java class if it creates it via JNI.
- If a Java class owns a native object, ensure that there is an
onDestroy()
method that calls delete via JNI, and sets the field 0.
- 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.
Guidance for @JniType
- Search all .h files for
FromJniTypeorToJniTypeto discover which types
are currently supported.
- Strings are either
std::stringorstd::u16string. - Use
std::optionalfor@Nullabletypes except when the type itself contains
a null-state (e.g. GURL, Callback, Strings where "" is not a valid value).
- 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 extend jobject.
- 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 convert fromJavaRef<jobject>to a specific generated
type.
- Convert
ScopedJavaLocalRef<jobject>()(empty constructor) tonullptr.
2. Method Calls
- Instance Methods: Replace
Java_MyClass_method(env, ref, ...)with
ref->method(env, ...).
- Static Methods: Replace
Java_MyClass_method(env, ...)with
JMyClassClass::method(env, ...).
- Methods taking
java.lang.Objectwill takejobject. If you have a
JavaRef<jobject>, you can pass it directly.
3. Namespaces and Aliases
- JNI types often have long namespaces (e.g.,
::org::chromium::base::JFoo). - If a
usingstatement isn't already in the_jni.hfile, add an alias at the
top of the .cc file.
- E.g.,
using ::org::chromium::base::JLogo;. - Avoid fully-qualified names in method signatures within
.ccfiles when an
alias is available.
4. Headers and Dependencies
- If a type
JFoois used in a.hfile: - Include
foo_shared_jni.h. - In the
BUILD.gn, add apublic_deponto thegenerate_jni()target that
produces it.
- Do NOT include
_shared_jni.hin.ccfiles; use regular_jni.hheaders
there.
- For system classes (
List,Set,Map): - Add a dependency on
//third_party/jni_zero:system_jni. - Use generated headers from that target.
Examples
See examples.md for concrete before/after snippets.
Validation
Build all affected .cc and .java files using autoninja:
Note: Use the specified `OUTPUT_DIR` (typically `out/Default` or `out/Debug`).
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingthird_party/jni_zero/skills/jni-zero/SKILL.md
Popularity
- Repository stars
- 3,290
- Repository forks
- 173
- 7-day trend score
- 0
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/reisxd/tizentubecobalt/jni-zero)