(Gradle and OrmLite config) How to add resource file after Java has been compiled but before .apk is generated?

十年热恋 提交于 2019-12-05 13:27:46

The problem is that the resources are compiled by aapt when R.java is produced (and so before javac). Those compiled resources are then used to build the apk. AFAIK those compiled resources is just a kind of zip archive.

So, to achieve your goal you need to modify those compiled resources after javac compilation.

You can try to define a new task (let's call it addOrmToRes) invoking aapt with required arguments to modify the compiled resources.

aapt is located under <ANDROID_SDK_HOME>/build-tools/<version>/

The aapt Usage indicates that :

aapt a[dd] [-v] file.{zip,jar,apk} file1 [file2 ...]
   Add specified files to Zip-compatible archive.

So executing something like this:

aapt add /build/apk/myapp.apk ormlite_config.txt

or this

aapt add <the_path_to_compiled_resources> ormlite_config.txt

in your new custom task addOrmToRes should do the trick. So to answer to your edit 3 : aapt add is probably the solution.

To integrate it in the build, something like this should work:

runOrmGenTask.dependsOn(variant.javaCompile) 
addOrmToRes.dependsOn(runOrmGenTask)
addOrmToRes.dependsOn(<the task creating the apk>)
<the task signing the apk>.dependsOn(addOrmToRes)

Note that I didn't identify every tasks here. But I guess you get the idea.

Try to use dependsOn mechanism.

runOrmGenTask.dependsOn(variant.javaCompile) 

And, then for example:

packageApplication.dependsOn(runOrmGenTask)

More about task manipulation you can get from here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!