从本机代码访问生成的 Android R 类
我想知道是否有一种方法可以从本机代码访问 R 类属性,我需要它来读取一些生成的 ID,这些 ID 可能会在每次我对项目进行干净构建时发生变化,并且我不希望将它们手动传递给原生部分。
编辑 正如 @trashkalmar 所建议的,这是解决方案:
static const char* const strClassName = "your/app/package/R$string";
clazz = env->FindClass(strClassName);
if (clazz == NULL) {
LOGE("Can't find class %s\n", strClassName);
return result;
}
jfieldID field = env->GetStaticFieldID(clazz , "you_string_resource", "I");
jint value = env->GetStaticIntField(clazz, field);
I'm wondering if there is a way to access R class attribute from native code, I need it to read some generated ID that may change every time i do a clean build of my project and I would prefer not to pass them manually to the native part.
EDIT
As suggested from @trashkalmar here is the solution:
static const char* const strClassName = "your/app/package/R$string";
clazz = env->FindClass(strClassName);
if (clazz == NULL) {
LOGE("Can't find class %s\n", strClassName);
return result;
}
jfieldID field = env->GetStaticFieldID(clazz , "you_string_resource", "I");
jint value = env->GetStaticIntField(clazz, field);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
访问您的 R 类并像任何其他类一样读取其字段。
Access your R class and read its fields as any other classes.