Fabian Fabian - 5 months ago 30
Android Question

greenDAO: ftl-templates not found

I'm using the greenDAO for Android and I want to edit the greenDAO generator, because it overwrites @SerializedName which I need for GSON every time I change something on my database.

So I was adding greenDAO from https://github.com/greenrobot/greenDAO to my Android Studio project by File -> New -> Import Module... and just selected the GreenDaoGenerator-module.

Next, I build my java module to generate the greenDAO database, which uses the GreenDaoGenerator-module as dependency and I get an exception that the template dao.ftl was not found.

Can someone please help me? I'm new to Android and do not check, why it doesn't find the file.

Exception in thread "main" java.io.FileNotFoundException: Template "dao.ftl" not found.
at freemarker.template.Configuration.getTemplate(Configuration.java:742)
at freemarker.template.Configuration.getTemplate(Configuration.java:665)
at de.greenrobot.daogenerator.DaoGenerator.<init>(DaoGenerator.java:65)
at com.example.GreenDaoCreater.main(GreenDaoCreater.java:39)


Edit:
Here is a screenshot from the project structure. I hope it helps.

enter image description here

Answer

The problem is change in path of the template files. While I am not sure how to set the path to something relative, here is a hack that works. Open DaoGenerator.java. In the constructor, replace

config.setClassForTemplateLoading(this.getClass(), "/");

with

config.setDirectoryForTemplateLoading(new File("<your absolute path to templates>"));

You will need to find where the actual templates are stored. For me they were in /DaoGenerator/src-template/ directory.