Visual Studio 2017 科尔多瓦;更改 Android 构建的目标文件夹

Visual Studio 2017 Cordova; change target folder for Android build

我已经弄清楚如何让 visual studio 使用 cordova 7.1.0 进行编译和测试,尽管理论上它应该只适用于 6.3.1 但我还有一个问题 Android.

Deploy 正在寻找 platforms\android\build\outputs\apk

中的 apk

Build 现在将 apk 放入 platforms\android\build\outputs\apk\debug

因此,如果我构建它,然后复制 apk,然后告诉 visual studio 进行调试,它可以正常工作,因为它可以在 platforms\android\build\outputs\apk

中优化 apk

所以每次我想测试时,我都必须删除 apk,构建它,然后复制它,然后再次构建它以允许 visual studio 部署它。

visual studio、项目或注册表中是否有我可以用来更改部署文件夹或构建文件夹以使其匹配的设置?

我尝试了一些似乎有效的方法。我清除了 Cordova 缓存,Tools/Options Appache Cordova 的工具 科尔多瓦工具

然后我重启了Visual Studio

现在它可以工作,并将调试直接发布到设备。

虽然应用程序成功启动 Visual Studio 停止响应,我记得在 2017 年的初始更新后也偶尔发生过一次,但似乎消失了。再次重启 Visual Studio 并将调试部署到 Android 设备后,它运行正常。

我听从了这里的建议 但对其进行了一些调整以使其适用于我的情况。

The cordova android platform 6.4+ puts the built apk here:

[project]\platforms\android\app\build\outputs\apk\debug\app-debug.apk

Visual Studio seems to be looking for it here:

[project]\platforms\android\build\outputs\apk\app-debug.apk

I added an "after_build" hook that copies the app-debug.apk and output.json files to the folder VS is looking in. I had to manually add the folder structures (for both the location of files being copied and location of hook file). I just added the following file, and the build process picks it up automatically.

下一步与建议略有不同。 "after_build" 挂钩将 app-debug.apk 和 app-release 文件复制到 VS 正在查找的文件夹:

我把copy_android_apk.js放在了[project]\scripts\

[项目]\scripts\copy_android_apk.js

我在[项目]\config.xml

中添加了一个"after_build"钩子元素
<platform name="android">
  <hook src="scripts/copy_android_apk.js" type="after_build" />
</platform>

copy_android_apk.js的内容:

#!/usr/bin/env node

module.exports = function (context) {
    console.log(" -- manual step -- have to copy apk to this folder because that is where VS is looking for it...");

    var fs = require('fs');
    var path = require('path');
    var rootdir = process.argv[2];

    var srcfile = path.join(process.cwd(), "platforms\android\app\build\outputs\apk\debug\app-debug.apk");
    var destfile = path.join(process.cwd(), "platforms\android\build\outputs\apk\app-debug.apk");

    var destdir = path.dirname(destfile);

    //Create the output directory if it doesn't exist
    if (!fs.existsSync(destdir)) {
        mkdirSyncRecursive(destdir);
    }

    if (fs.existsSync(srcfile) && fs.existsSync(destdir)) {
        fs.createReadStream(srcfile).pipe(
            fs.createWriteStream(destfile));
    }

    srcfile = path.join(process.cwd(), "platforms\android\app\build\outputs\apk\release\app-release.apk");
    destfile = path.join(process.cwd(), "platforms\android\build\outputs\apk\app-release.apk");

    destdir = path.dirname(destfile);
    if (fs.existsSync(srcfile) && fs.existsSync(destdir)) {
        fs.createReadStream(srcfile).pipe(
            fs.createWriteStream(destfile));
    }

    /**
     * Splits whole path into segments and checks each segment for existence and recreates directory tree from the bottom.
     * If since some segment tree doesn't exist it will be created in series.
     * Existing directories will be skipped.
     * @param {String} directory
     */
    function mkdirSyncRecursive(directory) {
        var path = directory.replace(/\$/, '').split('\');
        for (var i = 1; i <= path.length; i++) {
            var segment = path.slice(0, i).join('/');
            !fs.existsSync(segment) ? fs.mkdirSync(segment) : null;
        }
    }
}