Java Android注释处理器访问资源(资产)
我想从注释处理器中的andoid studio项目访问资源 我首先尝试使用filer中的getResource方法:Java Android注释处理器访问资源(资产),java,android,android-resources,annotation-processing,Java,Android,Android Resources,Annotation Processing,我想从注释处理器中的andoid studio项目访问资源 我首先尝试使用filer中的getResource方法: FileObject fo = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", "src/main/res/layout/activity_main.xml"); ,但它总是抛出一个异常,只返回“src/main/res/layout/activity_main.xml”作为消息 下一
FileObject fo = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", "src/main/res/layout/activity_main.xml");
,但它总是抛出一个异常,只返回“src/main/res/layout/activity_main.xml”作为消息
下一个我想尝试的是
this.getClass().getResource("src/main/res/layout/activity_main.xml")
,但它始终返回null
我最后一次尝试使用的想法是:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
Iterable<? extends File> locations = fm.getLocation(StandardLocation.SOURCE_PATH);
for (File file : locations) {
System.out.print(file.getAbsolutePath());
}
JavaCompiler compiler=ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm=compiler.getStandardFileManager(null,null,null);
Iterable我使用以下方法从注释处理器获取布局文件夹:
private File findLayouts() throws Exception {
Filer filer = processingEnv.getFiler();
JavaFileObject dummySourceFile = filer.createSourceFile("dummy" + System.currentTimeMillis());
String dummySourceFilePath = dummySourceFile.toUri().toString();
if (dummySourceFilePath.startsWith("file:")) {
if (!dummySourceFilePath.startsWith("file://")) {
dummySourceFilePath = "file://" + dummySourceFilePath.substring("file:".length());
}
} else {
dummySourceFilePath = "file://" + dummySourceFilePath;
}
URI cleanURI = new URI(dummySourceFilePath);
File dummyFile = new File(cleanURI);
File projectRoot = dummyFile.getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getParentFile();
return new File(projectRoot.getAbsolutePath() + "/src/main/res/layout");
}
我使用以下方法从注释处理器获取布局文件夹:
private File findLayouts() throws Exception {
Filer filer = processingEnv.getFiler();
JavaFileObject dummySourceFile = filer.createSourceFile("dummy" + System.currentTimeMillis());
String dummySourceFilePath = dummySourceFile.toUri().toString();
if (dummySourceFilePath.startsWith("file:")) {
if (!dummySourceFilePath.startsWith("file://")) {
dummySourceFilePath = "file://" + dummySourceFilePath.substring("file:".length());
}
} else {
dummySourceFilePath = "file://" + dummySourceFilePath;
}
URI cleanURI = new URI(dummySourceFilePath);
File dummyFile = new File(cleanURI);
File projectRoot = dummyFile.getParentFile().getParentFile().getParentFile().getParentFile().getParentFile().getParentFile();
return new File(projectRoot.getAbsolutePath() + "/src/main/res/layout");
}
Processor实例在项目的根级别运行,因此您可以这样简单地执行:
String fileSeparator = System.getProperty("file.separator");
String absoluteFilePth = "app" + fileSeparator + "src" + fileSeparator + "main" + fileSeparator + "res" + fileSeparator + "layout";
File file = new File(absoluteFilePth);
在这方面,处理器实例在项目的根级别运行,因此您可以这样简单地执行:
String fileSeparator = System.getProperty("file.separator");
String absoluteFilePth = "app" + fileSeparator + "src" + fileSeparator + "main" + fileSeparator + "res" + fileSeparator + "layout";
File file = new File(absoluteFilePth);
关于,在适用于传统Java(除Android外)的工具中提及的术语“资源”将指的是Java/JAR资源,而不是Android资源。我发现解决方案在适用于传统Java(除Android外)的工具中提及的术语“资源”将指的是Java/JAR资源,不是Android资源。我找到了一个解决方案。我知道在评论中感谢你是违反规则的,但我无法控制自己!非常感谢。我知道在评论中感谢你是违反规则的,但我无法控制自己!非常感谢。