본문으로 건너뛰기
Skillpedia
모바일 앱25k · #10065

jni-zero

하는 일

이 스킬은 Java와 C++ 사이 호출 코드를 생성하는 도구인 JNI Zero를 AI가 다루도록 안내합니다. 이런 브리지를 작성하는 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에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/chromium/chromium/blob/main/third_party/jni_zero/skills/jni-zero/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

AI에게 요청하는 예시

“제 Java 메서드가 C++ 코드를 호출할 수 있도록 JNI Zero 바인딩을 추가해 주실 수 있나요?”

매주 새 스킬 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 _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란 무엇인가요?

Android에서 Java 코드가 C++ 코드를 호출하게 해 주는 브리지입니다. 손으로 쓰면 실수하기 쉽습니다.

특정 빌드 시스템이 필요한가요?

이 안내는 GN 빌드 규칙을 사용하므로, GN으로 빌드하는 프로젝트에 맞습니다.

Chromium 전용인가요?

이 스킬은 Chromium의 JNI Zero 사본을 참조하므로, 경로가 그 설정과 맞습니다.

상세 정보

작성자의 원본 설명

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

repo: chromium/chromium

인기도

GitHub 스타
25k
순위
#10065
라이선스
BSD-3-Clause
업데이트
2026. 11. 2.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬