8

I am writing a plugin for intellij and I would like to programmatically alter the build.gradle file of an Intellij Project.

I want to add

sourceSets {
    resources {
        srcDir 'src/resources'
    }
}

inside the android object in the build.gradle file (this is an android project)

I realize I have to do something along the lines

GradleBuildFile gradleBuildFile = GradleBuildFile.get(module);
GrStatementOwner closure = gradleBuildFile.getClosure("android/sourceSets/main/resources");
gradleBuildFile.setValue(closure, BuildFileKey.SRC_DIR, "blah");

Problem is, there is no BuildFileKey.SRC_DIR and I didn't find something remotely similar.

Also, will this add the sourceSets object if it is not there in the build.gradle file?

Any advice?

Thanks

Update: Alternatively, is there a way to mark a folder as a resource folder? I don't want to use the default res folder. Perhaps using the AndroidFacet object..

Karim
  • 247
  • 4
  • 11

1 Answers1

0

Good Question. I need to do the same thing, but have been doing it with the crude FileWriter. As long as it works :S

public void actionPerformed(AnActionEvent event) {

    Project project = event.getProject();
    String buildFilePath = project.getBasePath() + "\\app\\build.gradle";

    BufferedReader br_build = new BufferedReader(new FileReader(buildFilePath));

    String newBuildFile = "";
    String line;

    while ((line = br_build.readLine()) != null) {
        newBuildFile += line + "\n";
        if (line.contains("android {")){
            newBuildFile += "// Add your stuff here\n" +
                    "sourceSets {\n" +
                    "    resources {\n" +
                    "        srcDir 'src/resources'\n" +
                    "    }\n" +
                    "}\n";
        }
    }
    br_build.close();

    FileWriter fw_build = new FileWriter(buildFilePath);
    fw_build.write(newBuildFile);
    fw_build.close();
}
gregory
  • 188
  • 2
  • 17