Resource not found when creating a project from a custom Android Studio template
Planned
Hi,
I am creating a plugin for Android Studio which adds a custom project template. The project created from this template must contain some resources. The RecipeExecutor.copy method allows to copy a source file into a destination file. However, when running Android Studio with my plugin, the following error appears : "resource relative to com.android.tools.idea.wizard.template.TemplateBuilder not found." The resource's path is correct but the error is still present. How can I add this resource ?
Please sign in to leave a comment.
Hi Denis Bedin,
Error message `com.android.tools.idea.wizard.template.TemplateBuilder not found.` suggests that this is not about `copy` method. Do you have `thumb` in your template? E.g.
If not - could you please post full stacktrace for IllegalArgumentException("resource %s relative to %s not found.")?
Hi Andrei Kuznetsov and thanks for your answer,
I do have `thumb` in my template. Here is the stacktrace :
However, in addition to this error message, I also have an error when using `copy` method : `resource relative to com.android.tools.idea.wizard.template.ModuleTemplateData not found.` As the two errors are quite similar I thought it was due to the resources not being placed in the right folder, but it does not seem to be the case.
For `thumb` workaround is to use `thumb` property instead of `thumb()` function:
Second one (ModuleTemplateData) is about copy indeed. To me this looks like a bug. Please report it to Android Studio issues tracker: https://issuetracker.google.com/issues/new?component=192708&template=840533
The problem is (in my opinion) that wrong context class is passed to `com.android.tools.idea.wizard.template.TemplateHelpersKt#findResource` when this method is invoked from an extension located outside the Android plugin. But I didn't find an easy way to force IDE using different context class.
As a workaround for `copy` method you can use plain java.io API, IntelliJ API (e.g., `com.intellij.openapi.fileEditor.FileDocumentManager`), or try `com.android.tools.idea.wizard.template.RecipeExecutor#save`. BTW, `copy` uses IntelliJ API underneath. See https://cs.android.com/android-studio/platform/tools/adt/idea/+/mirror-goog-studio-main:android-templates/src/com/android/tools/idea/templates/recipe/DefaultRecipeExecutor.kt;l=291
Thanks to your two workarounds, I don't have these two issues anymore, and now everything works as expected, thank you.