Jni type conversion

From nwjs/chromium.src · View interactive version

Jni type conversion is an AI agent skill from the GitHub repository nwjs/chromium.src. How to use @JniType annotations for ergonomic JNI. Relevant for Java files that use @NativeMethods or @CalledByNative.

How to use this skill

Skill file

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++.

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.

  1. 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:

```bash

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.

  1. Common @JniType Types & C++ Conversions:

ConvertJavaStringToUTF8, ConvertJavaStringToUTF16,

ConvertUTF8ToJavaString, ConvertUTF16ToJavaString

(third_party/jni_zero/default_conversions.h) \<-

JavaIntArrayToIntVector, JavaLongArrayToInt64Vector,

AppendJavaStringArrayToStringVector, ToJavaArrayOfStrings, etc.

base::RepeatingCallback<...> (base/android/callback_android.h) \<-

RunObjectCallbackAndroid, RunBooleanCallbackAndroid,

RunIntCallbackAndroid, etc.

content::WebContents::FromJavaWebContents, GetJavaWebContents

\<- content::RenderFrameHost::FromJavaRenderFrameHost,

GetJavaRenderFrameHost

Profile::FromJavaObject, GetJavaObject

TabAndroid::GetNativeTab, GetJavaObject

ui::WindowAndroid::FromJavaWindowAndroid, GetJavaObject

url::GURLAndroid::FromNativeGURL

ToJavaObject

PrefServiceAndroid::FromPrefServiceAndroid

(components/signin/public/identity_manager/identity_manager.h)

(base/android/unguessable_token_android.h), base::Uuid for

java.util.UUID (base/uuid.h)

  1. Verify Constraints: Do NOT convert if:

it).

these to @JniType makes the conversion eager, which can impact

performance.

  1. Annotate Java:

int32_t (e.g., @JniType("std::vector<int32_t>") instead of

@JniType("std::vector<int>")).

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(...)).

Java null to C++ "". Prefer this over std::optional<std::string>

unless the C++ logic specifically distinguishes between null and empty.

Java null to an empty vector. Prefer empty containers over

std::optional<> unless the C++ logic specifically distinguishes between

null and empty, since std::optional<> has larger binary size.

already converted to std::optional<T>, in which case use

@JniType("std::optional<...>") @Nullable.

  1. Update C++:

const std::string&, const std::vector<int32_t>&, base::OnceClosure).

temporary values (rvalues) that are forwarded to the C++ method. When the

C++ implementation transfers ownership or can avoid a copy with

std::move() (e.g., assigning into a struct field, container, or binding

into a callback), use a non-const rvalue reference parameter (e.g.,

std::vector<int64_t>&&, std::string&&) instead of const T&, and pass

it with std::move(). Note that && is not needed in the Java

@JniType("...") annotation.

but is no longer used after @JniType additions, it should be removed from

the C++ function signature.

parameter is usually unnecessary. Remove it from Java and C++ to reduce

boilerplate.

no longer have any uses (e.g.,

using base::android::ConvertStringToUTF8;).

generated _jni.h file.

functions:

(std::vector, std::optional, base::span).

  1. Validate (CRITICAL): Changes are INCOMPLETE until you have verified they

build. Build all .cc and .java files to ensure JNI generation and compilation

succeed.

autoninja -C OUTPUT_DIR ../../path/to/foo.cc^ ../../path/to/Foo.java^ ...

made.

Common Recipes

base::Uuid Handling

uuid.AsLowercaseString() (outgoing).

Collection Types (`std::vector` \<-> `List<T>` / `T[]`)

return types.

@CalledByNative and @NativeMethods parameters and return types.

Prefer:

@CalledByNative
private void onSuggestedTabsUpdated(@JniType("std::vector") List<SuggestedTabInfo> suggestedTabs) {
    mSuggestedTabsSupplier.set(suggestedTabs);
}

Over:

@CalledByNative
private void onSuggestedTabsUpdated(@JniType("std::vector") SuggestedTabInfo[] suggestedTabs) {
    mSuggestedTabsSupplier.set(Arrays.asList(suggestedTabs));
}

Examples

Callback Parameters

Java:

void zeroArg(@JniType("base::OnceClosure&&") Runnable c1);
void oneArg(@JniType("base::OnceCallback<void(int32_t)>&&") Callback<Integer> c2);
void twoArgs(@JniType("base::OnceCallback<void(bool, int32_t)>&&") Callback2<Boolean, Integer> c3);

C++:

#include "base/android/callback_android.h"
void JNI_MyClass_ZeroArg(base::OnceClosure&& callback) {
    std::move(callback).Run();
}
void JNI_MyClass_OneArg(base::OnceCallback<void(int32_t)>&& callback) {
    std::move(callback).Run(0);
}
void JNI_MyClass_TwoArgs(base::OnceCallback<void(bool, int32_t)>&& callback) {
    std::move(callback).Run(true, 67);
}
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.