Cordova + Visual Studio + Android@7.0.0?

匆匆过客 提交于 2019-12-05 04:15:37

问题


Has anyone been able to get the lastest android update (7.0.0) to work with visual studio?

VS says the build is successful, but when you try to deploy, the step fails because it cannot find the created output.

I THINK it may be a matter of updating some of the paths, but figured I'd see if anyone has already figured it out.

Given that taco.visualstudio.com hasn't seen an update since June 2017, I'm wondering if the project isn't dead :-(

1>------ Build started: Project: myProject, Configuration: Debug Android ------
Cordova 7.1.0
------ Platform android already exists
------ Copying native files from D:\myproject\res\native\android to D:\myproject\platforms\android
------ Done copying native files to D:\myproject\platforms\android
2>------ Deploy started: Project: myProject, Configuration: Debug Android ------
2>Could not locate the start page. You may need to build your project.
2>Deployment failed.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Deploy: 0 succeeded, 1 failed, 0 skipped ==========

EDIT: Backing down to Android@6.4.0 has resolved my immediate problem. But it is not a long term solution.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/47729386/cordova-visual-studio-android7-0-0

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