Skip to content

Commit 1e4d8da

Browse files
Merge pull request #554 from NativeScript/vladimirov/support-android-22
Add support for different Android SDKs
2 parents 46aa1b6 + 1320394 commit 1e4d8da

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

docs/man_pages/project/configuration/platform-add.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ platform add
33

44
Usage | Synopsis
55
------|-------
6-
Android latest runtime | `$ tns platform add android [--frameworkPath <File Path>] [--symlink]`
7-
Android selected runtime | `$ tns platform add android[@<Version>] [--frameworkPath <File Path>] [--symlink]`
6+
Android latest runtime | `$ tns platform add android [--frameworkPath <File Path>] [--symlink] [--sdk <target sdk>]`
7+
Android selected runtime | `$ tns platform add android[@<Version>] [--frameworkPath <File Path>] [--symlink] [--sdk <target sdk>]`
88
<% if (isMacOS) { %>iOS latest runtime | `$ tns platform add ios [--frameworkPath <File Path>] [--symlink]`
99
iOS selected runtime | `$ tns platform add ios[@<Version>] [--frameworkPath <File Path>] [--symlink]`<% } %>
1010

@@ -13,6 +13,7 @@ Configures the current project to target the selected platform. <% if(isHtml) {
1313
### Options
1414
* `--frameworkPath` - Sets the path to a NativeScript runtime for the specified platform that you want to use instead of the default runtime. If `--symlink` is specified, `<File Path>` must point to directory in which the runtime is already extracted. If `--symlink` is not specified, `<File Path>` must point to a valid npm package.
1515
* `--symlink` - Creates a symlink to a NativeScript runtime for the specified platform that you want to use instead of the default runtime. If `--frameworkPath` is specified, creates a symlink to the specified directory. If `--frameworkPath` is not specified, creates a symlink to platform runtime installed with your current version of NativeScript.
16+
* `--sdk` - Sets the Android target SDK. The value should be a valid Android API Level, for example 17, 19, MNC.
1617

1718
### Attributes
1819
* `<File Path>` is the complete path to a valid npm package or a directory that contains a NativeScript runtime for the selected platform.

lib/declarations.ts

+1
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,5 @@ interface IOptions extends ICommonOptions {
6262
keyStorePassword: string;
6363
keyStoreAlias: string;
6464
keyStoreAliasPassword: string;
65+
sdk: string;
6566
}

lib/options.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export class Options extends commonOptionsLibPath.OptionsBase {
2424
keyStorePath: { type: OptionType.String },
2525
keyStorePassword: { type: OptionType.String,},
2626
keyStoreAlias: { type: OptionType.String },
27-
keyStoreAliasPassword: { type: OptionType.String }
27+
keyStoreAliasPassword: { type: OptionType.String },
28+
sdk: { type: OptionType.String }
2829
},
2930
path.join($hostInfo.isWindows ? process.env.LocalAppData : path.join(osenv.home(), ".local/share"), ".nativescript-cli"),
3031
$errors, $staticConfig);

lib/services/android-project-service.ts

+30-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import fs = require("fs");
1010
import os = require("os");
1111

1212
class AndroidProjectService implements IPlatformProjectService {
13-
private SUPPORTED_TARGETS = ["android-17", "android-18", "android-19", "android-21"]; // forbidden for now: "android-MNC"
13+
private static MIN_SUPPORTED_VERSION = 17;
14+
private SUPPORTED_TARGETS = ["android-17", "android-18", "android-19", "android-21", "android-22"]; // forbidden for now: "android-MNC"
15+
private static ANDROID_TARGET_PREFIX = "android";
1416
private static RES_DIRNAME = "res";
1517
private static VALUES_DIRNAME = "values";
1618
private static VALUES_VERSION_DIRNAME_PREFIX = AndroidProjectService.VALUES_DIRNAME + "-v";
@@ -67,7 +69,9 @@ class AndroidProjectService implements IPlatformProjectService {
6769
public createProject(projectRoot: string, frameworkDir: string): IFuture<void> {
6870
return (() => {
6971
this.$fs.ensureDirectoryExists(projectRoot).wait();
70-
let newTarget = this.getLatestValidAndroidTarget(frameworkDir).wait();
72+
73+
let newTarget = this.getAndroidTarget(frameworkDir).wait();
74+
this.$logger.trace(`Using Android SDK '${newTarget}'.`);
7175
let versionNumber = _.last(newTarget.split("-"));
7276
if(this.$options.symlink) {
7377
this.copyResValues(projectRoot, frameworkDir, versionNumber).wait();
@@ -331,17 +335,37 @@ class AndroidProjectService implements IPlatformProjectService {
331335
}
332336
}
333337

338+
private getAndroidTarget(frameworkDir: string): IFuture<string> {
339+
return ((): string => {
340+
let newTarget = this.$options.sdk ? `${AndroidProjectService.ANDROID_TARGET_PREFIX}-${this.$options.sdk}` : this.getLatestValidAndroidTarget(frameworkDir).wait();
341+
if(!_.contains(this.SUPPORTED_TARGETS, newTarget)) {
342+
let versionNumber = parseInt(_.last(newTarget.split("-")));
343+
if(versionNumber && (versionNumber < AndroidProjectService.MIN_SUPPORTED_VERSION)) {
344+
this.$errors.failWithoutHelp(`The selected target SDK ${newTarget} is not supported. You should target at least ${AndroidProjectService.MIN_SUPPORTED_VERSION}.`);
345+
}
346+
347+
if(!_.contains(this.getInstalledTargets().wait(), newTarget)) {
348+
this.$errors.failWithoutHelp(`You have selected to use ${newTarget}, but it is not currently installed.`+
349+
' Run \"android\" from your command-line to install/update any missing SDKs or tools.');
350+
}
351+
this.$logger.warn(`The selected Android target '${newTarget}' is not verified as supported. Some functionality may not work as expected.`);
352+
}
353+
354+
return newTarget;
355+
}).future<string>()();
356+
}
357+
334358
private getLatestValidAndroidTarget(frameworkDir: string): IFuture<string> {
335359
return (() => {
336360
let validTarget = this.getTarget(frameworkDir).wait();
337361
let installedTargets = this.getInstalledTargets().wait();
338362

339363
// adjust to the latest available version
340-
var newTarget = _(this.SUPPORTED_TARGETS).sort().findLast(supportedTarget => _.contains(installedTargets, supportedTarget));
364+
var newTarget = _(this.SUPPORTED_TARGETS).sort().findLast(supportedTarget => _.contains(installedTargets, supportedTarget));
341365
if (!newTarget) {
342-
this.$errors.fail("Please install Android target %s. Make sure you have the latest Android tools installed as well." +
343-
" Run \"android\" from your command-line to install/update any missing SDKs or tools.",
344-
validTarget.split('-')[1]);
366+
this.$errors.failWithoutHelp(`Could not find supported Android target. Please install one of the following: ${this.SUPPORTED_TARGETS.join(", ")}.` +
367+
" Make sure you have the latest Android tools installed as well." +
368+
' Run "android" from your command-line to install/update any missing SDKs or tools.')
345369
}
346370

347371
return newTarget;

0 commit comments

Comments
 (0)