android库项目包含一些提供者,它们的权限在contract类中定义如下:
public static final String CONTENT_AUTHORITY = "my.com.library.providers.tester";
private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
现在有很多应用程序项目都使用了这个库项目。我目前遇到的问题是,对于每个应用程序项目,我需要在库项目中为每个应用程序创建一个单独的分支,以便拥有唯一的内容授权。这造成了一些版本管理问题(比如将特性/bug修复从一个分支传播到每个其他分支,等等)。相反,我想将定义内容权限的责任委托给应用程序项目。有没有办法做到这一点?
发布于 2013-01-30 04:56:34
我知道这是一个古老的话题,但今天遇到了这个问题,我们已经开发了很长一段时间,所以还没有准备好在我们的内容提供商合同中完成所有静态操作并更改它们,这也是因为我们的内容提供商和DB是由Mechanoid Plugin for Eclipse生成的(是的,我也是作者!:)
我想出的解决方案是向我们生成的协定添加一个静态初始化器,它使用反射来查找类,并在其上使用静态CONTENT_AUTHORITY字段(如果它存在),如果它不存在,则退回到默认值:
public class QuxContract {
public static final String CONTENT_AUTHORITY = initAuthority();
private static String initAuthority() {
String authority = "com.example.app.data.qux";
try {
ClassLoader loader = QuxContract.class.getClassLoader();
Class<?> clz = loader.loadClass("com.example.app.data.QuxContentProviderAuthority");
Field declaredField = clz.getDeclaredField("CONTENT_AUTHORITY");
authority = declaredField.get(null).toString();
} catch (ClassNotFoundException e) {}
catch (NoSuchFieldException e) {}
catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
return authority;
}
private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
// ...
现在,在每个链接到库项目的项目中都可以提供自己的权限:
package com.example.app.data;
public class QuxContentProviderAuthority {
public static final String CONTENT_AUTHORITY = "com.example.app.data.baz";
}
此外,也不要忘记更改清单中的权限。
https://stackoverflow.com/questions/10790919
复制相似问题