package kaiqi.cn.demo001;
public class Key {
public static native String hello();
public static void init() {
System.loadLibrary("hello");
}
}
public class MainActivity extends AppCompatActivity {
public static String KEY = "30001";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et = (EditText)findViewById(R.id.ets);//submit
Button sub = (Button)findViewById(R.id.submit);//submit
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(KEY.equals(et.getText().toString().trim())){
Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show();
}
}
});
Key.init();
KEY = Key.hello();
}
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -lm -llog -landroid
LOCAL_MODULE := hello
LOCAL_SRC_FILES := K.cpp
include $(BUILD_SHARED_LIBRARY)
K.cpp
#include <jni.h>
#include <stdio.h>
#include <string.h>
extern "C" jstring statTst(JNIEnv* env, jobject thiz);
//(void*) 指针
JNINativeMethod metheds[] = {
// java方法名,函数签名(参数列表)-返回值,java映射成C世界的方法
{ "hello", "()Ljava/lang/String;", (void*) statTst } };
jstring statTst(JNIEnv* env, jobject thiz) {
return env->NewStringUTF("2000");
}
jint JNI_OnLoad(JavaVM* vm, void* reserved) {
jint result = JNI_ERR;
JNIEnv* env = NULL;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
return result;
}
jclass clazz = env->FindClass("kaiqi/cn/demo001/Key");
if (clazz == NULL) {
return result;
}
if (env->RegisterNatives(clazz, metheds,
sizeof(metheds) / sizeof(metheds[0])) < 0) {
return result;
}
result = JNI_VERSION_1_4;
return result;
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "kaiqi.cn.demo001"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
// externalNativeBuild {
// //cmake {...}
// cmake {
// }
// }
ndk{
moduleName "hello2" // 生成hello.so文件
ldLibs "log"//实现__android_log_print
// abiFilters "armeabi", "armeabi-v7a", "x86" //输出指定三种abi体系结构下的so库。
abiFilters "armeabi-v7a" //输出指定三种abi体系结构下的so库。
// abiFilters "x86" //输出指定三种abi体系结构下的so库。
// abiFilters "armeabi" //输出指定三种abi体系结构下的so库。
}
sourceSets{
main {
//jniLibs.srcDirs = ['src/main/libs']
jni.srcDirs = []
jniLibs.srcDir "src/main/libs"
}
}
}
// packagingOptions{
// doNotStrip '*/mips/*.so'
// doNotStrip '*/mips64/*.so'
// }
// externalNativeBuild {
// ndkBuild {
// path 'src/main/jni/*.cpp'
// }
// }
externalNativeBuild {
ndkBuild {
//path file("src/main/jni/Android.mk")
path "src/main/jni/Android.mk"
}
}
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
// sourceSets { main { jni .srcDirs= ['src/main/jni','src/main/jni/'] } }
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
}
网友评论