0

I am generating some data files during build (using a custom Gradle plugin) and I would like to access in an Android library (i.e, included in the apk file that uses this library) and also want to make sure they do not show up as files added in the git. These files should not be part of the repository and should be removed after build is complete.

I can generate those files in src/main/assets and access using

context.assets.open(filename)

However, the files are left over as side-effects. Removing them from the gradle plugin (after the build is done) doesn't seem easy to me.

Is it possible to generate these files under build directory such that I can access them from the library using context (passed by app) but also will not show up as files added with git status.

Here is the plugin code:

import org.gradle.api.Task
import org.gradle.api.execution.TaskExecutionListener
import org.gradle.api.tasks.TaskState

import java.util.regex.Matcher
import java.util.regex.Pattern 

class BuildEventsListener implements TaskExecutionListener {

    @Override
    void beforeExecute(Task task) {
    }

    @Override
    void afterExecute(Task task, TaskState taskState) {
        boolean isApp = task.project.plugins.hasPlugin("com.android.application")
        if (!isApp) {
            return
        }
        String taskname = task.name
        Pattern pattern = (taskname.contains("assemble")) ? Pattern.compile("assemble(\\w*)(Release|Debug)\$")
                : Pattern.compile("generate(\\w*)(Release|Debug)\$")
        Matcher matcher = pattern.matcher(taskname)
        if (matcher.find() && task.hasProperty("project")) {
            getData(task.project)
        }
    }

    void getData(Project project) {
        String projectDir = project.projectDir.name
        String jsonPath = "${projectDir}/src/main/assets/data.json"
        BufferedWriter writer = new BufferedWriter(new FileWriter(jsonPath))
        writer.write("{")
        // write ...
        writer.write("}")
        writer.close()
    }
}
rysv
  • 2,416
  • 7
  • 30
  • 48

0 Answers0