Skip to content

Commit

Permalink
Add C++ example to jni_generator/README.md
Browse files Browse the repository at this point in the history
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/+/2128226
Reviewed-by: Andrew Grieve <agrieve@chromium.org>
Commit-Queue: Mehran Mahmoudi <mahmoudi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755008}
  • Loading branch information
Mehran Mahmoudi authored and Commit Bot committed Mar 31, 2020
1 parent fb30c93 commit 2682495
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions base/android/jni_generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,37 +83,59 @@ To add JNI to a class:
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
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:
#### Java
```java
class MyClass {
// Cannot be private. Must be package or public.
@NativeMethods
/* package */ interface Natives {
void foo();
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
// @NativeClassQualifiedName("ClassName") must be used.
// @NativeClassQualifiedName("MyClass") must be used.
//
// If the native class is nested, use
// @NativeClassQualifiedName("FooClassName::BarClassName") and call the
// parameter |nativePointer|.
void nonStatic(long nativeClassName, NewStyle self);
void nonStatic(long nativeMyClass);
}

void callNatives() {
// NewStyleJni is generated by the JNI annotation processor.
// Storing NewStyleJni.get() in a field defeats some of the desired R8
// MyClassJni is generated by the JNI annotation processor.
// Storing MyClassJni.get() in a field defeats some of the desired R8
// optimizations, but local variables are fine.
Natives jni = NewStyleJni.get();
Natives jni = MyClassJni.get();
jni.foo();
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**
Expand Down

0 comments on commit 2682495

Please sign in to comment.