Reputation: 71
I have figured out how to get visual studio to compile and test with cordova 7.1.0 even though in theory it is supposed to only work with 6.3.1 But I have one more issue with Android.
Deploy is looking for the apk in platforms\android\build\outputs\apk
Build now puts the apk in platforms\android\build\outputs\apk\debug
So if I build it, then copy the apk then tell visual studio to debug, it works since it can fine the apk in platforms\android\build\outputs\apk
So every time I want to test, I have to delete the apk, built it, then copy it, then build it again to allow visual studio to deploy it.
Is there a setting in visual studio, the project, or the registry that I can use to change either the deploy folder or the build folder so they match?
Upvotes: 4
Views: 1074
Reputation: 31
I followed the advice from here https://stackoverflow.com/a/49270052/9874134 but tweaked it a bit to make it work for my case.
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.
The next step is a bit different to the advice. 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;
}
}
}
Upvotes: 1
Reputation: 71
I tried something that appears to have worked. I cleared the Cordova Cache, Tools/Options Tools for Appache Cordova Cordova Tools
Then I restarted Visual Studio
Now it works and will publish the debug directly to the device.
Although the app launched successfully Visual Studio stopped responding, which I recall also happened once in a while after the initial update to 2017, but seemed to go away. After restarting Visual Studio again, and Deploying the debug to Android Device, it operated properly.
Upvotes: 0