Cordova + Visual Studio + Android@7.0.0?

限于喜欢 提交于 2019-12-03 21:08:33

A bit hacky, but here's what I got to work.

Got a clue from: https://developercommunity.visualstudio.com/content/problem/199609/cordova-ios-640-deployment-error-due-to-change-bui.html (although suggested path did not work for me)

I've only been looking at Android so far, so I will talk in terms of that. 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.

[project]\hooks\after_build\copy_android_apk.js

contents of copy_android_apk.js:

#!/usr/bin/env node

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(rootdir, "platforms\\android\\app\\build\\outputs\\apk\\debug\\app-debug.apk");
var destfile = path.join(rootdir, "platforms\\android\\build\\outputs\\apk\\app-debug.apk");

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

srcfile = path.join(rootdir, "platforms\\android\\app\\build\\outputs\\apk\\debug\\output.json");
destfile = path.join(rootdir, "platforms\\android\\build\\outputs\\apk\\output.json");

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

For this answer https://stackoverflow.com/a/49270052/9874134, I tweaked it a bit to make it work for my case. The "after_build" hook copies the app-debug.apk and app-release files to the folder VS is looking in.

I placed copy_android_apk.js under [project]\scripts\

[project]\scripts\copy_android_apk.js

I added an "after_build" hook element in [project]\config.xml

<platform name="android">
  <hook src="scripts/copy_android_apk.js" type="after_build" />
</platform>

contents of 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;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!