@@ -5,7 +5,6 @@ import * as semver from "semver";
5
5
import * as projectServiceBaseLib from "./platform-project-service-base" ;
6
6
import { DeviceAndroidDebugBridge } from "../common/mobile/android/device-android-debug-bridge" ;
7
7
import { attachAwaitDetach } from "../common/helpers" ;
8
- import { EOL } from "os" ;
9
8
import { Configurations } from "../common/constants" ;
10
9
import { SpawnOptions } from "child_process" ;
11
10
@@ -36,7 +35,8 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
36
35
private $injector : IInjector ,
37
36
private $pluginVariablesService : IPluginVariablesService ,
38
37
private $devicePlatformsConstants : Mobile . IDevicePlatformsConstants ,
39
- private $npm : INodePackageManager ) {
38
+ private $npm : INodePackageManager ,
39
+ private $projectV4MigrationService : IProjectV4MigrationService ) {
40
40
super ( $fs , $projectDataService ) ;
41
41
this . _androidProjectPropertiesManagers = Object . create ( null ) ;
42
42
this . isAndroidStudioTemplate = false ;
@@ -133,18 +133,32 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
133
133
return Promise . resolve ( true ) ;
134
134
}
135
135
136
- public getAppResourcesDestinationDirectoryPath ( projectData : IProjectData , frameworkVersion ?: string ) : string {
137
- if ( this . canUseGradle ( projectData , frameworkVersion ) ) {
138
- const resourcePath : string [ ] = [ constants . SRC_DIR , constants . MAIN_DIR , constants . RESOURCES_DIR ] ;
139
- if ( this . isAndroidStudioTemplate ) {
140
- resourcePath . unshift ( constants . APP_FOLDER_NAME ) ;
141
- }
136
+ public getAppResourcesDestinationDirectoryPath ( projectData : IProjectData ) : string {
137
+ const appResourcesDirStructureHasMigrated = this . $projectV4MigrationService . hasMigrated ( projectData . getAppResourcesDirectoryPath ( ) ) ;
138
+
139
+ if ( appResourcesDirStructureHasMigrated ) {
140
+ return this . getAppResourcesDestinationDirectoryPathUpdatedAppResourcesDirStructure ( projectData ) ;
141
+ } else {
142
+ return this . getAppResourcesDestinationDirectoryPathOldAppResourcesDirStructure ( projectData ) ;
143
+ }
144
+ }
145
+
146
+ private getAppResourcesDestinationDirectoryPathOldAppResourcesDirStructure ( projectData : IProjectData ) : string {
147
+ const resourcePath : string [ ] = [ constants . SRC_DIR , constants . MAIN_DIR , constants . RESOURCES_DIR ] ;
148
+ if ( this . isAndroidStudioTemplate ) {
149
+ resourcePath . unshift ( constants . APP_FOLDER_NAME ) ;
150
+ }
142
151
143
- return path . join ( this . getPlatformData ( projectData ) . projectRoot , ...resourcePath ) ;
152
+ return path . join ( this . getPlatformData ( projectData ) . projectRoot , ...resourcePath ) ;
153
+ }
144
154
155
+ private getAppResourcesDestinationDirectoryPathUpdatedAppResourcesDirStructure ( projectData : IProjectData ) : string {
156
+ const resourcePath : string [ ] = [ constants . SRC_DIR ] ;
157
+ if ( this . isAndroidStudioTemplate ) {
158
+ resourcePath . unshift ( constants . APP_FOLDER_NAME ) ;
145
159
}
146
160
147
- return path . join ( this . getPlatformData ( projectData ) . projectRoot , constants . RESOURCES_DIR ) ;
161
+ return path . join ( this . getPlatformData ( projectData ) . projectRoot , ... resourcePath ) ;
148
162
}
149
163
150
164
public async validate ( projectData : IProjectData ) : Promise < void > {
@@ -199,7 +213,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
199
213
this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , "gradlew gradlew.bat" , "-f" ) ;
200
214
}
201
215
202
- this . cleanResValues ( targetSdkVersion , projectData , frameworkVersion ) ;
216
+ this . cleanResValues ( targetSdkVersion , projectData ) ;
203
217
204
218
const npmConfig : INodePackageManagerInstallOptions = {
205
219
save : true ,
@@ -235,8 +249,8 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
235
249
}
236
250
}
237
251
238
- private cleanResValues ( targetSdkVersion : number , projectData : IProjectData , frameworkVersion : string ) : void {
239
- const resDestinationDir = this . getAppResourcesDestinationDirectoryPath ( projectData , frameworkVersion ) ;
252
+ private cleanResValues ( targetSdkVersion : number , projectData : IProjectData ) : void {
253
+ const resDestinationDir = this . getAppResourcesDestinationDirectoryPath ( projectData ) ;
240
254
const directoriesInResFolder = this . $fs . readDirectory ( resDestinationDir ) ;
241
255
const directoriesToClean = directoriesInResFolder
242
256
. map ( dir => {
@@ -260,8 +274,16 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
260
274
public async interpolateData ( projectData : IProjectData , platformSpecificData : IPlatformSpecificData ) : Promise < void > {
261
275
// Interpolate the apilevel and package
262
276
this . interpolateConfigurationFile ( projectData , platformSpecificData ) ;
277
+ const appResourcesDirectoryPath = projectData . getAppResourcesDirectoryPath ( ) ;
278
+
279
+ let stringsFilePath : string ;
280
+
281
+ if ( ! this . $projectV4MigrationService . hasMigrated ( appResourcesDirectoryPath ) ) {
282
+ stringsFilePath = path . join ( this . getAppResourcesDestinationDirectoryPath ( projectData ) , 'values' , 'strings.xml' ) ;
283
+ } else {
284
+ stringsFilePath = path . join ( this . getAppResourcesDestinationDirectoryPath ( projectData ) , "main" , "res" , 'values' , 'strings.xml' ) ;
285
+ }
263
286
264
- const stringsFilePath = path . join ( this . getAppResourcesDestinationDirectoryPath ( projectData ) , 'values' , 'strings.xml' ) ;
265
287
shell . sed ( '-i' , / _ _ N A M E _ _ / , projectData . projectName , stringsFilePath ) ;
266
288
shell . sed ( '-i' , / _ _ T I T L E _ A C T I V I T Y _ _ / , projectData . projectName , stringsFilePath ) ;
267
289
@@ -317,33 +339,28 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
317
339
}
318
340
319
341
public async buildProject ( projectRoot : string , projectData : IProjectData , buildConfig : IBuildConfig ) : Promise < void > {
320
- if ( this . canUseGradle ( projectData ) ) {
321
- const buildOptions = this . getBuildOptions ( buildConfig , projectData ) ;
322
- if ( this . $logger . getLevel ( ) === "TRACE" ) {
323
- buildOptions . unshift ( "--stacktrace" ) ;
324
- buildOptions . unshift ( "--debug" ) ;
325
- }
326
- if ( buildConfig . release ) {
327
- buildOptions . unshift ( "assembleRelease" ) ;
328
- } else {
329
- buildOptions . unshift ( "assembleDebug" ) ;
330
- }
331
-
332
- const handler = ( data : any ) => {
333
- this . emit ( constants . BUILD_OUTPUT_EVENT_NAME , data ) ;
334
- } ;
335
-
336
- await attachAwaitDetach ( constants . BUILD_OUTPUT_EVENT_NAME ,
337
- this . $childProcess ,
338
- handler ,
339
- this . executeGradleCommand ( this . getPlatformData ( projectData ) . projectRoot ,
340
- buildOptions ,
341
- { stdio : buildConfig . buildOutputStdio || "inherit" } ,
342
- { emitOptions : { eventName : constants . BUILD_OUTPUT_EVENT_NAME } , throwError : true } ) ) ;
342
+ const buildOptions = this . getBuildOptions ( buildConfig , projectData ) ;
343
+ if ( this . $logger . getLevel ( ) === "TRACE" ) {
344
+ buildOptions . unshift ( "--stacktrace" ) ;
345
+ buildOptions . unshift ( "--debug" ) ;
346
+ }
347
+ if ( buildConfig . release ) {
348
+ buildOptions . unshift ( "assembleRelease" ) ;
343
349
} else {
344
- this . $errors . failWithoutHelp ( "Cannot complete build because this project is ANT-based." + EOL +
345
- "Run `tns platform remove android && tns platform add android` to switch to Gradle and try again." ) ;
350
+ buildOptions . unshift ( "assembleDebug" ) ;
346
351
}
352
+
353
+ const handler = ( data : any ) => {
354
+ this . emit ( constants . BUILD_OUTPUT_EVENT_NAME , data ) ;
355
+ } ;
356
+
357
+ await attachAwaitDetach ( constants . BUILD_OUTPUT_EVENT_NAME ,
358
+ this . $childProcess ,
359
+ handler ,
360
+ this . executeGradleCommand ( this . getPlatformData ( projectData ) . projectRoot ,
361
+ buildOptions ,
362
+ { stdio : buildConfig . buildOutputStdio || "inherit" } ,
363
+ { emitOptions : { eventName : constants . BUILD_OUTPUT_EVENT_NAME } , throwError : true } ) ) ;
347
364
}
348
365
349
366
private getBuildOptions ( settings : IAndroidBuildOptionsSettings , projectData : IProjectData ) : Array < string > {
@@ -391,7 +408,15 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
391
408
}
392
409
393
410
public ensureConfigurationFileInAppResources ( projectData : IProjectData ) : void {
394
- const originalAndroidManifestFilePath = path . join ( projectData . appResourcesDirectoryPath , this . $devicePlatformsConstants . Android , this . getPlatformData ( projectData ) . configurationFileName ) ;
411
+ const appResourcesDirectoryPath = projectData . appResourcesDirectoryPath ;
412
+ const appResourcesDirStructureHasMigrated = this . $projectV4MigrationService . hasMigrated ( appResourcesDirectoryPath ) ;
413
+ let originalAndroidManifestFilePath ;
414
+
415
+ if ( appResourcesDirStructureHasMigrated ) {
416
+ originalAndroidManifestFilePath = path . join ( appResourcesDirectoryPath , this . $devicePlatformsConstants . Android , "src" , "main" , this . getPlatformData ( projectData ) . configurationFileName ) ;
417
+ } else {
418
+ originalAndroidManifestFilePath = path . join ( appResourcesDirectoryPath , this . $devicePlatformsConstants . Android , this . getPlatformData ( projectData ) . configurationFileName ) ;
419
+ }
395
420
396
421
const manifestExists = this . $fs . exists ( originalAndroidManifestFilePath ) ;
397
422
@@ -400,16 +425,13 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
400
425
return ;
401
426
}
402
427
// Overwrite the AndroidManifest from runtime.
403
- this . $fs . copyFile ( originalAndroidManifestFilePath , this . getPlatformData ( projectData ) . configurationFilePath ) ;
428
+ if ( ! appResourcesDirStructureHasMigrated ) {
429
+ this . $fs . copyFile ( originalAndroidManifestFilePath , this . getPlatformData ( projectData ) . configurationFilePath ) ;
430
+ }
404
431
}
405
432
406
433
public prepareAppResources ( appResourcesDirectoryPath : string , projectData : IProjectData ) : void {
407
- const resourcesDirPath = path . join ( appResourcesDirectoryPath , this . getPlatformData ( projectData ) . normalizedPlatformName ) ;
408
- const valuesDirRegExp = / ^ v a l u e s / ;
409
- const resourcesDirs = this . $fs . readDirectory ( resourcesDirPath ) . filter ( resDir => ! resDir . match ( valuesDirRegExp ) ) ;
410
- _ . each ( resourcesDirs , resourceDir => {
411
- this . $fs . deleteDirectory ( path . join ( this . getAppResourcesDestinationDirectoryPath ( projectData ) , resourceDir ) ) ;
412
- } ) ;
434
+ // Intentionally left empty
413
435
}
414
436
415
437
public async preparePluginNativeCode ( pluginData : IPluginData , projectData : IProjectData ) : Promise < void > {
@@ -559,20 +581,6 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
559
581
// Nothing android specific to check yet.
560
582
}
561
583
562
- private _canUseGradle : boolean ;
563
- private canUseGradle ( projectData : IProjectData , frameworkVersion ?: string ) : boolean {
564
- if ( ! this . _canUseGradle ) {
565
- if ( ! frameworkVersion ) {
566
- const frameworkInfoInProjectFile = this . $projectDataService . getNSValue ( projectData . projectDir , this . getPlatformData ( projectData ) . frameworkPackageName ) ;
567
- frameworkVersion = frameworkInfoInProjectFile && frameworkInfoInProjectFile . version ;
568
- }
569
-
570
- this . _canUseGradle = ! frameworkVersion || semver . gte ( frameworkVersion , AndroidProjectService . MIN_RUNTIME_VERSION_WITH_GRADLE ) ;
571
- }
572
-
573
- return this . _canUseGradle ;
574
- }
575
-
576
584
private copy ( projectRoot : string , frameworkDir : string , files : string , cpArg : string ) : void {
577
585
const paths = files . split ( ' ' ) . map ( p => path . join ( frameworkDir , p ) ) ;
578
586
shell . cp ( cpArg , paths , projectRoot ) ;
0 commit comments