@@ -49,7 +49,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
49
49
50
50
if ( projectData && projectData . platformsDir && this . _platformsDirCache !== projectData . platformsDir ) {
51
51
this . _platformsDirCache = projectData . platformsDir ;
52
- const projectRoot = path . join ( projectData . platformsDir , "android" ) ;
52
+ const projectRoot = path . join ( projectData . platformsDir , AndroidProjectService . ANDROID_PLATFORM_NAME ) ;
53
53
const packageName = this . getProjectNameFromId ( projectData ) ;
54
54
this . _platformData = {
55
55
frameworkPackageName : "tns-android" ,
@@ -79,6 +79,14 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
79
79
return this . _platformData ;
80
80
}
81
81
82
+ // TODO: Remove prior to the 4.0 CLI release @Pip3r4o @PanayotCankov
83
+ // Similar to the private method of the same name in platform-service.
84
+ private getCurrentPlatformVersion ( platformData : IPlatformData , projectData : IProjectData ) : string {
85
+ const currentPlatformData : IDictionary < any > = this . $projectDataService . getNSValue ( projectData . projectDir , platformData . frameworkPackageName ) ;
86
+
87
+ return currentPlatformData && currentPlatformData [ constants . VERSION_STRING ] ;
88
+ }
89
+
82
90
public validateOptions ( ) : Promise < boolean > {
83
91
return Promise . resolve ( true ) ;
84
92
}
@@ -348,8 +356,12 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
348
356
}
349
357
350
358
public async preparePluginNativeCode ( pluginData : IPluginData , projectData : IProjectData ) : Promise < void > {
351
- const pluginPlatformsFolderPath = this . getPluginPlatformsFolderPath ( pluginData , AndroidProjectService . ANDROID_PLATFORM_NAME ) ;
352
- await this . processResourcesFromPlugin ( pluginData , pluginPlatformsFolderPath , projectData ) ;
359
+ if ( ! this . shouldUseNewRuntimeGradleRoutine ( projectData ) ) {
360
+ const pluginPlatformsFolderPath = this . getPluginPlatformsFolderPath ( pluginData , AndroidProjectService . ANDROID_PLATFORM_NAME ) ;
361
+ await this . processResourcesFromPlugin ( pluginData , pluginPlatformsFolderPath , projectData ) ;
362
+ }
363
+
364
+ // Do nothing, the Android Gradle script will configure itself based on the input dependencies.json
353
365
}
354
366
355
367
public async processConfigurationFilesFromAppResources ( ) : Promise < void > {
@@ -389,9 +401,13 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
389
401
public async removePluginNativeCode ( pluginData : IPluginData , projectData : IProjectData ) : Promise < void > {
390
402
try {
391
403
// check whether the dependency that's being removed has native code
392
- const pluginConfigDir = path . join ( this . getPlatformData ( projectData ) . projectRoot , "configurations" , pluginData . name ) ;
393
- if ( this . $fs . exists ( pluginConfigDir ) ) {
394
- await this . cleanProject ( this . getPlatformData ( projectData ) . projectRoot , projectData ) ;
404
+ // TODO: Remove prior to the 4.0 CLI release @Pip3r4o @PanayotCankov
405
+ // the updated gradle script will take care of cleaning the prepared android plugins
406
+ if ( ! this . shouldUseNewRuntimeGradleRoutine ( projectData ) ) {
407
+ const pluginConfigDir = path . join ( this . getPlatformData ( projectData ) . projectRoot , "configurations" , pluginData . name ) ;
408
+ if ( this . $fs . exists ( pluginConfigDir ) ) {
409
+ await this . cleanProject ( this . getPlatformData ( projectData ) . projectRoot , projectData ) ;
410
+ }
395
411
}
396
412
} catch ( e ) {
397
413
if ( e . code === "ENOENT" ) {
@@ -407,23 +423,61 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
407
423
}
408
424
409
425
public async beforePrepareAllPlugins ( projectData : IProjectData , dependencies ?: IDependencyData [ ] ) : Promise < void > {
426
+ const shouldUseNewRoutine = this . shouldUseNewRuntimeGradleRoutine ( projectData ) ;
427
+
410
428
if ( dependencies ) {
411
- const platformDir = path . join ( projectData . platformsDir , "android" ) ;
412
- const buildDir = path . join ( platformDir , "build-tools" ) ;
413
- const checkV8dependants = path . join ( buildDir , "check-v8-dependants.js" ) ;
414
- if ( this . $fs . exists ( checkV8dependants ) ) {
415
- const stringifiedDependencies = JSON . stringify ( dependencies ) ;
416
- try {
417
- await this . spawn ( 'node' , [ checkV8dependants , stringifiedDependencies , projectData . platformsDir ] , { stdio : "inherit" } ) ;
418
- } catch ( e ) {
419
- this . $logger . info ( "Checking for dependants on v8 public API failed. This is likely caused because of cyclic production dependencies. Error code: " + e . code + "\nMore information: https://github.com/NativeScript/nativescript-cli/issues/2561" ) ;
429
+ dependencies = this . filterUniqueDependencies ( dependencies ) ;
430
+ if ( shouldUseNewRoutine ) {
431
+ this . provideDependenciesJson ( projectData , dependencies ) ;
432
+ } else {
433
+ // TODO: Remove prior to the 4.0 CLI release @Pip3r4o @PanayotCankov
434
+
435
+ const platformDir = path . join ( projectData . platformsDir , AndroidProjectService . ANDROID_PLATFORM_NAME ) ;
436
+ const buildDir = path . join ( platformDir , "build-tools" ) ;
437
+ const checkV8dependants = path . join ( buildDir , "check-v8-dependants.js" ) ;
438
+ if ( this . $fs . exists ( checkV8dependants ) ) {
439
+ const stringifiedDependencies = JSON . stringify ( dependencies ) ;
440
+ try {
441
+ await this . spawn ( 'node' , [ checkV8dependants , stringifiedDependencies , projectData . platformsDir ] , { stdio : "inherit" } ) ;
442
+ } catch ( e ) {
443
+ this . $logger . info ( "Checking for dependants on v8 public API failed. This is likely caused because of cyclic production dependencies. Error code: " + e . code + "\nMore information: https://github.com/NativeScript/nativescript-cli/issues/2561" ) ;
444
+ }
420
445
}
421
446
}
422
447
}
423
448
424
- const projectRoot = this . getPlatformData ( projectData ) . projectRoot ;
449
+ if ( ! shouldUseNewRoutine ) {
450
+ // TODO: Remove prior to the 4.0 CLI release @Pip3r4o @PanayotCankov
451
+ const projectRoot = this . getPlatformData ( projectData ) . projectRoot ;
452
+ await this . cleanProject ( projectRoot , projectData ) ;
453
+ }
454
+ }
425
455
426
- await this . cleanProject ( projectRoot , projectData ) ;
456
+ private filterUniqueDependencies ( dependencies : IDependencyData [ ] ) : IDependencyData [ ] {
457
+ const depsDictionary = dependencies . reduce ( ( dict , dep ) => {
458
+ const collision = dict [ dep . name ] ;
459
+ // in case there are multiple dependencies to the same module, the one declared in the package.json takes precedence
460
+ if ( ! collision || collision . depth > dep . depth ) {
461
+ dict [ dep . name ] = dep ;
462
+ }
463
+ return dict ;
464
+ } , < IDictionary < IDependencyData > > { } ) ;
465
+ return _ . values ( depsDictionary ) ;
466
+ }
467
+
468
+ private provideDependenciesJson ( projectData : IProjectData , dependencies : IDependencyData [ ] ) : void {
469
+ const platformDir = path . join ( projectData . platformsDir , AndroidProjectService . ANDROID_PLATFORM_NAME ) ;
470
+ const dependenciesJsonPath = path . join ( platformDir , "dependencies.json" ) ;
471
+ const nativeDependencies = dependencies
472
+ . filter ( AndroidProjectService . isNativeAndroidDependency )
473
+ . map ( ( { name, directory } ) => ( { name, directory : path . relative ( platformDir , directory ) } ) ) ;
474
+ const jsonContent = JSON . stringify ( nativeDependencies , null , 4 ) ;
475
+
476
+ this . $fs . writeFile ( dependenciesJsonPath , jsonContent ) ;
477
+ }
478
+
479
+ private static isNativeAndroidDependency ( { nativescript } : IDependencyData ) : boolean {
480
+ return nativescript && ( nativescript . android || ( nativescript . platforms && nativescript . platforms . android ) ) ;
427
481
}
428
482
429
483
public stopServices ( projectRoot : string ) : Promise < ISpawnResult > {
@@ -530,6 +584,14 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
530
584
spawnFromEventOptions ) ;
531
585
}
532
586
}
587
+
588
+ // TODO: Remove prior to the 4.0 CLI release @Pip3r4o @PanayotCankov
589
+ private shouldUseNewRuntimeGradleRoutine ( projectData : IProjectData ) : boolean {
590
+ const platformVersion = this . getCurrentPlatformVersion ( this . getPlatformData ( projectData ) , projectData ) ;
591
+ const newRuntimeGradleRoutineVersion = "3.3.0" ;
592
+
593
+ return semver . gte ( platformVersion , newRuntimeGradleRoutineVersion ) ;
594
+ }
533
595
}
534
596
535
597
$injector . register ( "androidProjectService" , AndroidProjectService ) ;
0 commit comments