static void setBuild(JNIEnv
*
env) {
/
/
查找 Build 类
jclass BuildClass
=
env
-
>FindClass(
"android/os/Build"
);
if
(BuildClass
=
=
NULL) {
LOGD(
"Failed to find class android/os/Build"
);
return
;
}
/
/
遍历 buildMap 设置属性
for
(auto it
=
buildMap.begin(); it !
=
buildMap.end();
+
+
it) {
LOGD(
"Setting build prop -> key = %s, value = %s"
, it
-
>first.c_str(), it
-
>second.c_str());
/
/
创建 key 和 value 的 JNI 字符串
jstring key
=
env
-
>NewStringUTF(it
-
>first.c_str());
jstring value
=
env
-
>NewStringUTF(it
-
>second.c_str());
/
/
检查创建结果
if
(key
=
=
NULL || value
=
=
NULL) {
LOGD(
"Failed to create JNI string for key or value."
);
if
(key !
=
NULL) env
-
>DeleteLocalRef(key);
if
(value !
=
NULL) env
-
>DeleteLocalRef(value);
continue
;
}
/
/
获取属性字段
ID
jfieldID buildField
=
env
-
>GetStaticFieldID(BuildClass, it
-
>first.c_str(),
"Ljava/lang/String;"
);
if
(env
-
>ExceptionCheck() || buildField
=
=
NULL) {
env
-
>ExceptionClear();
LOGD(
"Failed to get field ID for key = %s"
, it
-
>first.c_str());
env
-
>DeleteLocalRef(key);
env
-
>DeleteLocalRef(value);
continue
;
}
/
/
设置字段值
env
-
>SetStaticObjectField(BuildClass, buildField, value);
if
(env
-
>ExceptionCheck()) {
env
-
>ExceptionClear();
LOGD(
"Failed to set field for key = %s"
, it
-
>first.c_str());
}
/
/
释放局部引用
env
-
>DeleteLocalRef(key);
env
-
>DeleteLocalRef(value);
}
/
/
释放 BuildClass 的引用
env
-
>DeleteLocalRef(BuildClass);
}