最近,我观察到在使用 Android P 开发者预览版时,我维护的一个应用程序出现了大量崩溃。
潜入(深入)项目代码,我发现问题方法如下:
public static <T> T get(MatrixCursor cursor, int column) {
try {
cursor.moveToFirst();
Method get = MatrixCursor.class.getDeclaredMethod("get", int.class);
get.setAccessible(true);
return (T) get.invoke(cursor, column);
} catch (Exception e) {
throw new IllegalArgumentException("Android has changed the implementation of MatrixCursor?!");
}
}
据我了解,此代码用于直接从 MatrixCursor 中检索自定义对象,而不是原始类型、字节数组或字符串。以前有一个私有方法在MatrixCursor
内部执行此操作,我们通过反射访问此方法。
不用说,这种方法存在许多问题。据我所知,访问私有 API 的反射是 Android强烈反对的一项功能。尽管如此,在 Android P 预览版之前,这似乎一直在按预期工作。
这导致我提出以下问题:
的实现是否已MatrixCursor
更改或从 Android P 开始完全弃用反射?
可悲的是,我不是 100% 知道我必须使用哪些替代方法来避免这个问题。非常感谢任何建议,是否有可用于存储自定义对象的光标?