【问题标题】:Add classpath to build.gradle in cordova plugin在 cordova 插件中将类路径添加到 build.gradle
【发布时间】:2018-12-07 06:57:55
【问题描述】:

我正在编写一个 Cordova 插件,我想在项目的 build.gradlebuildscript/dependencies 部分中添加一个 classpath

Cordova 允许插件有一个与主文件合并的 gradle 文件,但似乎这只适用于模块的 build.gradle,而不是项目的。

如何添加这个classpath

【问题讨论】:

    标签: cordova gradle build.gradle cordova-plugins


    【解决方案1】:

    我们必须完成同样的工作,因为 Cordova 似乎没有内置方法来修改主 build.gradle 文件,所以我们使用预构建挂钩来完成它:

    const fs = require("fs");
    const path = require("path");
    
    function addProjectLevelDependency(platformRoot) {
        const artifactVersion = "group:artifactId:1.0.0";
        const dependency = 'classpath "' + artifactVersion + '"';
    
        const projectBuildFile = path.join(platformRoot, "build.gradle");
    
        let fileContents = fs.readFileSync(projectBuildFile, "utf8");
    
        const myRegexp = /\bclasspath\b.*/g;
        let match = myRegexp.exec(fileContents);
        if (match != null) {
            let insertLocation = match.index + match[0].length;
    
            fileContents =
                fileContents.substr(0, insertLocation) +
                "; " +
                dependency +
                fileContents.substr(insertLocation);
    
            fs.writeFileSync(projectBuildFile, fileContents, "utf8");
    
            console.log("updated " + projectBuildFile + " to include dependency " + dependency);
        } else {
            console.error("unable to insert dependency " + dependency);
        }
    }
    
    module.exports = context => {
        "use strict";
        const platformRoot = path.join(context.opts.projectRoot, "platforms/android");
    
        return new Promise((resolve, reject) => {
            addProjectLevelDependency(platformRoot);
            resolve();
        });
    };
    

    钩子在config.xml中被引用:

    <hook src="build/android/configureProjectLevelDependency.js" type="before_build" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-28
      • 2016-06-26
      • 2021-07-21
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      • 2023-03-27
      相关资源
      最近更新 更多