• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 "SplitApp"
18 
19 #include <android/log.h>
20 #include <stdio.h>
21 
22 #include "jni.h"
23 
24 #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
25 #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
26 
27 
add(JNIEnv * env,jobject thiz,jint a,jint b)28 static jint add(JNIEnv *env, jobject thiz, jint a, jint b) {
29     int result = a + b;
30     LOGI("%d + %d = %d", a, b, result);
31     return result;
32 }
33 
arch(JNIEnv * env,jobject thiz)34 static jstring arch(JNIEnv *env, jobject thiz) {
35     return env->NewStringUTF(__ANDROID_ARCH__);
36 }
37 
38 static const char *classPathName = "com/android/cts/splitapp/Native";
39 
40 static JNINativeMethod methods[] = {
41     {"add", "(II)I", (void*)add },
42     {"arch", "()Ljava/lang/String;", (void*)arch },
43 };
44 
registerNativeMethods(JNIEnv * env,const char * className,JNINativeMethod * gMethods,int numMethods)45 static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods, int numMethods) {
46     jclass clazz;
47 
48     clazz = env->FindClass(className);
49     if (clazz == NULL) {
50         LOGE("Native registration unable to find class '%s'", className);
51         return JNI_FALSE;
52     }
53     if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
54         LOGE("RegisterNatives failed for '%s'", className);
55         return JNI_FALSE;
56     }
57 
58     return JNI_TRUE;
59 }
60 
registerNatives(JNIEnv * env)61 static int registerNatives(JNIEnv* env) {
62     if (!registerNativeMethods(env, classPathName, methods, sizeof(methods) / sizeof(methods[0]))) {
63         return JNI_FALSE;
64     }
65 
66     return JNI_TRUE;
67 }
68 
69 typedef union {
70     JNIEnv* env;
71     void* venv;
72 } UnionJNIEnvToVoid;
73 
JNI_OnLoad(JavaVM * vm,void * reserved)74 jint JNI_OnLoad(JavaVM* vm, void* reserved) {
75     UnionJNIEnvToVoid uenv;
76     uenv.venv = NULL;
77     jint result = -1;
78     JNIEnv* env = NULL;
79 
80     LOGI("JNI_OnLoad");
81 
82     if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {
83         LOGE("ERROR: GetEnv failed");
84         goto bail;
85     }
86     env = uenv.env;
87 
88     if (registerNatives(env) != JNI_TRUE) {
89         LOGE("ERROR: registerNatives failed");
90         goto bail;
91     }
92 
93     result = JNI_VERSION_1_4;
94 
95 bail:
96     return result;
97 }
98