public class ReflectUtil {
static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("accessFlags");
modifiersField.setAccessible(true);
int originModifier = field.getModifiers();
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
modifiersField.setInt(field, originModifier);
}
}
public class DeviceUtil {
public static String sysProperty(String key, String defValue) {
String res = null;
try {
@SuppressLint("PrivateApi") Class<?> clazz = Class.forName("android.os.SystemProperties");
Method method = clazz.getMethod("get", new Class<?>[]{String.class, String.class});
res = (String) method.invoke(clazz, new Object[]{key, defValue});
if (res == null || res.isEmpty()) {
return defValue;
}
} catch (Exception e) {
LogUtil.print("System property invoke error: " + e);
}
return res;
}
}
ReflectUtil.setFinalStatic(Build::class.java.getField("MANUFACTURER"), DeviceUtil.sysProperty("ro.product.manufacturer", Build.MANUFACTURER))
ReflectUtil.setFinalStatic(Build::class.java.getField("BRAND"), DeviceUtil.sysProperty("ro.product.brand", Build.BRAND))
ReflectUtil.setFinalStatic(Build::class.java.getField("MODEL"), DeviceUtil.sysProperty("ro.product.model", Build.MODEL))
ReflectUtil.setFinalStatic(Build::class.java.getField("DEVICE"), DeviceUtil.sysProperty("ro.product.device", Build.DEVICE))
ReflectUtil.setFinalStatic(Build::class.java.getField("PRODUCT"), DeviceUtil.sysProperty("ro.product.name", Build.PRODUCT))
ReflectUtil.setFinalStatic(Build::class.java.getField("FINGERPRINT"), DeviceUtil.sysProperty("ro.build.fingerprint", Build.FINGERPRINT))
ReflectUtil.setFinalStatic(Build.VERSION::class.java.getField("SDK_INT"), DeviceUtil.sysProperty("ro.build.version.sdk", Build.VERSION.SDK_INT.toString()).toInt())
ReflectUtil.setFinalStatic(Build.VERSION::class.java.getField("INCREMENTAL"), DeviceUtil.sysProperty("ro.build.version.incremental", Build.VERSION.INCREMENTAL))
ReflectUtil.setFinalStatic(Build.VERSION::class.java.getField("RELEASE"), DeviceUtil.sysProperty("ro.build.version.release", Build.VERSION.RELEASE))
ReflectUtil.setFinalStatic(Build.VERSION::class.java.getField("SECURITY_PATCH"), DeviceUtil.sysProperty("ro.build.version.security_patch", Build.VERSION.SECURITY_PATCH))