基于之前写的RK3568 Android 11新增系统服务_rk3568 selinux 加规则-CSDN博客,在上面增加JNI调用例子。
步骤
1、新增JNI对应Java文件frameworks/base/services/core/java/com/android/server/SystemTestJni.java
package com.android.server;
import android.content.Context;
import android.os.RemoteException;
import com.android.server.SystemService;
import android.util.Slog;
public class SystemTestJni {
private final String TAG = "SystemTestJni";
public static native void nativeSetTestOne(int flag);
public static native int nativeSetTestTwo();
public static native boolean nativeSetTestThree(int flag, String path) ;
public static native String nativeSetTestFour(boolean flag) ;
public SystemTestJni() {
}
public void setTestOne(int flag) throws RemoteException {
try {
Slog.d(TAG, "------setTestOne--------flag:" + String.valueOf(flag));
nativeSetTestOne(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
public int setTestTwo() throws RemoteException {
try {
Slog.d(TAG, "------setTestTwo--------");
//return 66;
return nativeSetTestTwo();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public boolean setTestThree(int flag, String path) throws RemoteException {
try {
Slog.d(TAG, path + "------setTestThree--------flag:" + String.valueOf(flag));
//return false;
return nativeSetTestThree(flag, path);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public String setTestFour(boolean flag) throws RemoteException {
try {
Slog.d(TAG, "------setTestFour--------flag:" + String.valueOf(flag));
//return "abc";
return nativeSetTestFour(flag);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
2、新增JNI对应CPP文件frameworks/base/services/core/jni/com_android_server_SystemTestJni.cpp
/*
* Copyright 2018 Rockchip Electronics Co. LTD
*
* 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.
*
*/
#define LOG_TAG "SystemTestJniCpp"
#include <cutils/log.h>
#include <cutils/properties.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <jni.h>
#include <linux/netlink.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedUtfChars.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/socket.h>
#include <iostream>
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include "android/graphics/Bitmap.h"
namespace android {
static void nativeSetTestOne(JNIEnv *env, jobject obj, jint flag) {
ALOGD("----run nativeTestOne----flag:%d", flag);
}
static jint nativeSetTestTwo(JNIEnv *env, jobject obj) {
ALOGD("----run nativeSetTestTwo----");
jint value = 678;
return value;
}
static jboolean nativeSetTestThree(JNIEnv *env, jobject obj, jint flag, jstring path) {
const char *cstr;
cstr = env->GetStringUTFChars(path, 0);
ALOGD("----run nativeSetTestThree----flag: %d,path:%s", flag, cstr);
env->ReleaseStringUTFChars(path, cstr);
jboolean value = true;
return value;
}
static jstring nativeSetTestFour(JNIEnv *env, jobject obj, jboolean flag) {
bool val = flag ? JNI_TRUE : JNI_FALSE;
const char *cstr;
if (val) {
cstr = "true";
} else {
cstr = "false";
}
ALOGD("----run nativeSetTestFour----flag: %s", cstr);
jstring str = env->NewStringUTF("abcdef");
return str;
}
// ----------------------------------------------------------------------------
// com.android.server.SystemTestJni
static const JNINativeMethod sSystemTestJniMethods[] = {
{"nativeSetTestOne", "(I)V", (void *)nativeSetTestOne},
{"nativeSetTestTwo", "()I", (void *)nativeSetTestTwo},
{"nativeSetTestThree", "(ILjava/lang/String;)Z", (void *)nativeSetTestThree},
{"nativeSetTestFour", "(Z)Ljava/lang/String;", (void *)nativeSetTestFour},
};
#define FIND_CLASS(var, className) \
var = env->FindClass(className); \
LOG_FATAL_IF(!var, "Unable to find class " className);
#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
var = env->GetMethodID(clazz, methodName, methodDescriptor); \
LOG_FATAL_IF(!var, "Unable to find method " methodName);
#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
LOG_FATAL_IF(!var, "Unable to find field " fieldName);
int register_com_android_server_SystemTestJni(JNIEnv *env) {
int res = jniRegisterNativeMethods(env, "com/android/server/SystemTestJni",
sSystemTestJniMethods, NELEM(sSystemTestJniMethods));
LOG_FATAL_IF(res < 0,
"Unable to register native methods "
"register_com_android_server_SystemTestJni");
(void)res; // Don't complain about unused variable in the LOG_NDEBUG case
jclass clazz;
FIND_CLASS(clazz, "com/android/server/SystemTestJni");
return 0;
}
} // namespace android
3、修改Android.bp 文件路径
frameworks/base/services/core/jni/Android.bp
"com_android_server_rkdisplay_RkDisplayModes.cpp",
"com_android_server_audio_RkAudioSetting.cpp",
"onload.cpp",
"com_android_server_SystemTestJni.cpp", //新增
":lib_networkStatsFactory_native",
],
4、修改onload.cpp文件路径
frameworks/base/services/core/jni/onload.cpp
int register_android_server_GpuService(JNIEnv* env);
int register_com_android_server_rkdisplay_RkDisplayModes(JNIEnv* env);
int register_com_android_server_audio_RkAudioSetting(JNIEnv* env);
int register_com_android_server_SystemTestJni(JNIEnv* env); //新增
};
using namespace android;
register_android_server_GpuService(env);
register_com_android_server_rkdisplay_RkDisplayModes(env);
register_com_android_server_audio_RkAudioSetting(env);
register_com_android_server_SystemTestJni(env); //新增
return JNI_VERSION_1_4;
}
5、修改系统服务的调用
frameworks/base/services/core/java/com/android/server/SystemTestService.java
package com.android.server;
import android.app.SystemTestManager;
import android.content.Context;
import android.os.test.ISystemTestService;
import android.os.RemoteException;
import com.android.server.SystemService;
import com.android.internal.app.IAppOpsService;
import android.util.Slog;
public class SystemTestService extends SystemService {
private final String TAG = "SystemTestService";
private Context mContext;
private IAppOpsService mAppOps;
private SystemTestManager mManager;
private SystemTestJni mSystemTestJni;
public SystemTestService(Context context) {
super(context);
this.mContext = context;
mSystemTestJni=new SystemTestJni();
}
public void systemReady(IAppOpsService appOps) {
mAppOps = appOps;
if (mManager == null) {
mManager = (SystemTestManager) mContext.getSystemService(Context.SYSTEMTEST_SERVICE);
}
}
@Override
public void onStart() {
publishBinderService(Context.SYSTEMTEST_SERVICE, new BinderService());
}
private final class BinderService extends ISystemTestService.Stub {
@Override
public void setTestOne(int flag) throws RemoteException {
try {
Slog.d(TAG, "------setTestOne--------flag:" + String.valueOf(flag));
mSystemTestJni.setTestOne(flag);//JNI调用
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int setTestTwo() throws RemoteException {
try {
Slog.d(TAG, "------setTestTwo--------");
return mSystemTestJni.setTestTwo();//JNI调用
//return 66;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
@Override
public boolean setTestThree(int flag, String path) throws RemoteException {
try {
Slog.d(TAG, path + "------setTestThree--------flag:" + String.valueOf(flag));
return mSystemTestJni.setTestThree(flag, path);//JNI调用
//return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public String setTestFour(boolean flag) throws RemoteException {
try {
Slog.d(TAG, "------setTestFour--------flag:" + String.valueOf(flag));
// return "abc";
return mSystemTestJni.setTestFour(flag);//JNI调用
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
}
6、修改完毕编译后调用即可。