Commit 26824953 authored by Mehran Mahmoudi's avatar Mehran Mahmoudi Committed by Commit Bot

Add C++ example to jni_generator/README.md

This adds a C++ example to the JNI doc as well as some changes to the
instructions for creating native functions.

Bug: 1066173
Change-Id: Ia84f038e009e5b0f7420f3c27a38a176bb34f537
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2128226Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Commit-Queue: Mehran Mahmoudi <mahmoudi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755008}
parent fb30c93b
...@@ -83,9 +83,13 @@ To add JNI to a class: ...@@ -83,9 +83,13 @@ To add JNI to a class:
depend on the location of the `generate_jni` BUILD rule that lists your Java depend on the location of the `generate_jni` BUILD rule that lists your Java
source code.) Only include this header from a single `.cc` file as the source code.) Only include this header from a single `.cc` file as the
header defines functions. That `.cc` must implement your native code by header defines functions. That `.cc` must implement your native code by
defining functions named `JNI_${OriginalClassName}_${UpperCamelCaseMethod}` defining non-member functions named `JNI_${OriginalClassName}_${UpperCamelCaseMethod}`
for static methods and member functions named `${OriginalClassName}::${UpperCamelCaseMethod}`
for non-static methods. Member functions need be declared in the header
file as well.
Example: Example:
#### Java
```java ```java
class MyClass { class MyClass {
// Cannot be private. Must be package or public. // Cannot be private. Must be package or public.
...@@ -93,27 +97,45 @@ class MyClass { ...@@ -93,27 +97,45 @@ class MyClass {
/* package */ interface Natives { /* package */ interface Natives {
void foo(); void foo();
double bar(int a, int b); double bar(int a, int b);
// Either the |ClassName| part of the |nativeClassName| parameter name must // Either the |MyClass| part of the |nativeMyClass| parameter name must
// match the native class name exactly, or the method annotation // match the native class name exactly, or the method annotation
// @NativeClassQualifiedName("ClassName") must be used. // @NativeClassQualifiedName("MyClass") must be used.
// //
// If the native class is nested, use // If the native class is nested, use
// @NativeClassQualifiedName("FooClassName::BarClassName") and call the // @NativeClassQualifiedName("FooClassName::BarClassName") and call the
// parameter |nativePointer|. // parameter |nativePointer|.
void nonStatic(long nativeClassName, NewStyle self); void nonStatic(long nativeMyClass);
} }
void callNatives() { void callNatives() {
// NewStyleJni is generated by the JNI annotation processor. // MyClassJni is generated by the JNI annotation processor.
// Storing NewStyleJni.get() in a field defeats some of the desired R8 // Storing MyClassJni.get() in a field defeats some of the desired R8
// optimizations, but local variables are fine. // optimizations, but local variables are fine.
Natives jni = NewStyleJni.get(); Natives jni = MyClassJni.get();
jni.foo(); jni.foo();
jni.bar(1,2); jni.bar(1,2);
jni.nonStatic(this, mNativePointer); jni.nonStatic(mNativePointer);
} }
} }
``` ```
#### C++
```c++
#include "base/android/jni_android.h"
#include "<path to BUILD.gn>/<generate_jni target name>/MyClass_jni.h"
class MyClass {
public:
void NonStatic(JNIEnv* env);
}
// Notice that unlike Java, function names are capitalized in C++.
// Static function names should follow this format and don't need to be declared.
void JNI_MyClass_Foo(JNIEnv* env) { ... }
void JNI_MyClass_Bar(JNIEnv* env, jint a, jint b) { ... }
// Member functions need to be declared.
void MyClass::NonStatic(JNIEnv* env) { ... }
```
**Using the 'native' keyword** **Using the 'native' keyword**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment