android 源码开发app,c++无法通过jni调用自己写的类,FindClass返回空,但可以调用系统类

guiwuz 2018-09-27 10:26:34
环境:android源码6.0 放到源码目录package/app/编译,有谁遇到这种问题的
...全文
1147 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lmj_wince 2019-03-12
  • 打赏
  • 举报
回复
我也遇到了,FindClass是可以的,GetMethodID一直=0,安装到data/app一点问题都没
guiwuz 2018-10-09
  • 打赏
  • 举报
回复
自己在顶顶,遇到此问题的,能解答一下吗
guiwuz 2018-09-30
  • 打赏
  • 举报
回复
别沉啊,帖子,哪位大哥遇到过
guiwuz 2018-09-29
  • 打赏
  • 举报
回复
1、回复1楼:程序代码已经是你说的模式,我是抽取webrtc里面的模块
jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/)
{
if (!vm)
{
return -1;
}
JNIEnv* env;
if (JNI_OK != vm->GetEnv(reinterpret_cast<void**> (&env),
JNI_VERSION_1_4))
{
return -1;
}
memset(&veData1, 0, sizeof(veData1));
veData1.jvm = vm;
return JNI_VERSION_1_4;
}
int32_t AudioDeviceAndroidJni::SetAndroidAudioDeviceObjects(void* javaVM,
void* env,
void* context) {

// TODO(leozwang): Make this function thread-safe.
globalJvm = reinterpret_cast<JavaVM*>(javaVM);

if (env) {
globalJNIEnv = reinterpret_cast<JNIEnv*>(env);
// Get java class type (note path to class packet).
jclass javaScClassLocal = globalJNIEnv->FindClass(
"org/webrtc/voiceengine/WebRTCAudioDevice");
if (!javaScClassLocal) {
WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1,
"%s: could not find java class", __FUNCTION__);
return -1; // exception thrown
}
// Create a global reference to the class (to tell JNI that we are
// referencing it after this function has returned).
globalScClass = reinterpret_cast<jclass> (
globalJNIEnv->NewGlobalRef(javaScClassLocal));
if (!globalScClass) {
WEBRTC_TRACE(kTraceError, kTraceAudioDevice, -1,
"%s: could not create reference", __FUNCTION__);
return -1;
}
globalContext = globalJNIEnv->NewGlobalRef(
reinterpret_cast<jobject>(context));
if (!globalContext) {
return -1;
}
// Delete local class ref, we only use the global ref
globalJNIEnv->DeleteLocalRef(javaScClassLocal);
}
2、回复二楼androidRuntime.cpp这个文件在哪里,能说的详细点吗?
3、我暂时解决办法,但还是要查找具体原因:因为我用源码开发的,当我app编译到/system/app目录下无法FindClass,,编译到/system/priv-app/目录就可以,,,控制编译选项是mk文件中#LOCAL_PRIVILEGED_MODULE := true变量
usecf 2018-09-27
  • 打赏
  • 举报
回复
你在androidRuntime.cpp中注册你的方法
namespace android {的上面增加下面的
extern int register_android_hardware_HelloWorldCF(JNIEnv* env);


在这个方法里面增加你自己的
static const RegJNIRec gRegJNI[] = {
REG_JNI(register_android_hardware_HelloWorldCF),

在自己的cpp文件中android_hardware_HelloWorldCF.cpp

static void helloworld_init(JNIEnv* env, jobject clazz) {
ALOGE("helloworld_init env 1111111222222");
}

static const JNINativeMethod method_table[] = {
{"get_helloworld","()V",(void*)helloworld_init},

};


int register_android_hardware_HelloWorldCF(JNIEnv *env){

ALOGE("[cf] ===");
return jniRegisterNativeMethods(env,"android/hardware/HelloWorldCF",method_table,NELEM(method_table));
}

在android.mk中写编译脚本
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libhelloworld
LOCAL_SRC_FILES := android_hardware_HelloWorldCF
include $(BUILD_SHARED_LIBRARY)
进击的灵魂 2018-09-27
  • 打赏
  • 举报
回复
这涉及到ClassLoader和ClassPath的方面,这个在这里不讲解。说下解决办法。需要在JNI_OnLoad方法里调用FindClass,在调用NewGlobalRef,传给c++相关模块里。

80,356

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧