我正在编写一个 Cordova 插件,我想classpath
在项目的build.gradle
,buildscript/dependencies
部分中添加一个。
Cordova 允许插件有一个与主文件合并的 gradle 文件,但似乎这只适用于模块的build.gradle
,而不是项目的。
我怎样才能添加这个classpath
?
我正在编写一个 Cordova 插件,我想classpath
在项目的build.gradle
,buildscript/dependencies
部分中添加一个。
Cordova 允许插件有一个与主文件合并的 gradle 文件,但似乎这只适用于模块的build.gradle
,而不是项目的。
我怎样才能添加这个classpath
?
我们必须完成同样的工作,因为 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" />