【问题标题】:Programmatically updating build.gradle in an intellij plugin以编程方式更新 intellij 插件中的 build.gradle
【发布时间】:2015-09-25 17:52:52
【问题描述】:

我正在为 intellij 编写一个插件,我想以编程方式更改 Intellij 项目的 build.gradle 文件。

我要添加

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

在 build.gradle 文件中的 android 对象内(这是一个 android 项目)

我意识到我必须做一些事情

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

问题是,没有BuildFileKey.SRC_DIR,我也没有找到类似的东西。

另外,如果 build.gradle 文件中没有 sourceSets 对象,这会添加它吗?

有什么建议吗?

谢谢

更新: 或者,有没有办法将文件夹标记为资源文件夹?我不想使用默认的 res 文件夹。也许使用 AndroidFacet 对象..

【问题讨论】:

  • 这个运气好吗?你能修改文件吗?我正在尝试做类似的事情。
  • 如果您对此有任何解决方案,请告诉我们。等待您的回复

标签: java android intellij-idea build.gradle intellij-plugin


【解决方案1】:

好问题。我需要做同样的事情,但一直在用粗略的 FileWriter 做。只要它有效: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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 2016-07-15
    • 2014-07-17
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多