android9.0 java静态库操作JNI实例 动态注册

一、java层

源码

目录:/Demo/java/com/android/simpleini/SimpleJNI.java


package com.example.simplejni;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SimpleJNI {
      private IpoManagerService(Context context) {
          nativeTest.LoadLibrary();
         if(nativeTest.mIsLoadNativeLibStatus){
            nativeTest.native_setIpoInitSuccFlag();
            Log.d(TAG, "nativeTest.native_setIpoInitSuccFlag");
        }else{
               Log.d(TAG,"nativeTest.mIsLoadNativeLibStatus="
                            +nativeTest.mIsLoadNativeLibStatus);
        }
    }
    
}


class nativeTest{
    private final static String TAG_LOG = "nativeTest";
    static void LoadLibrary(){
        try {
            System.loadLibrary("nativeTest");
            mIsLoadNativeLibStatus = true;
            Log.d(TAG_LOG, "loadLibrary nativeTestJni OK");
        } catch (UnsatisfiedLinkError e) {
            Log.e(TAG_LOG, "loadLibrary UnsatisfiedLinkError = " + e.toString());
        } catch (Exception e) {
            Log.e(TAG_LOG, "loadLibrary Exception = " + e.toString());
        }
    }
    static native  void native_setIpoInitSuccFlag();
    static boolean mIsLoadNativeLibStatus = false;


}

android.bp

java_library_static {
   name: "SimpleJNI",
   
   libs: [
       "xxx",
   ],
   
   required:[
        "libnativeTest",
   ],

   srcs: ["java/**/*.java"],
   optimize: {
       enabled: false,
   },
   
}

二、JNI层

代码

源码:/demo/jni/nativeTest.cpp

/*
	third screen brightness adjustment
*/
#define LOG_TAG "JNI_NativeTest"
#if 0
#include "jni.h"
#include <nativehelper/JNIHelp.h>
#include "android_runtime/AndroidRuntime.h"

#include <utils/misc.h>
#include <cutils/log.h>

#include <stdio.h>
#include<sstream>

#include <android-base/properties.h>
#include<iostream>
#endif

#include <utils/Log.h>
#include <stdio.h>
#include <jni.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedUtfChars.h>
#include <android_runtime/AndroidRuntime.h>
#include "core_jni_helpers.h"
#include <string.h>
#include <cutils/properties.h>


#define IPOINITOK_IPOMS_BSP_FIELPATH "/xx/xx/xx" //操作文件节点名称

static const char *kClassPathName = "com/android/simplejni/nativeTest";


static int write_int_to_bsp(char const* path, char* buffer, int bytes)
{
		int fd;
	
		if (path == NULL)
		{
		ALOGE("the path==NULL");
		return -1;
		}
	
		fd = open(path, O_RDWR);
		if (fd >= 0) {
			int amt = write(fd, buffer, bytes);
			close(fd);
			if(amt == -1)
			{
				ALOGE("write_int_to_bsp write [%s] failed! errno=%d", path,errno);
				return -errno;
			}
			else
			{
				ALOGI("write ok");
				return 0;
			}
		}
		else
		{
		ALOGE("write_int_to_bsp open [%s] failed! errno=%d", path,errno);
		return -errno;
		}

}
	
static void native_setIpoInitSuccFlag(JNIEnv *env, jobject thiz)
{
		char ipoinit_succ[1] = {1};
		ALOGD("enter JNI native_setIpoInitSuccFlag()");
		write_int_to_bsp(IPOINITOK_IPOMS_BSP_FIELPATH,ipoinit_succ,1);
}


static  JNINativeMethod methods[] = {
	{"native_setIpoInitSuccFlag", "()V", (void *)native_setIpoInitSuccFlag},
};

	/*
int register_com_mediatek_ipomanager_IpoManagerService(JNIEnv *env) {
	return jniRegisterNativeMethods(env, kClassPathName, method_table, NELEM(method_table));
}
*/

/*
* Register several native methods for one class.
*/
static int registerNativeMethods(JNIEnv* env, const char* className,
	JNINativeMethod* gMethods, int numMethods)
{
	jclass clazz;

	clazz = env->FindClass(className);
	if (clazz == NULL) {
		ALOGE("Native registration unable to find class '%s'", className);
		return JNI_FALSE;
	}
	if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
		ALOGE("RegisterNatives failed for '%s'", className);
		return JNI_FALSE;
	}

	return JNI_TRUE;
}


/*
* Register native methods for all classes we know about.
*
* returns JNI_TRUE on success.
*/
static int registerNatives(JNIEnv* env)
{
if (!registerNativeMethods(env, kClassPathName,
				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* /*reserved*/)
{
	UnionJNIEnvToVoid uenv;
	uenv.venv = NULL;
	jint result = -1;
	JNIEnv* env = NULL;
	
	ALOGI("JNI_OnLoad");

	if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {
		ALOGE("ERROR: GetEnv failed");
		goto bail;
	}
	env = uenv.env;

	if (registerNatives(env) != JNI_TRUE) {
		ALOGE("ERROR: registerNatives failed");
		goto bail;
	}
	
	result = JNI_VERSION_1_4;
	
bail:
	return result;
}

android.mk

路径:/demo/jni/android.mk

#

# Copyright (C) 2008 The Android Open Source Project

#

# Licensed under the Apache License, Version 2.0 (the "License");

# you may not use this file except in compliance with the License.

# You may obtain a copy of the License at

#

#      http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.

#

# This makefile supplies the rules for building a library of JNI code for

# use by our example platform shared library.

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

# This is the target being built.

LOCAL_MODULE:= libnativeTest

# All of the source files that we will compile.

LOCAL_SRC_FILES:= \

    nativeTest.cpp

# All of the shared libraries we link against.

LOCAL_SHARED_LIBRARIES := \

    libnativehelper \

    libcutils \

    libutils \

    liblog \

    libandroid_runtime

# No static libraries.

LOCAL_STATIC_LIBRARIES :=

# Also need the JNI headers.

LOCAL_C_INCLUDES += \

    $(JNI_H_INCLUDE)

LOCAL_CFLAGS += -Wall -Werror -Wunused-parameter -Wno-unused-parameter

include $(BUILD_SHARED_LIBRARY)




 

猜你喜欢

转载自blog.csdn.net/BersonKing/article/details/129183012