fix!: set & use ANDROID_HOME as default (#1444)

* fix: remove ANDROID_HOME's DEPRECATED text
* fix: check_gradle to check ANDROID_HOME first ANDROID_SDK_ROOT last
* fix: set ANDROID_HOME
* chore: deprecate flag on ANDROID_SDK_ROOT
This commit is contained in:
エリス 2022-06-27 22:07:32 +09:00 committed by GitHub
parent 4916e1db51
commit bf9e4d8aab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 44 deletions

View File

@ -146,7 +146,7 @@ module.exports.get_gradle_wrapper = function () {
// Returns a promise. Called only by build and clean commands. // Returns a promise. Called only by build and clean commands.
module.exports.check_gradle = function () { module.exports.check_gradle = function () {
const sdkDir = process.env.ANDROID_SDK_ROOT || process.env.ANDROID_HOME; const sdkDir = process.env.ANDROID_HOME || process.env.ANDROID_SDK_ROOT;
if (!sdkDir) { if (!sdkDir) {
return Promise.reject(new CordovaError('Could not find gradle wrapper within Android SDK. Could not find Android SDK directory.\n' + return Promise.reject(new CordovaError('Could not find gradle wrapper within Android SDK. Could not find Android SDK directory.\n' +
'Might need to install Android SDK or set up \'ANDROID_SDK_ROOT\' env variable.')); 'Might need to install Android SDK or set up \'ANDROID_SDK_ROOT\' env variable.'));
@ -179,15 +179,15 @@ module.exports.check_android = function () {
function maybeSetAndroidHome (value) { function maybeSetAndroidHome (value) {
if (!hasAndroidHome && fs.existsSync(value)) { if (!hasAndroidHome && fs.existsSync(value)) {
hasAndroidHome = true; hasAndroidHome = true;
process.env.ANDROID_SDK_ROOT = value; process.env.ANDROID_HOME = value;
} }
} }
const adbInPath = forgivingWhichSync('adb'); const adbInPath = forgivingWhichSync('adb');
const avdmanagerInPath = forgivingWhichSync('avdmanager'); const avdmanagerInPath = forgivingWhichSync('avdmanager');
if (process.env.ANDROID_SDK_ROOT) { if (process.env.ANDROID_HOME) {
maybeSetAndroidHome(path.resolve(process.env.ANDROID_SDK_ROOT)); maybeSetAndroidHome(path.resolve(process.env.ANDROID_HOME));
} }
// First ensure ANDROID_HOME is set // First ensure ANDROID_HOME is set
@ -240,7 +240,7 @@ module.exports.check_android = function () {
} }
if (!hasAndroidHome) { if (!hasAndroidHome) {
// If we dont have ANDROID_SDK_ROOT, but we do have some tools on the PATH, try to infer from the tooling PATH. // If we dont have ANDROID_HOME, but we do have some tools on the PATH, try to infer from the tooling PATH.
let parentDir, grandParentDir; let parentDir, grandParentDir;
if (adbInPath) { if (adbInPath) {
parentDir = path.dirname(adbInPath); parentDir = path.dirname(adbInPath);
@ -248,7 +248,7 @@ module.exports.check_android = function () {
if (path.basename(parentDir) === 'platform-tools') { if (path.basename(parentDir) === 'platform-tools') {
maybeSetAndroidHome(grandParentDir); maybeSetAndroidHome(grandParentDir);
} else { } else {
throw new CordovaError('Failed to find \'ANDROID_SDK_ROOT\' environment variable. Try setting it manually.\n' + throw new CordovaError('Failed to find \'ANDROID_HOME\' environment variable. Try setting it manually.\n' +
'Detected \'adb\' command at ' + parentDir + ' but no \'platform-tools\' directory found near.\n' + 'Detected \'adb\' command at ' + parentDir + ' but no \'platform-tools\' directory found near.\n' +
'Try reinstall Android SDK or update your PATH to include valid path to SDK' + path.sep + 'platform-tools directory.'); 'Try reinstall Android SDK or update your PATH to include valid path to SDK' + path.sep + 'platform-tools directory.');
} }
@ -259,26 +259,26 @@ module.exports.check_android = function () {
if (path.basename(parentDir) === 'bin' && path.basename(grandParentDir) === 'tools') { if (path.basename(parentDir) === 'bin' && path.basename(grandParentDir) === 'tools') {
maybeSetAndroidHome(path.dirname(grandParentDir)); maybeSetAndroidHome(path.dirname(grandParentDir));
} else { } else {
throw new CordovaError('Failed to find \'ANDROID_SDK_ROOT\' environment variable. Try setting it manually.\n' + throw new CordovaError('Failed to find \'ANDROID_HOME\' environment variable. Try setting it manually.\n' +
'Detected \'avdmanager\' command at ' + parentDir + ' but no \'tools' + path.sep + 'bin\' directory found near.\n' + 'Detected \'avdmanager\' command at ' + parentDir + ' but no \'tools' + path.sep + 'bin\' directory found near.\n' +
'Try reinstall Android SDK or update your PATH to include valid path to SDK' + path.sep + 'tools' + path.sep + 'bin directory.'); 'Try reinstall Android SDK or update your PATH to include valid path to SDK' + path.sep + 'tools' + path.sep + 'bin directory.');
} }
} }
} }
if (!process.env.ANDROID_SDK_ROOT) { if (!process.env.ANDROID_HOME) {
throw new CordovaError('Failed to find \'ANDROID_SDK_ROOT\' environment variable. Try setting it manually.\n' + throw new CordovaError('Failed to find \'ANDROID_HOME\' environment variable. Try setting it manually.\n' +
'Failed to find \'android\' command in your \'PATH\'. Try update your \'PATH\' to include path to valid SDK directory.'); 'Failed to find \'android\' command in your \'PATH\'. Try update your \'PATH\' to include path to valid SDK directory.');
} }
if (!fs.existsSync(process.env.ANDROID_SDK_ROOT)) { if (!fs.existsSync(process.env.ANDROID_HOME)) {
throw new CordovaError('\'ANDROID_SDK_ROOT\' environment variable is set to non-existent path: ' + process.env.ANDROID_SDK_ROOT + throw new CordovaError('\'ANDROID_HOME\' environment variable is set to non-existent path: ' + process.env.ANDROID_SDK_ROOT +
'\nTry update it manually to point to valid SDK directory.'); '\nTry update it manually to point to valid SDK directory.');
} }
// Next let's make sure relevant parts of the SDK tooling is in our PATH // Next let's make sure relevant parts of the SDK tooling is in our PATH
if (hasAndroidHome && !adbInPath) { if (hasAndroidHome && !adbInPath) {
process.env.PATH += path.delimiter + path.join(process.env.ANDROID_SDK_ROOT, 'platform-tools'); process.env.PATH += path.delimiter + path.join(process.env.ANDROID_HOME, 'platform-tools');
} }
if (hasAndroidHome && !avdmanagerInPath) { if (hasAndroidHome && !avdmanagerInPath) {
process.env.PATH += path.delimiter + path.join(process.env.ANDROID_SDK_ROOT, 'tools', 'bin'); process.env.PATH += path.delimiter + path.join(process.env.ANDROID_HOME, 'tools', 'bin');
} }
return hasAndroidHome; return hasAndroidHome;
}); });
@ -302,8 +302,8 @@ module.exports.check_android_target = function (projectRoot) {
// Returns a promise. // Returns a promise.
module.exports.run = function () { module.exports.run = function () {
console.log('Checking Java JDK and Android SDK versions'); console.log('Checking Java JDK and Android SDK versions');
console.log('ANDROID_SDK_ROOT=' + process.env.ANDROID_SDK_ROOT + ' (recommended setting)'); console.log('ANDROID_HOME=' + process.env.ANDROID_HOME + ' (recommended setting)');
console.log('ANDROID_HOME=' + process.env.ANDROID_HOME + ' (DEPRECATED)'); console.log('ANDROID_SDK_ROOT=' + process.env.ANDROID_SDK_ROOT + ' (DEPRECATED)');
return Promise.all([this.check_java(), this.check_android()]).then(function (values) { return Promise.all([this.check_java(), this.check_android()]).then(function (values) {
console.log('Using Android SDK: ' + process.env.ANDROID_SDK_ROOT); console.log('Using Android SDK: ' + process.env.ANDROID_SDK_ROOT);

View File

@ -68,20 +68,20 @@ describe('check_reqs', function () {
spyOn(which, 'sync').and.returnValue(null); spyOn(which, 'sync').and.returnValue(null);
spyOn(fs, 'existsSync').and.returnValue(true); spyOn(fs, 'existsSync').and.returnValue(true);
}); });
it('it should set ANDROID_SDK_ROOT on Windows', () => { it('it should set ANDROID_HOME on Windows', () => {
spyOn(check_reqs, 'isWindows').and.returnValue(true); spyOn(check_reqs, 'isWindows').and.returnValue(true);
process.env.LOCALAPPDATA = 'windows-local-app-data'; process.env.LOCALAPPDATA = 'windows-local-app-data';
process.env.ProgramFiles = 'windows-program-files'; process.env.ProgramFiles = 'windows-program-files';
return check_reqs.check_android().then(function () { return check_reqs.check_android().then(function () {
expect(process.env.ANDROID_SDK_ROOT).toContain('windows-local-app-data'); expect(process.env.ANDROID_HOME).toContain('windows-local-app-data');
}); });
}); });
it('it should set ANDROID_SDK_ROOT on Darwin', () => { it('it should set ANDROID_HOME on Darwin', () => {
spyOn(check_reqs, 'isWindows').and.returnValue(false); spyOn(check_reqs, 'isWindows').and.returnValue(false);
spyOn(check_reqs, 'isDarwin').and.returnValue(true); spyOn(check_reqs, 'isDarwin').and.returnValue(true);
process.env.HOME = 'home is where the heart is'; process.env.HOME = 'home is where the heart is';
return check_reqs.check_android().then(function () { return check_reqs.check_android().then(function () {
expect(process.env.ANDROID_SDK_ROOT).toContain('home is where the heart is'); expect(process.env.ANDROID_HOME).toContain('home is where the heart is');
}); });
}); });
}); });
@ -91,17 +91,17 @@ describe('check_reqs', function () {
return path; return path;
}); });
}); });
it('should set ANDROID_SDK_ROOT based on `adb` command if command exists in a SDK-like directory structure', () => { it('should set ANDROID_HOME based on `adb` command if command exists in a SDK-like directory structure', () => {
spyOn(fs, 'existsSync').and.returnValue(true); spyOn(fs, 'existsSync').and.returnValue(true);
spyOn(which, 'sync').and.callFake(function (cmd) { spyOn(which, 'sync').and.callFake(function (cmd) {
if (cmd === 'adb') { if (cmd === 'adb') {
return '/android/sdk/platform-tools/adb'; return path.normalize('/android/sdk/platform-tools/adb');
} else { } else {
return null; return null;
} }
}); });
return check_reqs.check_android().then(function () { return check_reqs.check_android().then(function () {
expect(process.env.ANDROID_SDK_ROOT).toEqual('/android/sdk'); expect(process.env.ANDROID_HOME).toEqual(path.normalize('/android/sdk'));
}); });
}); });
it('should error out if `adb` command exists in a non-SDK-like directory structure', () => { it('should error out if `adb` command exists in a non-SDK-like directory structure', () => {
@ -119,17 +119,17 @@ describe('check_reqs', function () {
expect(err.message).toContain('update your PATH to include valid path'); expect(err.message).toContain('update your PATH to include valid path');
}); });
}); });
it('should set ANDROID_SDK_ROOT based on `avdmanager` command if command exists in a SDK-like directory structure', () => { it('should set ANDROID_HOME based on `avdmanager` command if command exists in a SDK-like directory structure', () => {
spyOn(fs, 'existsSync').and.returnValue(true); spyOn(fs, 'existsSync').and.returnValue(true);
spyOn(which, 'sync').and.callFake(function (cmd) { spyOn(which, 'sync').and.callFake(function (cmd) {
if (cmd === 'avdmanager') { if (cmd === 'avdmanager') {
return '/android/sdk/tools/bin/avdmanager'; return path.normalize('/android/sdk/tools/bin/avdmanager');
} else { } else {
return null; return null;
} }
}); });
return check_reqs.check_android().then(function () { return check_reqs.check_android().then(function () {
expect(process.env.ANDROID_SDK_ROOT).toEqual('/android/sdk'); expect(process.env.ANDROID_HOME).toEqual(path.normalize('/android/sdk'));
}); });
}); });
it('should error out if `avdmanager` command exists in a non-SDK-like directory structure', () => { it('should error out if `avdmanager` command exists in a non-SDK-like directory structure', () => {
@ -169,7 +169,7 @@ describe('check_reqs', function () {
it('should use ANDROID_SDK_ROOT if defined', () => { it('should use ANDROID_SDK_ROOT if defined', () => {
spyOn(fs, 'existsSync').and.returnValue(true); spyOn(fs, 'existsSync').and.returnValue(true);
process.env.ANDROID_SDK_ROOT = '/android/sdk'; process.env.ANDROID_SDK_ROOT = path.normalize('/android/sdk');
return check_reqs.check_android().then(() => { return check_reqs.check_android().then(() => {
expect(process.env.ANDROID_SDK_ROOT).toContain(expectedAndroidSdkPath); expect(process.env.ANDROID_SDK_ROOT).toContain(expectedAndroidSdkPath);
}); });
@ -177,25 +177,25 @@ describe('check_reqs', function () {
it('should use ANDROID_HOME if defined and ANDROID_SDK_ROOT is not defined', () => { it('should use ANDROID_HOME if defined and ANDROID_SDK_ROOT is not defined', () => {
spyOn(fs, 'existsSync').and.returnValue(true); spyOn(fs, 'existsSync').and.returnValue(true);
process.env.ANDROID_HOME = '/android/sdk'; process.env.ANDROID_HOME = path.normalize('/android/sdk');
return check_reqs.check_android().then(() => { return check_reqs.check_android().then(() => {
expect(process.env.ANDROID_SDK_ROOT).toContain(expectedAndroidSdkPath); expect(process.env.ANDROID_HOME).toContain(expectedAndroidSdkPath);
}); });
}); });
it('should use ANDROID_SDK_ROOT if defined and ANDROID_HOME is defined', () => { it('should use ANDROID_HOME if defined and ANDROID_SDK_ROOT is defined', () => {
spyOn(fs, 'existsSync').and.returnValue(true); spyOn(fs, 'existsSync').and.returnValue(true);
process.env.ANDROID_SDK_ROOT = '/android/sdk/root'; process.env.ANDROID_SDK_ROOT = path.normalize('/android/sdk/root');
process.env.ANDROID_HOME = '/android/sdk'; process.env.ANDROID_HOME = path.normalize('/android/sdk');
return check_reqs.check_android().then(() => { return check_reqs.check_android().then(() => {
expect(process.env.ANDROID_SDK_ROOT).toContain(expectedAndroidRootSdkPath); expect(process.env.ANDROID_SDK_ROOT).toContain(expectedAndroidRootSdkPath);
}); });
}); });
it('should throw if ANDROID_SDK_ROOT points to an invalid path', () => { it('should throw if ANDROID_HOME points to an invalid path', () => {
process.env.ANDROID_SDK_ROOT = '/android/sdk'; process.env.ANDROID_HOME = path.normalize('/android/sdk');
return check_reqs.check_android().catch((error) => { return check_reqs.check_android().catch((error) => {
expect(error.toString()).toContain('\'ANDROID_SDK_ROOT\' environment variable is set to non-existent path:'); expect(error.toString()).toContain('\'ANDROID_HOME\' environment variable is set to non-existent path:');
}); });
}); });
}); });
@ -203,7 +203,7 @@ describe('check_reqs', function () {
describe('set PATH for various Android binaries if not available', function () { describe('set PATH for various Android binaries if not available', function () {
beforeEach(function () { beforeEach(function () {
spyOn(which, 'sync').and.returnValue(null); spyOn(which, 'sync').and.returnValue(null);
process.env.ANDROID_SDK_ROOT = 'let the children play'; process.env.ANDROID_HOME = 'let the children play';
spyOn(fs, 'existsSync').and.returnValue(true); spyOn(fs, 'existsSync').and.returnValue(true);
}); });
it('should add tools/bin,tools,platform-tools to PATH if `avdmanager`,`android`,`adb` is not found', () => { it('should add tools/bin,tools,platform-tools to PATH if `avdmanager`,`android`,`adb` is not found', () => {
@ -222,24 +222,24 @@ describe('check_reqs', function () {
delete process.env.ANDROID_SDK_ROOT; delete process.env.ANDROID_SDK_ROOT;
delete process.env.ANDROID_HOME; delete process.env.ANDROID_HOME;
spyOn(check_reqs, 'get_gradle_wrapper').and.callFake(() => { spyOn(check_reqs, 'get_gradle_wrapper').and.callFake(() => {
return (process.env.ANDROID_SDK_ROOT || process.env.ANDROID_HOME) + '/bin/gradle'; return path.normalize((process.env.ANDROID_HOME || process.env.ANDROID_SDK_ROOT) + '/bin/gradle');
}); });
}); });
it('with ANDROID_SDK_ROOT / without ANDROID_HOME', async () => { it('with ANDROID_SDK_ROOT / without ANDROID_HOME', async () => {
process.env.ANDROID_SDK_ROOT = '/android/sdk/root'; process.env.ANDROID_SDK_ROOT = path.normalize('/android/sdk/root');
await expectAsync(check_reqs.check_gradle()).toBeResolvedTo('/android/sdk/root/bin/gradle'); await expectAsync(check_reqs.check_gradle()).toBeResolvedTo(path.normalize('/android/sdk/root/bin/gradle'));
}); });
it('with ANDROID_SDK_ROOT / with ANDROID_HOME', async () => { it('with ANDROID_SDK_ROOT / with ANDROID_HOME', async () => {
process.env.ANDROID_SDK_ROOT = '/android/sdk/root'; process.env.ANDROID_SDK_ROOT = path.normalize('/android/sdk/root');
process.env.ANDROID_HOME = '/android/sdk/home'; process.env.ANDROID_HOME = path.normalize('/android/sdk/home');
await expectAsync(check_reqs.check_gradle()).toBeResolvedTo('/android/sdk/root/bin/gradle'); await expectAsync(check_reqs.check_gradle()).toBeResolvedTo(path.normalize('/android/sdk/home/bin/gradle'));
}); });
it('without ANDROID_SDK_ROOT / with ANDROID_HOME', async () => { it('without ANDROID_SDK_ROOT / with ANDROID_HOME', async () => {
process.env.ANDROID_HOME = '/android/sdk/home'; process.env.ANDROID_HOME = path.normalize('/android/sdk/home');
await expectAsync(check_reqs.check_gradle()).toBeResolvedTo('/android/sdk/home/bin/gradle'); await expectAsync(check_reqs.check_gradle()).toBeResolvedTo(path.normalize('/android/sdk/home/bin/gradle'));
}); });
it('without ANDROID_SDK_ROOT / without ANDROID_HOME', () => { it('without ANDROID_SDK_ROOT / without ANDROID_HOME', () => {
@ -250,7 +250,7 @@ describe('check_reqs', function () {
}); });
it('should error if sdk is installed but no gradle found', () => { it('should error if sdk is installed but no gradle found', () => {
process.env.ANDROID_SDK_ROOT = '/android/sdk'; process.env.ANDROID_SDK_ROOT = path.normalize('/android/sdk');
spyOn(check_reqs, 'get_gradle_wrapper').and.callFake(() => { spyOn(check_reqs, 'get_gradle_wrapper').and.callFake(() => {
return ''; return '';
}); });