OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_ | 5 #ifndef BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_ |
6 #define BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_ | 6 #define BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_ |
7 | 7 |
8 #include <jni.h> | 8 #include <jni.h> |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/android/jni_android.h" | 12 #include "base/android/jni_android.h" |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 namespace android { | 16 namespace android { |
17 | 17 |
18 // This file is used to: | 18 // This file is used to: |
19 // - document the best practices and guidelines on JNI usage. | 19 // - document the best practices and guidelines on JNI usage. |
20 // - ensure sample_for_tests_jni.h compiles and the functions declared in it | 20 // - ensure sample_for_tests_jni.h compiles and the functions declared in it |
21 // as expected. | 21 // as expected. |
22 // | 22 // |
23 // All methods are called directly from Java. See more documentation in | 23 // Methods are called directly from Java (except RegisterJNI). More |
24 // SampleForTests.java. | 24 // documentation in SampleForTests.java |
25 // | |
26 // For C++ to access Java methods: | |
27 // - GYP Build must be configured to generate bindings: | |
28 // # ... | |
29 // 'targets': [ | |
30 // { | |
31 // # An example target that will rely on JNI: | |
32 // 'target_name': 'base_jni_generator_sample', | |
cjhopman
2015/05/20 02:28:27
I'd probably do s/base_jni_generator/foo/ througho
cjhopman
2015/05/20 02:28:27
add a 'type': 'static_library' (or shared/componen
scheib
2015/05/20 22:12:06
Done.
scheib
2015/05/20 22:12:06
Done.
| |
33 // # ... normal sources, defines, deps. | |
34 // # Add deps for JNI: | |
35 // 'conditions': [ | |
36 // ['OS == "android"', { | |
37 // 'dependencies': [ | |
38 // 'base_jni_generator_sample_java', | |
nyquist
2015/05/14 00:31:13
I don't think we should suggest depending on both
scheib
2015/05/14 02:14:47
By demonstration, I have the device/bluetooth targ
nyquist
2015/05/14 17:01:01
C++ target at compile time only need _jni_headers.
scheib
2015/05/14 17:11:27
That seems consistent with "C++ target requires _j
| |
39 // 'base_jni_generator_sample_jni_headers', | |
40 // ], | |
41 // }], | |
42 // ], | |
43 // }, | |
44 // ], | |
45 // # ... | |
46 // # Create targets for JNI: | |
47 // 'conditions': [ | |
48 // ['OS == "android"', { | |
49 // 'targets': [ | |
50 // { | |
51 // 'target_name': 'base_jni_generator_sample_jni_headers', | |
52 // 'type': 'none', | |
53 // 'sources': [ | |
54 // 'java/src/org/chromium/example/jni_generator/SampleForTests.java', | |
55 // ], | |
56 // 'variables': { | |
57 // 'jni_gen_package': 'base_jni_generator_sample', | |
58 // }, | |
59 // 'includes': [ '../../../build/jni_generator.gypi' ], | |
60 // }, | |
61 // { | |
62 // 'target_name': 'base_jni_generator_sample_java', | |
63 // 'type': 'none', | |
64 // 'dependencies': [ | |
65 // '../../../base/base.gyp:base', | |
66 // ], | |
67 // 'variables': { | |
68 // 'java_in_dir': 'java', | |
69 // }, | |
70 // 'includes': [ '../../../build/java.gypi' ], | |
71 // }, | |
72 // ], | |
73 // }], | |
74 // ], | |
75 // | |
76 // - GN Build must be configured to generate bindings: | |
77 // # Add import at top of file: | |
78 // if (is_android) { | |
79 // import("//build/config/android/rules.gni") # For generate_jni(). | |
80 // } | |
81 // # ... | |
82 // # An example target that will rely on JNI: | |
83 // component("base_jni_generator_sample") { | |
84 // # ... normal sources, defines, deps. | |
nyquist
2015/05/14 00:31:13
Mention that herein lies the one and only .cc file
scheib
2015/05/20 22:12:06
Done.
| |
85 // # Add a dep for JNI: | |
86 // if (is_android) { | |
87 // deps += [ ":base_jni_generator_sample_jni" ] | |
88 // } | |
89 // } | |
90 // # ... | |
91 // # Create target for JNI: | |
92 // if (is_android) { | |
93 // generate_jni("base_jni_generator_sample_jni") { | |
94 // sources = [ | |
95 // "java/src/org/chromium/example/jni_generator/SampleForTests.java", | |
96 // ] | |
97 // jni_package = "base_jni_generator_sample" | |
98 // } | |
cjhopman
2015/05/20 02:28:27
add:
android_library("base_jni_generator_sample_ja
scheib
2015/05/20 22:12:06
Done.
| |
99 // } | |
100 // | |
101 // For C++ methods to be exposed to Java: | |
102 // - The generated RegisterNativesImpl method must be called, this is typically | |
103 // done by having a static RegisterJNI method in the C++ class. | |
104 // - The RegisterJNI method is added to a module's collection of register | |
105 // methods, such as: example_jni_registrar.h/cc files which call | |
106 // base::android::RegisterNativeMethods. | |
107 // An example_jni_registstrar.cc: | |
108 // | |
109 // namespace { | |
110 // const base::android::RegistrationMethod kRegisteredMethods[] = { | |
111 // // Initial string is for debugging only. | |
112 // { "ExampleName", base::ExampleNameAndroid::RegisterJNI }, | |
113 // { "ExampleName2", base::ExampleName2Android::RegisterJNI }, | |
114 // }; | |
115 // } // namespace | |
116 // | |
117 // bool RegisterModuleNameJni(JNIEnv* env) { | |
118 // return RegisterNativeMethods(env, kRegisteredMethods, | |
119 // arraysize(kRegisteredMethods)); | |
120 // } | |
121 // | |
122 // - Each module's RegisterModuleNameJni is then called by a larger module, | |
cjhopman
2015/05/20 02:28:27
I'd do s/is then called/must be called/ to make it
scheib
2015/05/20 22:12:06
Done.
| |
123 // or application during startup. | |
124 // | |
25 class CPPClass { | 125 class CPPClass { |
26 public: | 126 public: |
27 CPPClass(); | 127 CPPClass(); |
28 ~CPPClass(); | 128 ~CPPClass(); |
29 | 129 |
130 // Register C++ methods exposed to Java using JNI. | |
131 static bool RegisterJNI(JNIEnv* env); | |
132 | |
133 // Java @CalledByNative methods implicitly available to C++ via the _jni.h | |
134 // file included in the .cc file. | |
135 | |
30 class InnerClass { | 136 class InnerClass { |
31 public: | 137 public: |
32 jdouble MethodOtherP0(JNIEnv* env, jobject obj); | 138 jdouble MethodOtherP0(JNIEnv* env, jobject obj); |
33 }; | 139 }; |
34 | 140 |
35 void Destroy(JNIEnv* env, jobject obj); | 141 void Destroy(JNIEnv* env, jobject obj); |
36 | 142 |
37 jint Method(JNIEnv* env, jobject obj); | 143 jint Method(JNIEnv* env, jobject obj); |
38 | 144 |
39 void AddStructB(JNIEnv* env, jobject obj, jobject structb); | 145 void AddStructB(JNIEnv* env, jobject obj, jobject structb); |
40 | 146 |
41 void IterateAndDoSomethingWithStructB(JNIEnv* env, jobject obj); | 147 void IterateAndDoSomethingWithStructB(JNIEnv* env, jobject obj); |
42 | 148 |
43 base::android::ScopedJavaLocalRef<jstring> ReturnAString( | 149 base::android::ScopedJavaLocalRef<jstring> ReturnAString( |
44 JNIEnv* env, jobject obj); | 150 JNIEnv* env, jobject obj); |
45 | 151 |
46 private: | 152 private: |
47 std::map<long, std::string> map_; | 153 std::map<long, std::string> map_; |
48 | 154 |
49 DISALLOW_COPY_AND_ASSIGN(CPPClass); | 155 DISALLOW_COPY_AND_ASSIGN(CPPClass); |
50 }; | 156 }; |
51 | 157 |
52 } // namespace android | 158 } // namespace android |
53 } // namespace base | 159 } // namespace base |
54 | 160 |
55 #endif // BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_ | 161 #endif // BASE_ANDROID_JNI_GENERATOR_SAMPLE_FOR_TESTS_H_ |
OLD | NEW |