gpt4 book ai didi

cordova - 在 cordova 插件中将类路径添加到 build.gradle

转载 作者:行者123 更新时间:2023-12-03 02:58:27 31 4
gpt4 key购买 nike

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

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

如何添加 classpath ?

最佳答案

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

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();
});
};

该钩子(Hook)在 config.xml 中被引用:

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

关于cordova - 在 cordova 插件中将类路径添加到 build.gradle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51078719/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com