Skip to content

Commit

Permalink
fix(android): clean/rebuild should release gradle file locks
Browse files Browse the repository at this point in the history
- Gradle daemon file locks prevented some build files from being deleted on Windows.
- Modified app rebuild to run gradle "clean" task or "stop" gradle deamon as a fallback.
  * Titanium 7.x.x and 8.x.x has gradlew files for proguard, but cannot run "clean" due to missing project files.
- Modified module clean to run gradle "clean" task.
- Modified hyperloop to v5.0.4 to run gradle "clean" task.
  • Loading branch information
jquick-axway authored and sgtcoolguy committed Jun 17, 2020
1 parent 83e9300 commit be923f1
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
27 changes: 26 additions & 1 deletion android/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -1959,9 +1959,34 @@ AndroidBuilder.prototype.checkIfShouldForceRebuild = function checkIfShouldForce
};

AndroidBuilder.prototype.checkIfNeedToRecompile = async function checkIfNeedToRecompile() {
// Delete all files under the "./build/android" if we need to do a full rebuild.
// Determine if we should do a "clean" build.
this.forceRebuild = this.checkIfShouldForceRebuild();
if (this.forceRebuild) {
// First, make the gradle daemon release its file locks so that they can be deleted on Windows.
const gradlew = new GradleWrapper(this.buildDir);
if (await gradlew.hasWrapperFiles()) {
// Attempt to run the gradle "clean" task if possible. (This is the fastest solution.)
let wasCleaned = false;
try {
if (await gradlew.hasLocalPropertiesFile() && await gradlew.hasSettingsGradleFile()) {
await gradlew.clean('app');
wasCleaned = true;
}
} catch (err) {
this.logger.debug('Failed to run gradle "clean" task. Reason:\n' + err);
}

// If unable to clean project, then try to stop all gradle daemons.
if (!wasCleaned) {
try {
await gradlew.run('--stop --console plain');
} catch (err) {
this.logger.debug('Failed to stop gradle daemon. Reason:\n' + err);
}
}
}

// Delete all files under the "./build/android" directory.
await fs.emptyDir(this.buildDir);
this.unmarkBuildDirFiles(this.buildDir);
}
Expand Down
20 changes: 17 additions & 3 deletions android/cli/commands/_cleanModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,28 @@
*/

'use strict';
const path = require('path');
const fs = require('fs-extra');

const appc = require('node-appc');
const fs = require('fs-extra');
const GradleWrapper = require('../lib/gradle-wrapper');
const path = require('path');
const __ = appc.i18n(__dirname).__;

exports.run = function run(logger, config, cli, finished) {
exports.run = async function run(logger, config, cli, finished) {
const projectDir = cli.argv['project-dir'];

// Run the gradle "clean" task if possible.
// This makes the gradle daemon release its file locks so that they can be deleted on Windows.
try {
const gradlew = new GradleWrapper(path.join(projectDir, 'build'));
gradlew.logger = logger;
if (await gradlew.hasWrapperFiles()) {
await gradlew.clean();
}
} catch (err) {
this.logger.error(`Failed to run gradle "clean" task. Reason:\n${err}`);
}

const toDelete = [ 'build', 'dist', 'java-sources.txt' ];
toDelete.forEach(f => {
const target = path.join(projectDir, f);
Expand Down
47 changes: 45 additions & 2 deletions android/cli/lib/gradle-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ class GradleWrapper {
return this._gradlewDirPath;
}

/**
* Gets a path to the "gradlew" script file that will be executed to run gradle tasks.
* @type {String}
*/
get gradlewFilePath() {
return path.join(this._gradlewDirPath, isWindows ? 'gradlew.bat' : 'gradlew');
}

/**
* Gets/Sets the "appc-logger" object that gradle will output to. Can be null/undefined.
* @type {Object}
Expand Down Expand Up @@ -155,8 +163,7 @@ class GradleWrapper {
*/
async run(argsString) {
// Set up the "gradlew" command line string.
const gradlewFilePath = path.join(this._gradlewDirPath, isWindows ? 'gradlew.bat' : 'gradlew');
let commandLineString = `"${gradlewFilePath}"`;
let commandLineString = `"${this.gradlewFilePath}"`;
if (argsString) {
commandLineString += ' ' + argsString;
}
Expand Down Expand Up @@ -209,6 +216,42 @@ class GradleWrapper {
});
}

/**
* Determines if a "locale.properties" file exists at the gradle project's root location.
* @returns {Promise<Boolean>} Returns true if the file exists. Returns false if not.
*/
async hasLocalPropertiesFile() {
return fs.exists(path.join(this._gradlewDirPath, 'local.properties'));
}

/**
* Determines if a "settings.gradle" file exists at the gradle project's root location.
* @returns {Promise<Boolean>} Returns true if the file exists. Returns false if not.
*/
async hasSettingsGradleFile() {
return fs.exists(path.join(this._gradlewDirPath, 'settings.gradle'));
}

/**
* Determines if the gradlew wrapper files exist in the gradle project's root location.
* @returns {Promise<Boolean>} Returns true if the wrapper files exist. Returns false if not.
*/
async hasWrapperFiles() {
if (!await fs.exists(this.gradlewFilePath)) {
return false;
}

const wrapperLibraryDirPath = path.join(this._gradlewDirPath, 'gradle', 'wrapper');
if (!await fs.exists(path.join(wrapperLibraryDirPath, 'gradle-wrapper.jar'))) {
return false;
}
if (!await fs.exists(path.join(wrapperLibraryDirPath, 'gradle-wrapper.properties'))) {
return false;
}

return true;
}

/**
* Copies the "gradlew" file tree from the given "templateDirPath" to this object's assigned "directoryPath".
* This must be done before calling this object's clean(), assembleRelease(), assembleDebug(), or run() methods
Expand Down
4 changes: 2 additions & 2 deletions support/module/packaged/modules.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
},
"hyperloop": {
"hyperloop": {
"url": "https://github.com/appcelerator-modules/hyperloop-builds/releases/download/v5.0.3/hyperloop-5.0.3.zip",
"integrity": "sha512-4vHEggtcmag7lF25yubM2Gt2jstrexx41ADFYWKGAyPely3A2+HVnifiY/em8r4eMLy0W0tKATop7bP9g/oLnA=="
"url": "https://github.com/appcelerator-modules/hyperloop-builds/releases/download/v5.0.4/hyperloop-5.0.4.zip",
"integrity": "sha512-XcQheY69GCOBply+n9ZmosrSKqXEyTiLYMmcvRxtIktubKOHHDgmDLgc8F8vPnyS8LUnOiNKmHPqWPu+U+dLGw=="
}
}
}

0 comments on commit be923f1

Please sign in to comment.