簡體   English   中英

在cordova插件中將類路徑添加到build.gradle

[英]Add classpath to build.gradle in cordova plugin

我正在編寫一個 Cordova 插件,我想在buildscript/dependencies部分中向項目的build.gradle添加一個classpath

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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM