MainActivity.java
package com.nb.s2long;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 當(dāng)頁(yè)面加載時(shí)间驮,自動(dòng)執(zhí)行
int v1 = EncryptUtils.s1(11, 22);
Log.e("v1是", String.valueOf(v1)); // v1=33
String v2 = EncryptUtils.s2("wupeiqi");
Log.e("v2是", String.valueOf(v2));
String v3 = EncryptUtils.s3("wupeiqi");
Log.e("v3是", String.valueOf(v3)); // v1=33
String v4 = EncryptUtils.s4("wupeiqi");
Log.e("v4是", String.valueOf(v4)); // v1=33
String v5 = EncryptUtils.s5("wupeiqi");
Log.e("v5是", String.valueOf(v5));
String v6 = EncryptUtils.s6("wupeiqi");
Log.e("v6是", String.valueOf(v6)); // 77757065697169
String v7 = EncryptUtils.s7();
Log.e("v7是", String.valueOf(v7));
String v8 = EncryptUtils.s8();
Log.e("v8是", String.valueOf(v8));
int v9 = DynamicUtils.add(11,2);
Log.e("v9是", String.valueOf(v9));
}
}
EncryptUtils.java
package com.nb.s2long;
class EncryptUtils {
static {
System.loadLibrary("encrypt");
}
public static native int s1(int v1, int v2);
public static native String s2(String origin);
public static native String s3(String origin);
public static native String s4(String origin);
public static native String s5(String origin);
public static native String s6(String origin);
public static native String s7();
public static native String s8();
}
encrypt.c
//
// Created by xl on 2/24/22.
//
#include <jni.h>
#include <string.h>
#include <stdio.h>
JNIEXPORT jint
JNICALL Java_com_nb_s2long_EncryptUtils_s1(JNIEnv *env, jclass obj, jint v1, jint v2) {
return v1 + v2;
}
JNIEXPORT jstring
JNICALL Java_com_nb_s2long_EncryptUtils_s2(JNIEnv *env, jclass obj, jstring origin) {
char data[] = "hello";
// C語(yǔ)言中的字符串轉(zhuǎn)換成jstring
jstring response = (*env)->NewStringUTF(env, data);
return response; // jstring -> Java的字符串
}
JNIEXPORT jstring
JNICALL Java_com_nb_s2long_EncryptUtils_s3(JNIEnv *env, jclass obj, jstring origin) {
// 獲取參數(shù)中傳入的字符串
const char *string = (*env)->GetStringUTFChars(env, origin, 0);
char data[4] = {string[0], string[2], string[4]}; // wpi
// C語(yǔ)言中的字符串轉(zhuǎn)換成jstring
jstring response = (*env)->NewStringUTF(env, data);
return response; // jstring -> Java的字符串
}
JNIEXPORT jstring
JNICALL Java_com_nb_s2long_EncryptUtils_s4(JNIEnv *env, jclass obj, jstring origin) {
// 獲取參數(shù)中傳入的字符串
const char *string = (*env)->GetStringUTFChars(env, origin, 0);
char *data = "root";
char buffer[100] = {};
strcat(buffer, string);
strcat(buffer, data);
// C語(yǔ)言中的字符串轉(zhuǎn)換成jstring
jstring response = (*env)->NewStringUTF(env, buffer);
return response; // jstring -> Java的字符串
}
JNIEXPORT jstring
JNICALL Java_com_nb_s2long_EncryptUtils_s5(JNIEnv *env, jclass obj, jstring origin) {
char v34[80]; // ['0','1',,,,,,,,,]
char *v28 = (char *) &v34;
sprintf(v28, "%02x", 1); // 01
v28 += 2;
sprintf(v28, "%02x", 2); // 02
v28 += 2;
sprintf(v28, "%02x", 9); // 09
v28 += 2;
sprintf(v28, "%02x", 12); // 0b
v28 += 2;
sprintf(v28, "%02x", 20); // 14
v28 += 2;
sprintf(v28, "%02x", 'a'); // 'a'字符 十進(jìn)制 97 十六進(jìn)制61
// C語(yǔ)言中的字符串轉(zhuǎn)換成jstring
jstring response = (*env)->NewStringUTF(env, v34);
return response; // jstring -> Java的字符串
}
JNIEXPORT jstring
JNICALL Java_com_nb_s2long_EncryptUtils_s6(JNIEnv *env, jclass obj, jstring origin) {
// char v36[] = {'a', 'b', 'c', 'd', 'e', 'f'}; // 616263646566
const char *v36 = (*env)->GetStringUTFChars(env, origin, 0);
const int size = (*env)->GetStringLength(env, origin);
char v34[80]; // [6,1,,,,,,,,]
char *v28 = (char *) &v34;
int v29 = 0;
do {
sprintf(v28, "%02x", v36[v29++]);
v28 += 2;
} while (v29 != size);
// C語(yǔ)言中的字符串轉(zhuǎn)換成jstring
jstring response = (*env)->NewStringUTF(env, v34);
return response; // jstring -> Java的字符串
}
JNIEXPORT jstring
JNICALL Java_com_nb_s2long_EncryptUtils_s7(JNIEnv *env, jclass obj) {
char buffer[100] = {};
char *data = "root";
// 調(diào)用Java中的某個(gè)類(lèi)中的某個(gè)靜態(tài)方法猿诸,獲取值。
// 找到類(lèi)
jclass cls = (*env)->FindClass(env, "com/nb/s2long/DbHelper");
// 類(lèi)中找方法(靜態(tài)方法)
jmethodID method = (*env)->GetStaticMethodID(env, cls, "getPrev", "()Ljava/lang/String;");
// 執(zhí)行方法
jstring res = (*env)->CallStaticObjectMethod(env, cls, method);
// C語(yǔ)言的字節(jié)數(shù)組
const char *string = (*env)->GetStringUTFChars(env, res, 0);
strcat(buffer, data);
strcat(buffer, string);
// C語(yǔ)言中的字符串轉(zhuǎn)換成jstring
jstring response = (*env)->NewStringUTF(env, buffer);
return response; // jstring -> Java的字符串
}
JNIEXPORT jstring
JNICALL Java_com_nb_s2long_EncryptUtils_s8(JNIEnv *env, jclass obj) {
char buffer[100] = {};
char *data = "root";
// 調(diào)用Java中的某個(gè)類(lèi)中的某個(gè)靜態(tài)方法绞蹦,獲取值。
// 找到類(lèi)
jclass cls = (*env)->FindClass(env, "com/nb/s2long/DbHelper");
// 類(lèi)中找方法(靜態(tài)方法)
jmethodID method1 = (*env)->GetStaticMethodID(env, cls, "getPrev", "()Ljava/lang/String;");
// 執(zhí)行方法
jstring res1 = (*env)->CallStaticObjectMethod(env, cls, method1);
// C語(yǔ)言的字節(jié)數(shù)組
const char *string1 = (*env)->GetStringUTFChars(env, res1, 0);
// 類(lèi)中找方法(靜態(tài)方法)
jmethodID method2 = (*env)->GetStaticMethodID(env, cls, "getEnding",
"(Ljava/lang/String;II)Ljava/lang/String;");
// 執(zhí)行方法
jstring res2 = (*env)->CallStaticObjectMethod(env, cls, method2,
(*env)->NewStringUTF(env, "xxx"), 11, 22);
// C語(yǔ)言的字節(jié)數(shù)組
const char *string2 = (*env)->GetStringUTFChars(env, res2, 0);
// 找到FileHelper實(shí)例化并執(zhí)行他的getData
jclass cls2 = (*env)->FindClass(env, "com/nb/s2long/FileHelper");
// 先找構(gòu)造方法
jmethodID init = (*env)->GetMethodID(env, cls2, "<init>", "(II)V");
// 實(shí)例化對(duì)象
jobject cls_obj = (*env)->NewObject(env, cls2, init, 11, 22);
// 找getData方法
jmethodID mid = (*env)->GetMethodID(env, cls2, "getData", "()Ljava/lang/String;");
// 執(zhí)行方法
jstring res3 = (*env)->CallObjectMethod(env,cls_obj,mid);
const char *string3 = (*env)->GetStringUTFChars(env, res3, 0);
strcat(buffer, data);
strcat(buffer, string1);
strcat(buffer, string2);
strcat(buffer, string3);
// C語(yǔ)言中的字符串轉(zhuǎn)換成jstring
jstring response = (*env)->NewStringUTF(env, buffer);
return response; // jstring -> Java的字符串
}