Android : Resource files created by APT are not in the APK

on Wednesday, January 28, 2015


I am trying to create an annotation processor for a Android project. As a part of this I want to create a .properties file that the Android project should pick up, and this is where the pain begins.


I am using Gradle with the android and android-apt plugin, and I can see that the file is getting generated just fine under /build/generated/apt/source.


However it doesn't look like the properties file is getting copied correctly to the final APK as it is not present when you decompile the APK nor can the Classloader find it. Am I missing something or is it a bug in the android-apt plugin?


It does work if I place a properties file manually under /src/main/resource, which is why I suspect either android-apt or the Android plugin not behaving properly.


Relevant code from annotation processor:



FileObject propertiesFile = processingEnvironment.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, "com.package", "foo.properties");
Writer fileWriter = propertiesFile.openWriter();
Properties properties = new Properties();
properties.setProperty("bar", "baz");
properties.store(fileWriter, "");
fileWriter.close();


App loader (I need a list as multiple files can exist in different JARs):



List<String> list = new ArrayList<String>();
Enumeration<URL> systemResources = com.package.RandomClass.class.getClassLoader().getResources("com/package/foo.properties");
while (systemResources.hasMoreElements()) {
InputStream in = systemResources.nextElement().openStream();
Properties props = new Properties();
props.load(in);
in.close();
list.add(props.getProperty("bar"));
}
return list;

0 comments:

Post a Comment