Issue
I want to access a resource from my andoid studio project in my annotation processor.
I first tried to use the getResource method from filer:
FileObject fo = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", "src/main/res/layout/activity_main.xml");
, but it always throwed a exception that just returned "src/main/res/layout/activity_main.xml" as a message.
Next think i have tried was
this.getClass().getResource("src/main/res/layout/activity_main.xml")
, but this always returned null.
The last think i have tried to use was this:
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());
}
, but it throwes a null pointer exception
Solution
I used the following to get the layout folder from my annotation processor:
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");
}
Answered By - H3x0n
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.