Android jni动态注册

按照步骤执行

local.properties文件(sdk和ndk配置路径):

## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=E\:\\AndroidSDK
ndk.dir=E\:\\AndroidNDK\\android-ndk-r14b

CMakeLists.txt文件 :

cmake_minimum_required(VERSION 3.4.1)

include_directories( ${CMAKE_SOURCE_DIR}/head)
add_library(jni_more SHARED  ${CMAKE_SOURCE_DIR}/cpp/More.cpp)

find_library(lib-log log)

target_link_libraries(jni_more ${lib-log})

app :  build.gradle文件:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.zzm.more"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        externalNativeBuild{
            cmake{
               abiFilters "arm64-v8a" , "armeabi-v7a"
            }
        }
    }
    externalNativeBuild {
        cmake{
            path "src/main/jni/CMakeLists.txt"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

MainActivity.java文件:

package com.zzm.more;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {

    static {
        System.loadLibrary("jni_more");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("22m",test("zengzeming"));
    }

    public static native String test(String name);
}

xxx.cpp文件:

#include "jni.h"
#include "android/log.h"
//#include "../head/more.h"
#include <stdio.h>
 jstring  test(JNIEnv* env,jclass ,jstring name){
     const char* name_char=env->GetStringUTFChars(name,false);
    __android_log_print(ANDROID_LOG_INFO,"22m","name : %s",name_char);
    return env->NewStringUTF("zengzeming");
}

const  char* classPathName="com/zzm/more/MainActivity";
const JNINativeMethod methods[]={
        { "test","(Ljava/lang/String;)Ljava/lang/String;",(void*)test }
};
int registerNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods,int numMethods){
    jclass clazz;
    clazz=env->FindClass(className);
    if(clazz==NULL){
        __android_log_print(ANDROID_LOG_INFO,"22m","native registration unable to find class '%s'",className);
        return  JNI_FALSE;
    }
    if(env->RegisterNatives(clazz,gMethods,numMethods)<0){
        __android_log_print(ANDROID_LOG_INFO,"22m","RegisterNatives failed for  '%s'",className);
        return  JNI_FALSE;
    }
    return JNI_TRUE;
}

int registerNatives(JNIEnv* env){
if(!registerNativeMethods(env,classPathName,methods, sizeof(methods)/ sizeof(methods[0]))){
    return  JNI_FALSE;
}
return JNI_TRUE;
}

typedef  union {
    JNIEnv* env;
    void* venv;
}UnionJNIEnvToVoid;

 jint  JNI_OnLoad(JavaVM* vm, void* ){
    UnionJNIEnvToVoid uenv;
    uenv.venv=NULL;
    jint  result =-1;
    JNIEnv* env=NULL;
     __android_log_print(ANDROID_LOG_INFO,"22m","JNI_OnLoad");
     if(vm->GetEnv(&uenv.venv,JNI_VERSION_1_6)!=JNI_OK){
         __android_log_print(ANDROID_LOG_INFO,"22m","ERROR: GetEnv failed");
         goto fail;
     }
     env=uenv.env;
     if(registerNatives(env)!=JNI_TRUE){
         __android_log_print(ANDROID_LOG_INFO,"22m","ERROR: registerNatives failed");
         goto fail;
     }
     result=JNI_VERSION_1_6;
     fail:
     return  result;
}

run起来的log信息:

2019-01-05 07:20:12.808 30571-30571/com.zzm.more I/22m: JNI_OnLoad
2019-01-05 07:20:13.100 30571-30571/com.zzm.more I/22m: name : zengzeming
2019-01-05 07:20:13.100 30571-30571/com.zzm.more I/22m: zengzeming

 

 

转载于:https://my.oschina.net/u/2987490/blog/3049040

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值