मुख्य सामग्री पर जाएँ
Skillpedia
मोबाइल ऐप25k · #10065

jni-zero

यह क्या करती है

यह स्किल आपके AI को JNI Zero में मार्गदर्शन देती है, जो Java और C++ के बीच कॉल के लिए कोड बनाने वाला टूल है। यह उन Android डेवलपर्स के लिए है जो ऐसे bridges लिखते हैं।

कब इस्तेमाल करें

  • ✓जब आप Android कोड में generate_jni target या @CalledByNative method जोड़ते हैं।
  • ✓जब आप C++ कोड लिखते हैं जो जनरेट किए गए JNI headers से Java को कॉल करता है।
  • ✓जब आपको जाँचना है कि जनरेट किए गए JNI headers कैसे शामिल और इस्तेमाल होने चाहिए।

Skill का लिंक

https://github.com/chromium/chromium/blob/main/third_party/jni_zero/skills/jni-zero/SKILL.md

इसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।

कैसे इस्तेमाल करें

सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।

पेस्ट करने के लिए तैयार मैसेज

इस skill से मेरी मदद करो: https://github.com/chromium/chromium/blob/main/third_party/jni_zero/skills/jni-zero/SKILL.md इस लिंक की SKILL.md फ़ाइल पढ़ो और मेरे काम में उसके निर्देशों का पालन करो।
क्या आप चाहते हैं कि आपका AI ऐसी skills खुद ढूँढे?मुफ़्त Skillpedia MCP इंस्टॉल करें: बस एक लाइन, और हर काम से पहले यह 15,000+ skills में खोजता है।1 मिनट में इंस्टॉल करें →

AI से क्या माँगें: उदाहरण

“क्या आप एक JNI Zero binding जोड़ सकते हैं, ताकि मेरा Java method C++ कोड को कॉल कर सके?”

हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।

यह कैसे काम करती है

स्किल AI को JNI Zero के build और कोडिंग नियम देती है, जैसे नामकरण, header includes और सुरक्षित प्रकार। फिर 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 _jni as a suffix for all generate_jni() and generate_jar_jni() targets.
  • generate_jni() targets should be in the same BUILD.gn as the android_library() that owns the sources.
  • Ensure that all android_library() targets that have a .java file 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_headers prefix when #include'ing them.
  • #includes of _jni.h headers from .cc files 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.h headers from .h files should always use the _shared_jni.h variants.

API Usage:

  • When returning or passing a null JavaRef, use nullptr rather than calling a constructor.
  • Prefer type-safe JArray<JFoo> over jobjectArray / JArray<jobject>, as well as for other array types (JArray<T> over jTArray).
  • Use the jni_zero:: namespace instead of the base::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>, use Java_Clazz_method()
  • To call member functions given a JavaRef<JFoo>, use foo->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.h files
  • 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 null must be annotated as @Nullable to 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 calls delete via JNI, and sets the field 0.
  • 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 FromJniType or ToJniType to discover which types are currently supported.
  • Strings are either std::string or std::u16string.
  • Although std::optional can be used for @Nullable types, it always leads to larger binary size. Prefer empty containers and empty strings, and use std::optional only when empty and null are semantically different.
    • null will convert to an empty string or an empty container, so use @JniType("container or string") @Nullable List/String when 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> and ScopedJavaLocalRef<jobject> to use generated classes that extend jobject.
  • E.g., JavaRef<jobject> for a FooBar class becomes JavaRef<JFooBar>.
  • Special Cases:
    • java.lang.Object maps to jobject
    • java.lang.String maps to jstring
    • java.lang.Throwable maps to jthrowable
    • java.lang.Class maps to jclass
  • Use .As<JFoo>() to con
अक्सर पूछे जाने वाले सवाल

JNI क्या है?

JNI वह bridge है जो Android पर Java कोड को C++ कोड कॉल करने देता है। हाथ से लिखने पर इसमें गलती करना आसान है।

क्या मुझे कोई खास build सिस्टम चाहिए?

मार्गदर्शन GN build नियमों का उपयोग करता है, इसलिए यह GN से build होने वाले प्रोजेक्ट में फ़िट बैठता है।

क्या यह केवल Chromium के लिए है?

स्किल JNI Zero की Chromium वाली प्रति का ज़िक्र करती है, इसलिए उसके रास्ते उसी सेटअप से मेल खाते हैं।

जानकारी

लेखक का मूल विवरण

Guidance for working with JNI Zero to call between C++ and Java.

repo: chromium/chromium

लोकप्रियता

GitHub स्टार
25k
रैंकिंग में स्थान
#10065
लाइसेंस
BSD-3-Clause
अपडेट की गई
2/11/2026

क्या यह skill काम की है?

0

लॉग इन करें वोट करने के लिए।

कमेंट

  • …

इस repository की और skills

मिलती-जुलती skills