@@ -10,20 +10,31 @@ import * as _ from "lodash";
1010
1111export class AndroidToolsInfo implements NativeScriptDoctor . IAndroidToolsInfo {
1212 public readonly ANDROID_TARGET_PREFIX = "android" ;
13- private static SUPPORTED_TARGETS = [
14- "android-17" ,
15- "android-18" ,
16- "android-19" ,
17- "android-21" ,
18- "android-22" ,
19- "android-23" ,
20- "android-24" ,
21- "android-25" ,
22- "android-26" ,
23- "android-27" ,
24- "android-28" ,
25- "android-29"
26- ] ;
13+ private getSupportedTargets ( projectDir : string ) {
14+ const runtimeVersion = this . getRuntimeVersion ( { projectDir} ) ;
15+ let baseTargets = [
16+ "android-17" ,
17+ "android-18" ,
18+ "android-19" ,
19+ "android-21" ,
20+ "android-22" ,
21+ "android-23" ,
22+ "android-24" ,
23+ "android-25" ,
24+ "android-26" ,
25+ "android-27" ,
26+ "android-28" ,
27+ "android-29"
28+ ] ;
29+
30+ if ( runtimeVersion && semver . lt ( semver . coerce ( runtimeVersion ) , "6.1.0" ) ) {
31+ baseTargets . sort ( ) ;
32+ const indexOfSdk29 = baseTargets . indexOf ( "android-29" ) ;
33+ baseTargets = baseTargets . slice ( 0 , indexOfSdk29 ) ;
34+ }
35+
36+ return baseTargets ;
37+ }
2738 private static MIN_REQUIRED_COMPILE_TARGET = 28 ;
2839 private static REQUIRED_BUILD_TOOLS_RANGE_PREFIX = ">=23" ;
2940 private static VERSION_REGEX = / ( ( \d + \. ) { 2 } \d + ) / ;
@@ -40,23 +51,23 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
4051 private hostInfo : HostInfo ,
4152 private helpers : Helpers ) { }
4253
43- public getToolsInfo ( ) : NativeScriptDoctor . IAndroidToolsInfoData {
54+ public getToolsInfo ( config : Partial < NativeScriptDoctor . IProjectDir > = { } ) : NativeScriptDoctor . IAndroidToolsInfoData {
4455 if ( ! this . toolsInfo ) {
4556 const infoData : NativeScriptDoctor . IAndroidToolsInfoData = Object . create ( null ) ;
4657 infoData . androidHomeEnvVar = this . androidHome ;
4758 infoData . installedTargets = this . getInstalledTargets ( ) ;
48- infoData . compileSdkVersion = this . getCompileSdk ( infoData . installedTargets ) ;
49- infoData . buildToolsVersion = this . getBuildToolsVersion ( ) ;
59+ infoData . compileSdkVersion = this . getCompileSdk ( infoData . installedTargets , config . projectDir ) ;
60+ infoData . buildToolsVersion = this . getBuildToolsVersion ( config . projectDir ) ;
5061
5162 this . toolsInfo = infoData ;
5263 }
5364
5465 return this . toolsInfo ;
5566 }
5667
57- public validateInfo ( ) : NativeScriptDoctor . IWarning [ ] {
68+ public validateInfo ( config : Partial < NativeScriptDoctor . IProjectDir > = { } ) : NativeScriptDoctor . IWarning [ ] {
5869 const errors : NativeScriptDoctor . IWarning [ ] = [ ] ;
59- const toolsInfoData = this . getToolsInfo ( ) ;
70+ const toolsInfoData = this . getToolsInfo ( config ) ;
6071 const isAndroidHomeValid = this . isAndroidHomeValid ( ) ;
6172 if ( ! toolsInfoData . compileSdkVersion ) {
6273 errors . push ( {
@@ -67,7 +78,7 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
6778 }
6879
6980 if ( ! toolsInfoData . buildToolsVersion ) {
70- const buildToolsRange = this . getBuildToolsRange ( ) ;
81+ const buildToolsRange = this . getBuildToolsRange ( config . projectDir ) ;
7182 const versionRangeMatches = buildToolsRange . match ( / ^ .* ?( [ \d \. ] + ) \s + .* ?( [ \d \. ] + ) $ / ) ;
7283 let message = `You can install any version in the following range: '${ buildToolsRange } '.` ;
7384
@@ -110,7 +121,7 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
110121 if ( semver . lt ( installedJavaCompilerSemverVersion , AndroidToolsInfo . MIN_JAVA_VERSION ) ) {
111122 warning = `Javac version ${ installedJavaCompilerVersion } is not supported. You have to install at least ${ AndroidToolsInfo . MIN_JAVA_VERSION } .` ;
112123 } else {
113- runtimeVersion = this . getRealRuntimeVersion ( runtimeVersion || this . getAndroidRuntimeVersionFromProjectDir ( projectDir ) ) ;
124+ runtimeVersion = this . getRuntimeVersion ( { runtimeVersion, projectDir} ) ;
114125 if ( runtimeVersion ) {
115126 // get the item from the dictionary that corresponds to our current Javac version:
116127 let runtimeMinVersion : string = null ;
@@ -184,13 +195,14 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
184195 return errors ;
185196 }
186197
187- public validateMinSupportedTargetSdk ( targetSdk : number ) : NativeScriptDoctor . IWarning [ ] {
198+ public validateMinSupportedTargetSdk ( { targetSdk, projectDir } : NativeScriptDoctor . ITargetValidationOptions ) : NativeScriptDoctor . IWarning [ ] {
188199 const errors : NativeScriptDoctor . IWarning [ ] = [ ] ;
189200 const newTarget = `${ this . ANDROID_TARGET_PREFIX } -${ targetSdk } ` ;
190- const targetSupported = _ . includes ( AndroidToolsInfo . SUPPORTED_TARGETS , newTarget ) ;
201+ const supportedTargets = this . getSupportedTargets ( projectDir ) ;
202+ const targetSupported = _ . includes ( supportedTargets , newTarget ) ;
191203
192- if ( ! _ . includes ( AndroidToolsInfo . SUPPORTED_TARGETS , newTarget ) ) {
193- const supportedVersions = AndroidToolsInfo . SUPPORTED_TARGETS . sort ( ) ;
204+ if ( ! _ . includes ( supportedTargets , newTarget ) ) {
205+ const supportedVersions = supportedTargets . sort ( ) ;
194206 const minSupportedVersion = this . parseAndroidSdkString ( _ . first ( supportedVersions ) ) ;
195207
196208 if ( ! targetSupported && targetSdk && ( targetSdk < minSupportedVersion ) ) {
@@ -205,12 +217,12 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
205217 return errors ;
206218 }
207219
208- public validataMaxSupportedTargetSdk ( targetSdk : number ) : NativeScriptDoctor . IWarning [ ] {
220+ public validataMaxSupportedTargetSdk ( { targetSdk, projectDir } : NativeScriptDoctor . ITargetValidationOptions ) : NativeScriptDoctor . IWarning [ ] {
209221 const errors : NativeScriptDoctor . IWarning [ ] = [ ] ;
210222 const newTarget = `${ this . ANDROID_TARGET_PREFIX } -${ targetSdk } ` ;
211- const targetSupported = _ . includes ( AndroidToolsInfo . SUPPORTED_TARGETS , newTarget ) ;
223+ const targetSupported = _ . includes ( this . getSupportedTargets ( projectDir ) , newTarget ) ;
212224
213- if ( ! targetSupported && ! targetSdk || targetSdk > this . getMaxSupportedVersion ( ) ) {
225+ if ( ! targetSupported && ! targetSdk || targetSdk > this . getMaxSupportedVersion ( projectDir ) ) {
214226 errors . push ( {
215227 warning :`Support for the selected Android target SDK ${ newTarget } is not verified. Your Android app might not work as expected.` ,
216228 additionalInformation : "" ,
@@ -264,8 +276,8 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
264276 return sdkManagementToolPath ;
265277 }
266278
267- private getCompileSdk ( installedTargets : string [ ] ) : number {
268- const latestValidAndroidTarget = this . getLatestValidAndroidTarget ( installedTargets ) ;
279+ private getCompileSdk ( installedTargets : string [ ] , projectDir : string ) : number {
280+ const latestValidAndroidTarget = this . getLatestValidAndroidTarget ( installedTargets , projectDir ) ;
269281 if ( latestValidAndroidTarget ) {
270282 const integerVersion = this . parseAndroidSdkString ( latestValidAndroidTarget ) ;
271283
@@ -300,23 +312,23 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
300312 return selectedVersion ;
301313 }
302314
303- private getBuildToolsRange ( ) : string {
304- return `${ AndroidToolsInfo . REQUIRED_BUILD_TOOLS_RANGE_PREFIX } <=${ this . getMaxSupportedVersion ( ) } ` ;
315+ private getBuildToolsRange ( projectDir : string ) : string {
316+ return `${ AndroidToolsInfo . REQUIRED_BUILD_TOOLS_RANGE_PREFIX } <=${ this . getMaxSupportedVersion ( projectDir ) } ` ;
305317 }
306318
307- private getBuildToolsVersion ( ) : string {
319+ private getBuildToolsVersion ( projectDir : string ) : string {
308320 let buildToolsVersion : string ;
309321 if ( this . androidHome ) {
310322 const pathToBuildTools = path . join ( this . androidHome , "build-tools" ) ;
311- const buildToolsRange = this . getBuildToolsRange ( ) ;
323+ const buildToolsRange = this . getBuildToolsRange ( projectDir ) ;
312324 buildToolsVersion = this . getMatchingDir ( pathToBuildTools , buildToolsRange ) ;
313325 }
314326
315327 return buildToolsVersion ;
316328 }
317329
318- private getLatestValidAndroidTarget ( installedTargets : string [ ] ) : string {
319- return _ . findLast ( AndroidToolsInfo . SUPPORTED_TARGETS . sort ( ) , supportedTarget => _ . includes ( installedTargets , supportedTarget ) ) ;
330+ private getLatestValidAndroidTarget ( installedTargets : string [ ] , projectDir : string ) : string {
331+ return _ . findLast ( this . getSupportedTargets ( projectDir ) . sort ( ) , supportedTarget => _ . includes ( installedTargets , supportedTarget ) ) ;
320332 }
321333
322334 private parseAndroidSdkString ( androidSdkString : string ) : number {
@@ -336,8 +348,9 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
336348 }
337349 }
338350
339- private getMaxSupportedVersion ( ) : number {
340- return this . parseAndroidSdkString ( AndroidToolsInfo . SUPPORTED_TARGETS . sort ( ) [ AndroidToolsInfo . SUPPORTED_TARGETS . length - 1 ] ) ;
351+ private getMaxSupportedVersion ( projectDir : string ) : number {
352+ const supportedTargets = this . getSupportedTargets ( projectDir ) ;
353+ return this . parseAndroidSdkString ( supportedTargets . sort ( ) [ supportedTargets . length - 1 ] ) ;
341354 }
342355
343356 private getSystemRequirementsLink ( ) : string {
@@ -363,7 +376,8 @@ export class AndroidToolsInfo implements NativeScriptDoctor.IAndroidToolsInfo {
363376 return runtimeVersion ;
364377 }
365378
366- private getRealRuntimeVersion ( runtimeVersion : string ) : string {
379+ private getRuntimeVersion ( { runtimeVersion, projectDir } : { runtimeVersion ?: string , projectDir ?: string } ) : string {
380+ runtimeVersion = runtimeVersion || this . getAndroidRuntimeVersionFromProjectDir ( projectDir ) ;
367381 if ( runtimeVersion ) {
368382 // Check if the version is not "next" or "rc", i.e. tag from npm
369383 if ( ! semver . valid ( runtimeVersion ) ) {
0 commit comments