Skip to content
Skillpedia
Mobile apps25k · #10060

jni-type-conversion

What it does

This skill helps your AI simplify code that passes data between Java and C++ on Android. It is for developers who maintain JNI bridge code.

When to use it

  • ✓When you want to remove manual string or object conversion code between Java and C++.
  • ✓When a Java method with @NativeMethods or @CalledByNative takes a type that is converted by hand in C++.
  • ✓When you need to check whether a type already has a @JniType conversion defined.

Skill link

https://github.com/chromium/chromium/blob/main/agents/skills/jni-type-conversion/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/chromium/chromium/blob/main/agents/skills/jni-type-conversion/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute →

Example of what to ask your AI

“Can you replace the manual string conversion in my Java native method with a @JniType annotation?”

5 new skills a week. 1 minute to read.

How it works

The AI finds JNI methods that convert data by hand, checks whether a @JniType conversion already exists for that type, and then updates the Java and C++ code to use the annotation.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

JNI Type Conversion

This skill guides the process of replacing explicit JNI conversion logic (like ConvertJavaStringToUTF8) with @JniType annotations in Java and corresponding native types in C++.

You must first read the prerequisite skill: //third_party/jni_zero/skills/jni-zero.

Workflow

  1. Identify Candidates: Look for JNI methods (annotated with @NativeMethods or @CalledByNative) that take or return types that are currently being explicitly converted in C++.
    • Tip: Inspect every JavaRef (jobject, jstring, j*Array, etc.) parameter in the C++ implementation. Any parameter that is immediately run through a conversion/unwrapping function (such as content::WebContents::FromJavaWebContents(...), Profile::FromJavaObject(...), url::GURLAndroid::ToNativeGURL(...), TabAndroid::GetNativeTab(...), ConvertJavaStringToUTF8(...), etc.) is a prime candidate for @JniType.
  2. Discovery (CRITICAL): To see if a type already has a @JniType conversion defined, search the codebase for FromJniType or ToJniType definitions for that C++ type:
    rg -g "*.h" "\binline .*(From|To)JniType"
    
    If a conversion exists, note the header file where it is defined; you will need to include it from any C++ files that require the conversion.
  3. Common @JniType Types & C++ Conversions:
    • std::string / std::u16string (base/android/jni_string.h) <- ConvertJavaStringToUTF8, ConvertJavaStringToUTF16, ConvertUTF8ToJavaString, ConvertUTF16ToJavaString
    • std::vector<T> / std::optional<T> (third_party/jni_zero/default_conversions.h) <- JavaIntArrayToIntVector, JavaLongArrayToInt64Vector, AppendJavaStringArrayToStringVector, ToJavaArrayOfStrings, etc.
    • base::OnceClosure / base::OnceCallback<...> / base::RepeatingCallback<...> (base/android/callback_android.h) <- RunObjectCallbackAndroid, RunBooleanCallbackAndroid, RunIntCallbackAndroid, etc.
    • content::WebContents* (content/public/browser/web_contents.h) <- content::WebContents::FromJavaWebContents, GetJavaWebContents
    • content::RenderFrameHost* (content/public/browser/render_frame_host.h) <- content::RenderFrameHost::FromJavaRenderFrameHost, GetJavaRenderFrameHost
    • Profile* (chrome/browser/profiles/profile.h) <- Profile::FromJavaObject, GetJavaObject
    • TabAndroid* (chrome/browser/android/tab_android.h) <- TabAndroid::GetNativeTab, GetJavaObject
    • ui::WindowAndroid* (ui/android/window_android.h) <- ui::WindowAndroid::FromJavaWindowAndroid, GetJavaObject
    • GURL (url/android/gurl_android.h) <- url::GURLAndroid::ToNativeGURL, url::GURLAndroid::FromNativeGURL
    • url::Origin (url/origin.h) <- url::Origin::FromJavaObject, ToJavaObject
    • PrefService* (components/prefs/android/pref_service_android.h) <- PrefServiceAndroid::FromPrefServiceAndroid
    • signin::IdentityManager* (components/signin/public/identity_manager/identity_manager.h)
    • base::Token (base/android/token_android.h), base::UnguessableToken (base/android/unguessable_token_android.h), base::Uuid for java.util.UUID (base/uuid.h)
  4. Verify Constraints: Do NOT convert if:
    • The conversion is conditional (e.g., inside an if block that might skip it).
    • The conversion happens inside a lambda (e.g., TRACE_EVENT macros). Moving these to @JniType makes the conversion eager, which can impact performance.
  5. Annotate Java:
    • Add @JniType("cpp_type") to the parameter or return type.
    • Integer Types: References to int in @JniType should always be int32_t (e.g., @JniType("std::vector<int32_t>") instead of @JniType("std::vector<int>")).
    • Collections: Prefer List<T> over object arrays (T[]) for collection parameters and return types (e.g., @JniType("std::vector") List<SuggestedTabInfo> suggestedTabs instead of SuggestedTabInfo[]), especially when Java passes or stores it as a List (avoiding Arrays.asList(...)).
    • @Nullable Types:
      • For String parameters, @JniType("std::string") automatically converts Java null to C++ "". Prefer this over std::optional<std::string> unless the C++ logic specifically distinguishes between null and empty.
FAQ

What is JNI?

JNI is the bridge that lets Java code call C++ code on Android. It can be tricky to write by hand.

What does @JniType do?

It is an annotation that tells the tool how to convert a Java type into a C++ type automatically, so less code is needed.

Do I need to read anything before using this skill?

The skill says to read a related jni-zero guide first. Your AI should follow that guide before making changes.

Details

Original description by the author

How to use @JniType annotations for ergonomic JNI. Relevant for Java files that use @NativeMethods or @CalledByNative.

repo: chromium/chromium

Popularity

GitHub stars
25k
Ranking position
#10060
License
BSD-3-Clause
Updated
11/2/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills