Add files to META-INF folder within Android .apk automatically
Asked Answered
H

1

7

I've managed to port Jersey to Android (jersey-android)

Jersey requires certain configuration files to be present within the folder META-INF/services . So far I'm manually copying the services folder to the generated apk's META-INF folder.

Is there a way to instruct Eclipse, ADT or ANT to copy the services folder within the apk in the META-INF folder?

Hothead answered 7/7, 2012 at 14:8 Comment(1)
related (according to a non-answer posted as an answer): discuss.gradle.org/t/add-generated-file-to-meta-inf/11831/7Bagging
C
1

I found a solution by gradle, hope it helps whoever meets the same situation.

Adding the following task and task dependency in your module's build.gradle, and replace your actual file path in the example, then rebuild your apk. It should solve the problem.

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 }
}

Refs: https://discuss.gradle.org/t/add-generated-file-to-meta-inf/11831/7

Certainty answered 8/6, 2023 at 0:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.