我正在为Android编写一个库,希望它专门使用BuildConfig中的常量-因此,可以这么说,lib的客户端不会轻易看到它们。
所以,我想实现的不是这样的公共常量:
package my.lib;
public final class BuildConfig {
public static final boolean FOO = false;
}
它宁愿生成一个没有访问修饰符的常量,而是使该东西在我的lib包中可见:
package my.lib;
public final class BuildConfig {
static final boolean FOO = false;
}
能否以某种方式实现? 谢谢!
答案 0 :(得分:1)
这是generate()
类中的BuildConfigGenerator
方法:
/**
* Generates the BuildConfig class.
*/
public void generate() throws IOException {
File pkgFolder = getFolderPath();
if (!pkgFolder.isDirectory()) {
if (!pkgFolder.mkdirs()) {
throw new RuntimeException("Failed to create " + pkgFolder.getAbsolutePath());
}
}
File buildConfigJava = new File(pkgFolder, BUILD_CONFIG_NAME);
FileWriter out = new FileWriter(buildConfigJava);
JavaWriter writer = new JavaWriter(out);
Set<Modifier> publicFinal = EnumSet.of(Modifier.PUBLIC, Modifier.FINAL);
Set<Modifier> publicFinalStatic = EnumSet.of(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC);
writer.emitJavadoc("Automatically generated file. DO NOT MODIFY")
.emitPackage(mBuildConfigPackageName)
.beginType("BuildConfig", "class", publicFinal);
for (ClassField field : mFields) {
writer.emitField(
field.getType(),
field.getName(),
publicFinalStatic,
field.getValue());
}
for (Object item : mItems) {
if (item instanceof ClassField) {
ClassField field = (ClassField)item;
writer.emitField(
field.getType(),
field.getName(),
publicFinalStatic,
field.getValue());
} else if (item instanceof String) {
writer.emitSingleLineComment((String) item);
}
}
writer.endType();
out.close();
}
}
所以这是不可能的,因为BuildConfigGenerator
仅创建public final
修饰符
Set<Modifier> publicFinal = EnumSet.of(Modifier.PUBLIC, Modifier.FINAL);
writer.emitJavadoc("Automatically generated file. DO NOT MODIFY")
.emitPackage(mBuildConfigPackageName)
.beginType("BuildConfig", "class", publicFinal);
,但您无法选择)