且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过Cordova中的build-extras.gradle将自定义值添加到build.gradle文件

更新时间:2022-04-18 22:14:59

我尝试了您的解决方案,发现声明为定义不同路径的vars错误.

I tried your solution and found that the vars declared to define the different paths are wrong.

我为此更改了挂钩代码:

I changed your hook code for this:

module.exports = function(ctx) {
    var fs = ctx.requireCordovaModule('fs'),
    path = ctx.requireCordovaModule('path'),
    rootdir = ctx.opts.projectRoot,
    android_dir = path.join(ctx.opts.projectRoot, 'platforms/android');
    gradle_file = rootdir + '/build-extras.gradle';
    dest_gradle_file = android_dir + '/build-extras.gradle';

    /*
    console.log("Before-Build Hook - rootdir", rootdir);
    console.log("Before-Build Hook - android_dir", android_dir);
    console.log("Before-Build Hook - gradle_file", gradle_file);
    console.log("Before-Build Hook - dest_gradle_file", dest_gradle_file);
    */

    if(!fs.existsSync(gradle_file)){
        console.log(gradle_file + ' not found. Skipping');
        return;
    }else if(!fs.existsSync(android_dir)){
        console.log(android_dir + ' not found. Skipping');
       return;
    }

    console.log('Copy ' + gradle_file + ' to ' + android_dir);
    fs.createReadStream(gradle_file).pipe(fs.createWriteStream(dest_gradle_file));
}

此外,在Hook文档中说它必须是可执行文件,因此需要用"module.exports = function(ctx){}"包装.

Also, in the Hook doc says it must be executable, so it needs to be wrapped by " module.exports = function(ctx) { }".