The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | <html> |
| 2 | <head> |
| 3 | <title>Android JNI Tips</title> |
| 4 | <link rel=stylesheet href="android.css"> |
| 5 | </head> |
| 6 | |
| 7 | <body> |
| 8 | <h1><a name="JNI_Tips"></a>Android JNI Tips</h1> |
| 9 | <p> |
| 10 | </p><p> |
| 11 | </p><ul> |
| 12 | <li> <a href="#What_s_JNI_">What's JNI?</a> |
| 13 | </li> |
| 14 | <li> <a href="#JavaVM_and_JNIEnv">JavaVM and JNIEnv</a> |
| 15 | |
| 16 | </li> |
Andy McFadden | 40f3271 | 2009-09-22 13:55:42 -0700 | [diff] [blame] | 17 | <li> <a href="#jclass_jmethodID_and_jfieldID">jclass, jmethodID, and jfieldID</a> |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 18 | </li> |
| 19 | <li> <a href="#local_vs_global_references">Local vs. Global References</a> |
| 20 | </li> |
| 21 | <li> <a href="#UTF_8_and_UTF_16_strings">UTF-8 and UTF-16 Strings</a> |
| 22 | </li> |
| 23 | <li> <a href="#Arrays">Primitive Arrays</a> |
| 24 | </li> |
| 25 | <li> <a href="#RegionCalls">Region Calls</a> |
| 26 | </li> |
| 27 | <li> <a href="#Exceptions">Exceptions</a> |
| 28 | </li> |
| 29 | |
| 30 | <li> <a href="#Extended_checking">Extended Checking</a> |
| 31 | </li> |
| 32 | <li> <a href="#Native_Libraries">Native Libraries</a> |
| 33 | </li> |
| 34 | <li> <a href="#64bit">64-bit Considerations</a> |
| 35 | </li> |
| 36 | |
| 37 | <li> <a href="#Unsupported">Unsupported Features</a> |
| 38 | </ul> |
| 39 | <p> |
| 40 | <noautolink> |
| 41 | </noautolink></p><p> |
| 42 | </p><h2><a name="What_s_JNI_"> </a> What's JNI? </h2> |
| 43 | <p> |
| 44 | |
| 45 | JNI is the Java Native Interface. It defines a way for code written in the |
| 46 | Java programming language to interact with native |
| 47 | code, e.g. functions written in C/C++. It's VM-neutral, has support for loading code from |
| 48 | dynamic shared libraries, and while cumbersome at times is reasonably efficient. |
| 49 | </p><p> |
| 50 | You really should read through the |
| 51 | <a href="http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html">JNI spec for J2SE 1.6</a> |
| 52 | to get a sense for how JNI works and what features are available. Some |
| 53 | aspects of the interface aren't immediately obvious on |
| 54 | first reading, so you may find the next few sections handy. |
| 55 | The more detailed <i>JNI Programmer's Guide and Specification</i> can be found |
| 56 | <a href="http://java.sun.com/docs/books/jni/html/jniTOC.html">here</a>. |
| 57 | </p><p> |
| 58 | </p><p> |
| 59 | </p><h2><a name="JavaVM_and_JNIEnv"> </a> JavaVM and JNIEnv </h2> |
| 60 | <p> |
| 61 | JNI defines two key data structures, "JavaVM" and "JNIEnv". Both of these are essentially |
| 62 | pointers to pointers to function tables. (In the C++ version, it's a class whose sole member |
| 63 | is a pointer to a function table.) The JavaVM provides the "invocation interface" functions, |
| 64 | which allow you to create and destroy the VM. In theory you can have multiple VMs per process, |
| 65 | but Android's VMs only allow one. |
| 66 | </p><p> |
| 67 | The JNIEnv provides most of the JNI functions. Your native functions all receive a JNIEnv as |
| 68 | the first argument. |
| 69 | </p><p> |
| 70 | |
| 71 | On some VMs, the JNIEnv is used for thread-local storage. For this reason, <strong>you cannot share a JNIEnv between threads</strong>. |
| 72 | If a piece of code has no other way to get its JNIEnv, you should share |
| 73 | the JavaVM, and use JavaVM->GetEnv to discover the thread's JNIEnv. |
| 74 | </p><p> |
| 75 | The C and C++ declarations of JNIEnv and JavaVM are different. "jni.h" provides different typedefs |
| 76 | depending on whether it's included into ".c" or ".cpp". For this reason it's a bad idea to |
| 77 | include JNIEnv arguments in header files included by both languages. (Put another way: if your |
| 78 | header file requires "#ifdef __cplusplus", you may have to do some extra work if anything in |
| 79 | that header refers to JNIEnv.) |
| 80 | </p><p> |
| 81 | </p><p> |
Andy McFadden | 40f3271 | 2009-09-22 13:55:42 -0700 | [diff] [blame] | 82 | </p><h2><a name="jclass_jmethodID_and_jfieldID"> jclass, jmethodID, and jfieldID </a></h2> |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 83 | <p> |
| 84 | If you want to access an object's field from native code, you would do the following: |
| 85 | </p><p> |
| 86 | </p><ul> |
| 87 | <li> Get the class object reference for the class with <code>FindClass</code> |
| 88 | </li> |
| 89 | <li> Get the field ID for the field with <code>GetFieldID</code> |
| 90 | </li> |
| 91 | <li> Get the contents of the field with something appropriate, e.g. |
| 92 | <code>GetIntField</code> |
| 93 | </li> |
| 94 | </ul> |
| 95 | <p> |
| 96 | Similarly, to call a method, you'd first get a class object reference and then a method ID. The IDs are often just |
| 97 | pointers to internal VM data structures. Looking them up may require several string |
| 98 | comparisons, but once you have them the actual call to get the field or invoke the method |
| 99 | is very quick. |
| 100 | </p><p> |
| 101 | If performance is important, it's useful to look the values up once and cache the results |
| 102 | in your native code. Because we are limiting ourselves to one VM per process, it's reasonable |
| 103 | to store this data in a static local structure. |
| 104 | </p><p> |
| 105 | The class references, field IDs, and method IDs are guaranteed valid until the class is unloaded. Classes |
| 106 | are only unloaded if all classes associated with a ClassLoader can be garbage collected, |
Andy McFadden | 40f3271 | 2009-09-22 13:55:42 -0700 | [diff] [blame] | 107 | which is rare but will not be impossible in our system. Note however that |
| 108 | the <code>jclass</code> |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 109 | is a class reference and <strong>must be protected</strong> with a call |
| 110 | to <code>NewGlobalRef</code> (see the next section). |
| 111 | </p><p> |
| 112 | If you would like to cache the IDs when a class is loaded, and automatically re-cache them |
| 113 | if the class is ever unloaded and reloaded, the correct way to initialize |
| 114 | the IDs is to add a piece of code that looks like this to the appropriate class: |
| 115 | </p><p> |
| 116 | |
| 117 | </p><pre> /* |
| 118 | * We use a class initializer to allow the native code to cache some |
| 119 | * field offsets. |
| 120 | */ |
| 121 | |
| 122 | /* |
| 123 | * A native function that looks up and caches interesting |
| 124 | * class/field/method IDs for this class. Returns false on failure. |
| 125 | */ |
| 126 | native private static boolean nativeClassInit(); |
Carl Shapiro | de75089 | 2010-06-08 16:37:12 -0700 | [diff] [blame] | 127 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 128 | /* |
| 129 | * Invoke the native initializer when the class is loaded. |
| 130 | */ |
| 131 | static { |
| 132 | if (!nativeClassInit()) |
| 133 | throw new RuntimeException("native init failed"); |
| 134 | } |
| 135 | </pre> |
| 136 | <p> |
| 137 | Create a nativeClassInit method in your C/C++ code that performs the ID lookups. The code |
| 138 | will be executed once, when the class is initialized. If the class is ever unloaded and |
| 139 | then reloaded, it will be executed again. (See the implementation of java.io.FileDescriptor |
| 140 | for an example in our source tree.) |
| 141 | </p><p> |
| 142 | </p><p> |
| 143 | </p><p> |
| 144 | </p><h2><a name="local_vs_global_references"> Local vs. Global References </a></h2> |
| 145 | <p> |
| 146 | Every object that JNI returns is a "local reference". This means that it's valid for the |
| 147 | duration of the current native method in the current thread. |
| 148 | <strong>Even if the object itself continues to live on after the native method returns, the reference is not valid.</strong> |
Andy McFadden | 40f3271 | 2009-09-22 13:55:42 -0700 | [diff] [blame] | 149 | This applies to all sub-classes of <code>jobject</code>, including |
| 150 | <code>jclass</code>, <code>jstring</code>, and <code>jarray</code>. |
| 151 | (Dalvik VM will warn you about most reference mis-uses when extended JNI |
| 152 | checks are enabled.) |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 153 | </p><p> |
| 154 | |
Andy McFadden | 40f3271 | 2009-09-22 13:55:42 -0700 | [diff] [blame] | 155 | If you want to hold on to a reference for a longer period, you must use |
| 156 | a "global" reference. The <code>NewGlobalRef</code> function takes the |
| 157 | local reference as an argument and returns a global one. |
| 158 | The global reference is guaranteed to be valid until you call |
| 159 | <code>DeleteGlobalRef</code>. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 160 | |
Andy McFadden | 40f3271 | 2009-09-22 13:55:42 -0700 | [diff] [blame] | 161 | </p><p> |
| 162 | This pattern is commonly used when caching copies of class objects obtained |
| 163 | from <code>FindClass</code>, e.g.: |
| 164 | <p><pre>jclass* localClass = env->FindClass("MyClass"); |
| 165 | jclass* globalClass = (jclass*) env->NewGlobalRef(localClass); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 166 | </pre> |
| 167 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 168 | </p><p> |
| 169 | All JNI methods accept both local and global references as arguments. |
Andy McFadden | 5aca603 | 2009-05-06 16:48:33 -0700 | [diff] [blame] | 170 | It's possible for references to the same object to have different values; |
| 171 | for example, the return values from consecutive calls to |
| 172 | <code>NewGlobalRef</code> on the same object may be different. |
| 173 | <strong>To see if two references refer to the same object, |
| 174 | you must use the <code>IsSameObject</code> function.</strong> Never compare |
| 175 | references with "==" in native code. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 176 | </p><p> |
Andy McFadden | c7659ec | 2009-09-18 16:14:41 -0700 | [diff] [blame] | 177 | One consequence of this is that you |
Andy McFadden | 40f3271 | 2009-09-22 13:55:42 -0700 | [diff] [blame] | 178 | <strong>must not assume object references are constant or unique</strong> |
Andy McFadden | c7659ec | 2009-09-18 16:14:41 -0700 | [diff] [blame] | 179 | in native code. The 32-bit value representing an object may be different |
| 180 | from one invocation of a method to the next, and it's possible that two |
Andy McFadden | 40f3271 | 2009-09-22 13:55:42 -0700 | [diff] [blame] | 181 | different objects could have the same 32-bit value on consecutive calls. Do |
| 182 | not use <code>jobject</code> values as keys. |
Andy McFadden | c7659ec | 2009-09-18 16:14:41 -0700 | [diff] [blame] | 183 | </p><p> |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 184 | Programmers are required to "not excessively allocate" local references. In practical terms this means |
| 185 | that if you're creating large numbers of local references, perhaps while running through an array of |
| 186 | Objects, you should free them manually with |
| 187 | <code>DeleteLocalRef</code> instead of letting JNI do it for you. The |
| 188 | VM is only required to reserve slots for |
| 189 | 16 local references, so if you need more than that you should either delete as you go or use |
| 190 | <code>EnsureLocalCapacity</code> to reserve more. |
| 191 | </p><p> |
| 192 | Note: method and field IDs are just 32-bit identifiers, not object |
| 193 | references, and should not be passed to <code>NewGlobalRef</code>. The raw data |
| 194 | pointers returned by functions like <code>GetStringUTFChars</code> |
| 195 | and <code>GetByteArrayElements</code> are also not objects. |
| 196 | </p><p> |
| 197 | One unusual case deserves separate mention. If you attach a native |
| 198 | thread to the VM with AttachCurrentThread, the code you are running will |
| 199 | never "return" to the VM until the thread detaches from the VM. Any local |
Andy McFadden | 40f3271 | 2009-09-22 13:55:42 -0700 | [diff] [blame] | 200 | references you create will have to be deleted manually unless you're going |
| 201 | to detach the thread soon. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 202 | </p><p> |
| 203 | </p><p> |
| 204 | </p><p> |
| 205 | </p><h2><a name="UTF_8_and_UTF_16_strings"> </a> UTF-8 and UTF-16 Strings </h2> |
| 206 | <p> |
| 207 | The Java programming language uses UTF-16. For convenience, JNI provides methods that work with "modified UTF-8" encoding |
| 208 | as well. (Some VMs use the modified UTF-8 internally to store strings; ours do not.) The |
| 209 | modified encoding only supports the 8- and 16-bit forms, and stores ASCII NUL values in a 16-bit encoding. |
| 210 | The nice thing about it is that you can count on having C-style zero-terminated strings, |
| 211 | suitable for use with standard libc string functions. The down side is that you cannot pass |
| 212 | arbitrary UTF-8 data into the VM and expect it to work correctly. |
| 213 | </p><p> |
| 214 | It's usually best to operate with UTF-16 strings. With our current VMs, the |
| 215 | <code>GetStringChars</code> method |
| 216 | does not require a copy, whereas <code>GetStringUTFChars</code> requires a malloc and a UTF conversion. Note that |
| 217 | <strong>UTF-16 strings are not zero-terminated</strong>, and \u0000 is allowed, |
| 218 | so you need to hang on to the string length as well as |
| 219 | the string pointer. |
| 220 | |
| 221 | </p><p> |
Andy McFadden | 5aca603 | 2009-05-06 16:48:33 -0700 | [diff] [blame] | 222 | <strong>Don't forget to Release the strings you Get</strong>. The |
| 223 | string functions return <code>jchar*</code> or <code>jbyte*</code>, which |
| 224 | are C-style pointers to primitive data rather than local references. They |
| 225 | are guaranteed valid until Release is called, which means they are not |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 226 | released when the native method returns. |
| 227 | </p><p> |
Andy McFadden | 7e7a332 | 2010-01-28 12:27:06 -0800 | [diff] [blame] | 228 | <strong>Data passed to NewStringUTF must be in "modified" UTF-8 format</strong>. A |
| 229 | common mistake is reading character data from a file or network stream |
| 230 | and handing it to <code>NewStringUTF</code> without filtering it. |
| 231 | Unless you know the data is 7-bit ASCII, you need to strip out high-ASCII |
| 232 | characters or convert them to proper "modified" UTF-8 form. If you don't, |
| 233 | the UTF-16 conversion will likely not be what you expect. The extended |
| 234 | JNI checks will scan strings and warn you about invalid data, but they |
| 235 | won't catch everything. |
| 236 | </p><p> |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 237 | </p><p> |
| 238 | |
| 239 | |
| 240 | </p><h2><a name="Arrays"> </a> Primitive Arrays </h2> |
| 241 | <p> |
| 242 | JNI provides functions for accessing the contents of array objects. |
| 243 | While arrays of objects must be accessed one entry at a time, arrays of |
| 244 | primitives can be read and written directly as if they were declared in C. |
| 245 | </p><p> |
| 246 | To make the interface as efficient as possible without constraining |
| 247 | the VM implementation, |
| 248 | the <code>Get<PrimitiveType>ArrayElements</code> family of calls |
| 249 | allows the VM to either return a pointer to the actual elements, or |
| 250 | allocate some memory and make a copy. Either way, the raw pointer returned |
| 251 | is guaranteed to be valid until the corresponding <code>Release</code> call |
| 252 | is issued (which implies that, if the data wasn't copied, the array object |
| 253 | will be pinned down and can't be relocated as part of compacting the heap). |
| 254 | <strong>You must Release every array you Get.</strong> Also, if the Get |
| 255 | call fails, you must ensure that your code doesn't try to Release a NULL |
| 256 | pointer later. |
| 257 | </p><p> |
| 258 | You can determine whether or not the data was copied by passing in a |
| 259 | non-NULL pointer for the <code>isCopy</code> argument. This is rarely |
| 260 | useful. |
| 261 | </p><p> |
| 262 | The <code>Release</code> call takes a <code>mode</code> argument that can |
| 263 | have one of three values. The actions performed by the VM depend upon |
| 264 | whether it returned a pointer to the actual data or a copy of it: |
| 265 | <ul> |
| 266 | <li><code>0</code> |
| 267 | <ul> |
| 268 | <li>Actual: the array object is un-pinned. |
| 269 | <li>Copy: data is copied back. The buffer with the copy is freed. |
| 270 | </ul> |
| 271 | <li><code>JNI_COMMIT</code> |
| 272 | <ul> |
| 273 | <li>Actual: does nothing. |
| 274 | <li>Copy: data is copied back. The buffer with the copy |
| 275 | <strong>is not freed</strong>. |
| 276 | </ul> |
| 277 | <li><code>JNI_ABORT</code> |
| 278 | <ul> |
| 279 | <li>Actual: the array object is un-pinned. Earlier |
| 280 | writes are <strong>not</strong> aborted. |
| 281 | <li>Copy: the buffer with the copy is freed; any changes to it are lost. |
| 282 | </ul> |
| 283 | </ul> |
| 284 | </p><p> |
| 285 | One reason for checking the <code>isCopy</code> flag is to know if |
| 286 | you need to call <code>Release</code> with <code>JNI_COMMIT</code> |
| 287 | after making changes to an array -- if you're alternating between making |
| 288 | changes and executing code that uses the contents of the array, you may be |
| 289 | able to |
| 290 | skip the no-op commit. Another possible reason for checking the flag is for |
| 291 | efficient handling of <code>JNI_ABORT</code>. For example, you might want |
| 292 | to get an array, modify it in place, pass pieces to other functions, and |
| 293 | then discard the changes. If you know that JNI is making a new copy for |
| 294 | you, there's no need to create another "editable" copy. If JNI is passing |
| 295 | you the original, then you do need to make your own copy. |
| 296 | </p><p> |
| 297 | Some have asserted that you can skip the <code>Release</code> call if |
| 298 | <code>*isCopy</code> is false. This is not the case. If no copy buffer was |
| 299 | allocated, then the original memory must be pinned down and can't be moved by |
| 300 | the garbage collector. |
| 301 | </p><p> |
| 302 | Also note that the <code>JNI_COMMIT</code> flag does NOT release the array, |
| 303 | and you will need to call <code>Release</code> again with a different flag |
| 304 | eventually. |
| 305 | </p><p> |
| 306 | </p><p> |
| 307 | |
| 308 | |
| 309 | </p><h2><a name="RegionCalls"> Region Calls </a></h2> |
| 310 | |
| 311 | <p> |
| 312 | There is an alternative to calls like <code>Get<Type>ArrayElements</code> |
| 313 | and <code>GetStringChars</code> that may be very helpful when all you want |
| 314 | to do is copy data in or out. Consider the following: |
| 315 | <pre> |
| 316 | jbyte* data = env->GetByteArrayElements(array, NULL); |
| 317 | if (data != NULL) { |
| 318 | memcpy(buffer, data, len); |
| 319 | env->ReleaseByteArrayElements(array, data, JNI_ABORT); |
| 320 | } |
| 321 | </pre> |
| 322 | <p> |
| 323 | This grabs the array, copies the first <code>len</code> byte |
| 324 | elements out of it, and then releases the array. Depending upon the VM |
| 325 | policies the <code>Get</code> call will either pin or copy the array contents. |
| 326 | We copy the data (for perhaps a second time), then call Release; in this case |
| 327 | we use <code>JNI_ABORT</code> so there's no chance of a third copy. |
| 328 | </p><p> |
| 329 | We can accomplish the same thing with this: |
| 330 | <pre> |
| 331 | env->GetByteArrayRegion(array, 0, len, buffer); |
| 332 | </pre> |
| 333 | </p><p> |
| 334 | This accomplishes the same thing, with several advantages: |
| 335 | <ul> |
Andy McFadden | c7659ec | 2009-09-18 16:14:41 -0700 | [diff] [blame] | 336 | <li>Requires one JNI call instead of 2, reducing overhead. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 337 | <li>Doesn't require pinning or extra data copies. |
Andy McFadden | c7659ec | 2009-09-18 16:14:41 -0700 | [diff] [blame] | 338 | <li>Reduces the risk of programmer error -- no risk of forgetting |
| 339 | to call <code>Release</code> after something fails. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 340 | </ul> |
| 341 | </p><p> |
| 342 | Similarly, you can use the <code>Set<Type>ArrayRegion</code> call |
| 343 | to copy data into an array, and <code>GetStringRegion</code> or |
| 344 | <code>GetStringUTFRegion</code> to copy characters out of a |
| 345 | <code>String</code>. |
| 346 | |
| 347 | |
| 348 | </p><h2><a name="Exceptions"> Exceptions </a></h2> |
| 349 | <p> |
| 350 | <strong>You may not call most JNI functions while an exception is pending.</strong> |
| 351 | Your code is expected to notice the exception (via the function's return value, |
| 352 | <code>ExceptionCheck()</code>, or <code>ExceptionOccurred()</code>) and return, |
| 353 | or clear the exception and handle it. |
| 354 | </p><p> |
| 355 | The only JNI functions that you are allowed to call while an exception is |
| 356 | pending are: |
| 357 | <font size="-1"><ul> |
| 358 | <li>DeleteGlobalRef |
| 359 | <li>DeleteLocalRef |
| 360 | <li>DeleteWeakGlobalRef |
| 361 | <li>ExceptionCheck |
| 362 | <li>ExceptionClear |
| 363 | <li>ExceptionDescribe |
| 364 | <li>ExceptionOccurred |
| 365 | <li>MonitorExit |
| 366 | <li>PopLocalFrame |
| 367 | <li>PushLocalFrame |
Elliott Hughes | b7906ed | 2009-09-09 15:16:59 -0700 | [diff] [blame] | 368 | <li>Release<PrimitiveType>ArrayElements |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 369 | <li>ReleasePrimitiveArrayCritical |
| 370 | <li>ReleaseStringChars |
| 371 | <li>ReleaseStringCritical |
| 372 | <li>ReleaseStringUTFChars |
| 373 | </ul></font> |
| 374 | </p><p> |
| 375 | Note that exceptions thrown by interpreted code do not "leap over" native code, |
| 376 | and C++ exceptions thrown by native code are not handled by Dalvik. |
| 377 | The JNI <code>Throw</code> and <code>ThrowNew</code> instructions just |
| 378 | set an exception pointer in the current thread. Upon returning to the VM from |
| 379 | native code, the exception will be noted and handled appropriately. |
| 380 | </p><p> |
| 381 | Native code can "catch" an exception by calling <code>ExceptionCheck</code> or |
| 382 | <code>ExceptionOccurred</code>, and clear it with |
| 383 | <code>ExceptionClear</code>. As usual, |
| 384 | discarding exceptions without handling them can lead to problems. |
| 385 | </p><p> |
| 386 | There are no built-in functions for manipulating the Throwable object |
| 387 | itself, so if you want to (say) get the exception string you will need to |
| 388 | find the Throwable class, look up the method ID for |
| 389 | <code>getMessage "()Ljava/lang/String;"</code>, invoke it, and if the result |
| 390 | is non-NULL use <code>GetStringUTFChars</code> to get something you can |
| 391 | hand to printf or a LOG macro. |
| 392 | |
| 393 | </p><p> |
| 394 | </p><p> |
| 395 | </p><h2><a name="Extended_checking"> Extended Checking </a></h2> |
| 396 | <p> |
Andy McFadden | 40f3271 | 2009-09-22 13:55:42 -0700 | [diff] [blame] | 397 | JNI does very little error checking. Calling <code>SetIntField</code> |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 398 | on an Object field will succeed, even if the field is marked |
| 399 | <code>private</code> and <code>final</code>. The |
| 400 | goal is to minimize the overhead on the assumption that, if you've written it in native code, |
| 401 | you probably did it for performance reasons. |
| 402 | </p><p> |
| 403 | Some VMs support extended checking with the "<code>-Xcheck:jni</code>" flag. If the flag is set, the VM |
| 404 | puts a different table of functions into the JavaVM and JNIEnv pointers. These functions do |
| 405 | an extended series of checks before calling the standard implementation. |
| 406 | |
| 407 | </p><p> |
Andy McFadden | c7659ec | 2009-09-18 16:14:41 -0700 | [diff] [blame] | 408 | Some things that may be checked: |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 409 | </p><p> |
| 410 | </p> |
| 411 | <ul> |
| 412 | <li> Check for null pointers where not allowed. |
Andy McFadden | 3ccaec4 | 2010-06-04 16:51:47 -0700 | [diff] [blame] | 413 | </li> |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 414 | <li> Verify argument type correctness (jclass is a class object, |
| 415 | jfieldID points to field data, jstring is a java.lang.String). |
| 416 | </li> |
| 417 | <li> Field type correctness, e.g. don't store a HashMap in a String field. |
| 418 | </li> |
| 419 | <li> Check to see if an exception is pending on calls where pending exceptions are not legal. |
| 420 | </li> |
| 421 | <li> Check for calls to inappropriate functions between Critical get/release calls. |
| 422 | </li> |
| 423 | <li> Check that JNIEnv structs aren't being shared between threads. |
| 424 | |
| 425 | </li> |
| 426 | <li> Make sure local references aren't used outside their allowed lifespan. |
| 427 | </li> |
Andy McFadden | 7e7a332 | 2010-01-28 12:27:06 -0800 | [diff] [blame] | 428 | <li> UTF-8 strings contain only valid "modified UTF-8" data. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 429 | </li> |
| 430 | </ul> |
| 431 | <p>Accessibility of methods and fields (i.e. public vs. private) is not |
| 432 | checked. |
| 433 | <p> |
| 434 | The Dalvik VM supports the <code>-Xcheck:jni</code> flag. For a |
| 435 | description of how to enable it for Android apps, see |
| 436 | <a href="embedded-vm-control.html">Controlling the Embedded VM</a>. |
| 437 | It's currently enabled by default in the Android emulator and on |
| 438 | "engineering" device builds. |
| 439 | |
| 440 | </p><p> |
| 441 | JNI checks can be modified with the <code>-Xjniopts</code> command-line |
| 442 | flag. Currently supported values include: |
| 443 | </p> |
| 444 | <blockquote><dl> |
| 445 | <dt>forcecopy |
| 446 | <dd>When set, any function that can return a copy of the original data |
| 447 | (array of primitive values, UTF-16 chars) will always do so. The buffers |
| 448 | are over-allocated and surrounded with a guard pattern to help identify |
| 449 | code writing outside the buffer, and the contents are erased before the |
| 450 | storage is freed to trip up code that uses the data after calling Release. |
Andy McFadden | c7659ec | 2009-09-18 16:14:41 -0700 | [diff] [blame] | 451 | This will have a noticeable performance impact on some applications. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 452 | <dt>warnonly |
| 453 | <dd>By default, JNI "warnings" cause the VM to abort. With this flag |
| 454 | it continues on. |
| 455 | </dl></blockquote> |
| 456 | |
| 457 | |
| 458 | </p><p> |
| 459 | </p><h2><a name="Native_Libraries"> Native Libraries </a></h2> |
| 460 | <p> |
| 461 | You can load native code from shared libraries with the standard |
| 462 | <code>System.loadLibrary()</code> call. The |
| 463 | preferred way to get at your native code is: |
| 464 | </p><p> |
| 465 | </p><ul> |
Andy McFadden | 40f3271 | 2009-09-22 13:55:42 -0700 | [diff] [blame] | 466 | <li> Call <code>System.loadLibrary()</code> from a static class |
| 467 | initializer. (See the earlier example, where one is used to call |
| 468 | <code>nativeClassInit()</code>.) The argument is the "undecorated" |
| 469 | library name, e.g. to load "libfubar.so" you would pass in "fubar". |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 470 | |
| 471 | </li> |
| 472 | <li> Provide a native function: <code><strong>jint JNI_OnLoad(JavaVM* vm, void* reserved)</strong></code> |
| 473 | </li> |
| 474 | <li>In <code>JNI_OnLoad</code>, register all of your native methods. You |
| 475 | should declare |
| 476 | the methods "static" so the names don't take up space in the symbol table |
| 477 | on the device. |
| 478 | </li> |
| 479 | </ul> |
| 480 | <p> |
| 481 | The <code>JNI_OnLoad</code> function should look something like this if |
| 482 | written in C: |
| 483 | </p><blockquote><pre>jint JNI_OnLoad(JavaVM* vm, void* reserved) |
| 484 | { |
| 485 | JNIEnv* env; |
| 486 | if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) |
| 487 | return -1; |
| 488 | |
| 489 | /* get class with (*env)->FindClass */ |
| 490 | /* register methods with (*env)->RegisterNatives */ |
| 491 | |
| 492 | return JNI_VERSION_1_4; |
| 493 | } |
| 494 | </pre></blockquote> |
| 495 | </p><p> |
| 496 | You can also call <code>System.load()</code> with the full path name of the |
Andy McFadden | 40f3271 | 2009-09-22 13:55:42 -0700 | [diff] [blame] | 497 | shared library. For Android apps, you may find it useful to get the full |
| 498 | path to the application's private data storage area from the context object. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 499 | </p><p> |
Andy McFadden | 278c12b | 2009-09-21 16:28:39 -0700 | [diff] [blame] | 500 | This is the recommended approach, but not the only approach. The VM does |
| 501 | not require explicit registration, nor that you provide a |
| 502 | <code>JNI_OnLoad</code> function. |
| 503 | You can instead use "discovery" of native methods that are named in a |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 504 | specific way (see <a href="http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/design.html#wp615"> |
Andy McFadden | 278c12b | 2009-09-21 16:28:39 -0700 | [diff] [blame] | 505 | the JNI spec</a> for details), though this is less desirable. |
| 506 | It requires more space in the shared object symbol table, |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 507 | loading is slower because it requires string searches through all of the |
| 508 | loaded shared libraries, and if a method signature is wrong you won't know |
| 509 | about it until the first time the method is actually used. |
| 510 | </p><p> |
Andy McFadden | 278c12b | 2009-09-21 16:28:39 -0700 | [diff] [blame] | 511 | One other note about <code>JNI_OnLoad</code>: any <code>FindClass</code> |
| 512 | calls you make from there will happen in the context of the class loader |
| 513 | that was used to load the shared library. Normally <code>FindClass</code> |
| 514 | uses the loader associated with the method at the top of the interpreted |
| 515 | stack, or if there isn't one (because the thread was just attached to |
| 516 | the VM) it uses the "system" class loader. |
| 517 | </p><p> |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 518 | |
| 519 | |
| 520 | </p><h2><a name="64bit"> 64-bit Considerations </a></h2> |
| 521 | |
| 522 | <p> |
| 523 | Android is currently expected to run on 32-bit platforms. In theory it |
| 524 | could be built for a 64-bit system, but that is not a goal at this time. |
| 525 | For the most part this isn't something that you will need to worry about |
| 526 | when interacting with native code, |
| 527 | but it becomes significant if you plan to store pointers to native |
| 528 | structures in integer fields in an object. To support architectures |
| 529 | that use 64-bit pointers, <strong>you need to stash your native pointers in a |
| 530 | <code>long</code> field rather than an <code>int</code></strong>. |
| 531 | |
| 532 | |
| 533 | </p><h2><a name="Unsupported"> Unsupported Features </a></h2> |
| 534 | <p>All JNI 1.6 features are supported, with the following exceptions: |
| 535 | <ul> |
| 536 | <li><code>DefineClass</code> is not implemented. Dalvik does not use |
| 537 | Java bytecodes or class files, so passing in binary class data |
| 538 | doesn't work. Translation facilities may be added in a future |
| 539 | version of the VM.</li> |
Andy McFadden | b18992f | 2009-09-25 10:42:15 -0700 | [diff] [blame] | 540 | <li>"Weak global" references are implemented, but may only be passed |
| 541 | to <code>NewLocalRef</code>, <code>NewGlobalRef</code>, and |
| 542 | <code>DeleteWeakGlobalRef</code>. (The spec strongly encourages |
| 543 | programmers to create hard references to weak globals before doing |
| 544 | anything with them, so this should not be at all limiting.)</li> |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 545 | <li><code>GetObjectRefType</code> (new in 1.6) is implemented but not fully |
| 546 | functional -- it can't always tell the difference between "local" and |
| 547 | "global" references.</li> |
| 548 | </ul> |
| 549 | |
| 550 | </p> |
| 551 | |
| 552 | <address>Copyright © 2008 The Android Open Source Project</address> |
| 553 | |
| 554 | </body> |
| 555 | </html> |