I’m using gradle 2.3 to build android APKs and AARs (With ‘android’ and ‘android-library’ plugins, respectively).
I’m trying to generate a file during the build that will be picked up into the META-INF directory in the resulting APK.
I can successfully generate the file, but it doesn’t get packaged.
task createVersionInformation {
def resDir = new File(buildDir, 'generated/resources/META-INF/')
def destDir = new File(resDir, 'META-INF/')
doLast {
destDir.mkdirs()
def vfile = new File(destDir, 'BUILD_INFO')
vfile.text = "build info goes here"
}
}
tasks.preBuild.dependsOn createVersionInformation
The file shows up in the ./build/generated/ dir, but not in the apk.
Thank you, Aviv! What is the directory you write your “BUILD_INFO” file into in order for it to be included in META-INF in the APK? I tried the directory you included in your original post above but it didn’t seem to work.
Thank you, Aviv! I can get your code to work, but in my case I am copying files into META-INF instead of creating the file from scratch. I must have something off with the copy; the files make it into generated folder, but not into the APK.
I got this to work and wanted to share the final code; this is an example of how to copy a LICENSE file into the APK’s META-INF directory; many thanks to Aviv for his solution (which this is very close to):
task put_files_in_META_INF {
def resDir = new File(buildDir, 'generated/files_for_META_INF/')
def destDir = new File(resDir, 'META-INF/')
// THIS IS KEY: Add resDir as a resource directory so that it is
// automatically included in the APK
android {
sourceSets {
main.resources {
srcDir resDir
}
}
}
doLast {
destDir.mkdirs()
copy {
into destDir
from <SOURCE_LOCATION_OF_LICENSE_FILE>
}
}
}
// Specify when put_files_in_META_INF should run
project.afterEvaluate {
tasks.findAll { task ->
task.name.startsWith('merge') && task.name.endsWith('Resources')
}.each { t -> t.dependsOn put_files_in_META_INF }
}