1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_
6 #define BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_
12 #include "base/android/jni_android.h"
13 #include "base/basictypes.h"
18 // This file is used to:
19 // - document the best practices and guidelines on JNI usage.
20 // - ensure sample_for_tests_jni.h compiles and the functions declared in it
23 // Methods are called directly from Java (except RegisterJNI). More
24 // documentation in SampleForTests.java
26 // For C++ to access Java methods:
27 // - GYP Build must be configured to generate bindings:
31 // # An example target that will rely on JNI:
32 // 'target_name': 'foo',
33 // 'type': '<(component)',
34 // # ... normal sources, defines, deps.
35 // # For each jni generated .java -> .h header file in foo_jni_headers
36 // # target there will be a single .cc file here that includes it.
38 // # Add deps for JNI:
40 // ['OS == "android"', {
50 // # Create targets for JNI:
52 // ['OS == "android"', {
55 // 'target_name': 'foo_jni_headers',
58 // 'java/src/org/chromium/example/jni_generator/SampleForTests.java',
61 // 'jni_gen_package': 'foo',
63 // 'includes': [ '../../../build/jni_generator.gypi' ],
66 // 'target_name': 'foo_java',
69 // '../../../base/base.gyp:base',
72 // 'java_in_dir': 'java',
74 // 'includes': [ '../../../build/java.gypi' ],
80 // - GN Build must be configured to generate bindings:
81 // # Add import at top of file:
83 // import("//build/config/android/rules.gni") # For generate_jni().
86 // # An example target that will rely on JNI:
88 // # ... normal sources, defines, deps.
89 // # For each jni generated .java -> .h header file in jni_headers
90 // # target there will be a single .cc file here that includes it.
92 // # Add a dep for JNI:
94 // deps += [ ":foo_jni" ]
98 // # Create target for JNI:
100 // generate_jni("jni_headers") {
102 // "java/src/org/chromium/example/jni_generator/SampleForTests.java",
104 // jni_package = "foo"
106 // android_library("java") {
108 // "java/src/org/chromium/example/jni_generator/SampleForTests.java",
109 // "java/src/org/chromium/example/jni_generator/NonJniFile.java",
114 // For C++ methods to be exposed to Java:
115 // - The generated RegisterNativesImpl method must be called, this is typically
116 // done by having a static RegisterJNI method in the C++ class.
117 // - The RegisterJNI method is added to a module's collection of register
118 // methods, such as: example_jni_registrar.h/cc files which call
119 // base::android::RegisterNativeMethods.
120 // An example_jni_registstrar.cc:
123 // const base::android::RegistrationMethod kRegisteredMethods[] = {
124 // // Initial string is for debugging only.
125 // { "ExampleName", base::ExampleNameAndroid::RegisterJNI },
126 // { "ExampleName2", base::ExampleName2Android::RegisterJNI },
130 // bool RegisterModuleNameJni(JNIEnv* env) {
131 // return RegisterNativeMethods(env, kRegisteredMethods,
132 // arraysize(kRegisteredMethods));
135 // - Each module's RegisterModuleNameJni must be called by a larger module,
136 // or application during startup.
143 // Register C++ methods exposed to Java using JNI.
144 static bool RegisterJNI(JNIEnv
* env
);
146 // Java @CalledByNative methods implicitly available to C++ via the _jni.h
147 // file included in the .cc file.
151 jdouble
MethodOtherP0(JNIEnv
* env
, jobject caller
);
154 void Destroy(JNIEnv
* env
, jobject caller
);
156 jint
Method(JNIEnv
* env
, jobject caller
);
158 void AddStructB(JNIEnv
* env
, jobject caller
, jobject structb
);
160 void IterateAndDoSomethingWithStructB(JNIEnv
* env
, jobject caller
);
162 base::android::ScopedJavaLocalRef
<jstring
> ReturnAString(
163 JNIEnv
* env
, jobject caller
);
166 std::map
<long, std::string
> map_
;
168 DISALLOW_COPY_AND_ASSIGN(CPPClass
);
171 } // namespace android
174 #endif // BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_