• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "simplejni native.cpp"
18 #include <android/log.h>
19 
20 #include <stdio.h>
21 
22 #include "jni.h"
23 
24 #define ALOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
25 #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
26 #define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
27 #define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
28 #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
29 
30 static jint
add(JNIEnv *,jobject,jint a,jint b)31 add(JNIEnv* /*env*/, jobject /*thiz*/, jint a, jint b) {
32 int result = a + b;
33     ALOGI("%d + %d = %d", a, b, result);
34     return result;
35 }
36 
37 static const char *classPathName = "com/example/android/simplejni/Native";
38 
39 static JNINativeMethod methods[] = {
40   {"add", "(II)I", (void*)add },
41 };
42 
43 /*
44  * Register several native methods for one class.
45  */
registerNativeMethods(JNIEnv * env,const char * className,JNINativeMethod * gMethods,int numMethods)46 static int registerNativeMethods(JNIEnv* env, const char* className,
47     JNINativeMethod* gMethods, int numMethods)
48 {
49     jclass clazz;
50 
51     clazz = env->FindClass(className);
52     if (clazz == NULL) {
53         ALOGE("Native registration unable to find class '%s'", className);
54         return JNI_FALSE;
55     }
56     if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
57         ALOGE("RegisterNatives failed for '%s'", className);
58         return JNI_FALSE;
59     }
60 
61     return JNI_TRUE;
62 }
63 
64 /*
65  * Register native methods for all classes we know about.
66  *
67  * returns JNI_TRUE on success.
68  */
registerNatives(JNIEnv * env)69 static int registerNatives(JNIEnv* env)
70 {
71   if (!registerNativeMethods(env, classPathName,
72                  methods, sizeof(methods) / sizeof(methods[0]))) {
73     return JNI_FALSE;
74   }
75 
76   return JNI_TRUE;
77 }
78 
79 
80 // ----------------------------------------------------------------------------
81 
82 /*
83  * This is called by the VM when the shared library is first loaded.
84  */
85 
86 typedef union {
87     JNIEnv* env;
88     void* venv;
89 } UnionJNIEnvToVoid;
90 
JNI_OnLoad(JavaVM * vm,void *)91 jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/)
92 {
93     UnionJNIEnvToVoid uenv;
94     uenv.venv = NULL;
95     jint result = -1;
96     JNIEnv* env = NULL;
97 
98     ALOGI("JNI_OnLoad");
99 
100     if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {
101         ALOGE("ERROR: GetEnv failed");
102         goto bail;
103     }
104     env = uenv.env;
105 
106     if (registerNatives(env) != JNI_TRUE) {
107         ALOGE("ERROR: registerNatives failed");
108         goto bail;
109     }
110 
111     result = JNI_VERSION_1_4;
112 
113 bail:
114     return result;
115 }
116