Repository navigation
Expand file tree
/
Copy pathhello-lib.c
More file actions
122 lines (88 loc) · 3.18 KB
/
Copy pathhello-lib.c
File metadata and controls
122 lines (88 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// Created by 飞飞 on 2018/6/12.
//
#include <jni.h>
#include <stdio.h>
#include <string.h>
#include <android/log.h>
#include<malloc.h>
#define LOG "JNILOG" // 这个是自定义的LOG的TAG
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG,__VA_ARGS__) // 定义LOGD类型
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG,__VA_ARGS__) // 定义LOGI类型
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG,__VA_ARGS__) // 定义LOGW类型
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG,__VA_ARGS__) // 定义LOGE类型
#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,LOG,__VA_ARGS__) // 定义LOGF类型
JNIEXPORT jstring JNICALL
Java_com_example_myndk_JNIUtils_stringFromJNI(JNIEnv *env, jobject instance) {
// TODO
LOGD("stringFromJNI was called");
return (*env)->NewStringUTF(env, "Hello World");
}
JNIEXPORT void JNICALL
Java_com_example_myndk_JNIUtils_writeFile(JNIEnv *env, jobject instance, jstring filePath_) {
const char *filePath = (*env)->GetStringUTFChars(env, filePath_, 0);
// TODO
FILE *file = fopen(filePath, "a+");
char data[] = "I am a boy";
int count = fwrite(data, strlen(data), 1, file);
if (file != NULL) {
fclose(file);
}
(*env)->ReleaseStringUTFChars(env, filePath_, filePath);
}
JNIEXPORT void JNICALL
Java_com_example_myndk_JNIUtils_operationArray(JNIEnv *env, jobject instance, jintArray args_) {
jint *args = (*env)->GetIntArrayElements(env, args_, NULL);
//获得数组指针
jint *arr = (*env)->GetIntArrayElements(env,args_,NULL);
//获得数组长度
jint len = (*env)->GetArrayLength(env,args_);
int i = 0;
for(;i < len ;i++){
++arr[i];
}
//释放资源
(*env)->ReleaseIntArrayElements(env, args_, args, 0);
}
JNIEXPORT void JNICALL
Java_com_example_myndk_JNIUtils_operationArray2(JNIEnv *env, jobject instance, jintArray args_) {
jint *args = (*env)->GetIntArrayElements(env, args_, NULL);
// TODO
//声明一个native层的数组,用于拷贝原数组
jint nativieArray[5];
//将传入的jintArray数组 拷贝到nativeArray
(*env)->GetIntArrayRegion(env,args_,0,5,nativieArray);
int i = 0;
for(;i<5;i++){
nativieArray[i]+=5;
}
//将操作完成的结果拷贝回jintArray
(*env)->SetIntArrayRegion(env,nativieArray,0,5,args_);
(*env)->ReleaseIntArrayElements(env, args_, args, 0);
}
static char * ptr ;
JNIEXPORT void JNICALL
Java_com_example_myndk_JNIUtils_initSDK(JNIEnv *env, jobject instance) {
int size = 100*1000*1000*1000;
ptr = (char *)malloc(size);
LOGD("feifei - 申请了 100M内存");
printf("feifei 测试测试:%s",&ptr);
}
JNIEXPORT void JNICALL
Java_com_example_myndk_JNIUtils_releaseSDK(JNIEnv *env, jobject instance) {
if(ptr != NULL){
free(ptr);
ptr = NULL;
LOGD("feifei - release ptr ");
} else{
LOGD("feifei - ptr == null No need 2 Release");
}
}
JNIEXPORT jlong JNICALL
Java_com_example_myndk_JNIUtils_natvieLock(JNIEnv *env, jobject instance) {
// TODO
}
JNIEXPORT jint JNICALL
Java_com_example_myndk_JNIUtils_getAge(JNIEnv *env, jobject instance, jlong natvieId) {
// TODO
}