@@ -22,6 +22,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
22
22
] ;
23
23
24
24
private _androidProjectPropertiesManagers : IDictionary < IAndroidProjectPropertiesManager > ;
25
+ private isAndroidStudioTemplate : boolean ;
25
26
26
27
constructor ( private $androidEmulatorServices : Mobile . IEmulatorPlatformServices ,
27
28
private $androidToolsInfo : IAndroidToolsInfo ,
@@ -38,6 +39,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
38
39
private $npm : INodePackageManager ) {
39
40
super ( $fs , $projectDataService ) ;
40
41
this . _androidProjectPropertiesManagers = Object . create ( null ) ;
42
+ this . isAndroidStudioTemplate = false ;
41
43
}
42
44
43
45
private _platformsDirCache : string = null ;
@@ -46,19 +48,41 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
46
48
if ( ! projectData && ! this . _platformData ) {
47
49
throw new Error ( "First call of getPlatformData without providing projectData." ) ;
48
50
}
51
+ if ( projectData && projectData . platformsDir ) {
52
+ const projectRoot = path . join ( projectData . platformsDir , AndroidProjectService . ANDROID_PLATFORM_NAME ) ;
53
+ if ( this . isAndroidStudioCompatibleTemplate ( projectData ) ) {
54
+ this . isAndroidStudioTemplate = true ;
55
+ }
56
+
57
+ const appDestinationDirectoryArr = [ projectRoot ] ;
58
+ if ( this . isAndroidStudioTemplate ) {
59
+ appDestinationDirectoryArr . push ( constants . APP_FOLDER_NAME ) ;
60
+ }
61
+ appDestinationDirectoryArr . push ( constants . SRC_DIR , constants . MAIN_DIR , constants . ASSETS_DIR ) ;
62
+
63
+ const configurationsDirectoryArr = [ projectRoot ] ;
64
+ if ( this . isAndroidStudioTemplate ) {
65
+ configurationsDirectoryArr . push ( constants . APP_FOLDER_NAME ) ;
66
+ }
67
+ configurationsDirectoryArr . push ( constants . SRC_DIR , constants . MAIN_DIR , constants . MANIFEST_FILE_NAME ) ;
68
+
69
+ const deviceBuildOutputArr = [ projectRoot ] ;
70
+ if ( this . isAndroidStudioTemplate ) {
71
+ deviceBuildOutputArr . push ( constants . APP_FOLDER_NAME ) ;
72
+ }
73
+ deviceBuildOutputArr . push ( constants . BUILD_DIR , constants . OUTPUTS_DIR , constants . APK_DIR ) ;
49
74
50
- if ( projectData && projectData . platformsDir && this . _platformsDirCache !== projectData . platformsDir ) {
51
75
this . _platformsDirCache = projectData . platformsDir ;
52
- const projectRoot = path . join ( projectData . platformsDir , AndroidProjectService . ANDROID_PLATFORM_NAME ) ;
53
76
const packageName = this . getProjectNameFromId ( projectData ) ;
77
+
54
78
this . _platformData = {
55
- frameworkPackageName : "tns-android" ,
79
+ frameworkPackageName : constants . TNS_ANDROID_RUNTIME_NAME ,
56
80
normalizedPlatformName : "Android" ,
57
- appDestinationDirectoryPath : path . join ( projectRoot , "src" , "main" , "assets" ) ,
81
+ appDestinationDirectoryPath : path . join ( ... appDestinationDirectoryArr ) ,
58
82
platformProjectService : this ,
59
83
emulatorServices : this . $androidEmulatorServices ,
60
84
projectRoot : projectRoot ,
61
- deviceBuildOutputPath : path . join ( projectRoot , "build" , "outputs" , "apk" ) ,
85
+ deviceBuildOutputPath : path . join ( ... deviceBuildOutputArr ) ,
62
86
getValidPackageNames : ( buildOptions : { isReleaseBuild ?: boolean , isForDevice ?: boolean } ) : string [ ] => {
63
87
const buildMode = buildOptions . isReleaseBuild ? Configurations . Release . toLowerCase ( ) : Configurations . Debug . toLowerCase ( ) ;
64
88
@@ -69,11 +93,12 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
69
93
] ;
70
94
} ,
71
95
frameworkFilesExtensions : [ ".jar" , ".dat" , ".so" ] ,
72
- configurationFileName : "AndroidManifest.xml" ,
73
- configurationFilePath : path . join ( projectRoot , "src" , "main" , "AndroidManifest.xml" ) ,
74
- relativeToFrameworkConfigurationFilePath : path . join ( "src" , "main" , "AndroidManifest.xml" ) ,
96
+ configurationFileName : constants . MANIFEST_FILE_NAME ,
97
+ configurationFilePath : path . join ( ... configurationsDirectoryArr ) ,
98
+ relativeToFrameworkConfigurationFilePath : path . join ( constants . SRC_DIR , constants . MAIN_DIR , constants . MANIFEST_FILE_NAME ) ,
75
99
fastLivesyncFileExtensions : [ ".jpg" , ".gif" , ".png" , ".bmp" , ".webp" ] // http://developer.android.com/guide/appendix/media-formats.html
76
100
} ;
101
+
77
102
}
78
103
79
104
return this . _platformData ;
@@ -93,10 +118,16 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
93
118
94
119
public getAppResourcesDestinationDirectoryPath ( projectData : IProjectData , frameworkVersion ?: string ) : string {
95
120
if ( this . canUseGradle ( projectData , frameworkVersion ) ) {
96
- return path . join ( this . getPlatformData ( projectData ) . projectRoot , "src" , "main" , "res" ) ;
121
+ const resourcePath : string [ ] = [ constants . SRC_DIR , constants . MAIN_DIR , constants . RESOURCES_DIR ] ;
122
+ if ( this . isAndroidStudioTemplate ) {
123
+ resourcePath . unshift ( constants . APP_FOLDER_NAME ) ;
124
+ }
125
+
126
+ return path . join ( this . getPlatformData ( projectData ) . projectRoot , ...resourcePath ) ;
127
+
97
128
}
98
129
99
- return path . join ( this . getPlatformData ( projectData ) . projectRoot , "res" ) ;
130
+ return path . join ( this . getPlatformData ( projectData ) . projectRoot , constants . RESOURCES_DIR ) ;
100
131
}
101
132
102
133
public async validate ( projectData : IProjectData ) : Promise < void > {
@@ -125,25 +156,31 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
125
156
const androidToolsInfo = this . $androidToolsInfo . getToolsInfo ( ) ;
126
157
const targetSdkVersion = androidToolsInfo && androidToolsInfo . targetSdkVersion ;
127
158
this . $logger . trace ( `Using Android SDK '${ targetSdkVersion } '.` ) ;
128
- this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , "libs" , "-R" ) ;
129
159
130
- if ( config . pathToTemplate ) {
131
- const mainPath = path . join ( this . getPlatformData ( projectData ) . projectRoot , "src" , "main" ) ;
132
- this . $fs . createDirectory ( mainPath ) ;
133
- shell . cp ( "-R" , path . join ( path . resolve ( config . pathToTemplate ) , "*" ) , mainPath ) ;
160
+ this . isAndroidStudioTemplate = this . isAndroidStudioCompatibleTemplate ( projectData ) ;
161
+ if ( this . isAndroidStudioTemplate ) {
162
+ this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , "*" , "-R" ) ;
134
163
} else {
135
- this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , "src" , "-R" ) ;
136
- }
137
- this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , "build.gradle settings.gradle build-tools" , "-Rf" ) ;
164
+ this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , "libs" , "-R" ) ;
138
165
139
- try {
140
- this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , "gradle.properties" , "-Rf" ) ;
141
- } catch ( e ) {
142
- this . $logger . warn ( `\n${ e } \nIt's possible, the final .apk file will contain all architectures instead of the ones described in the abiFilters!\nYou can fix this by using the latest android platform.` ) ;
143
- }
166
+ if ( config . pathToTemplate ) {
167
+ const mainPath = path . join ( this . getPlatformData ( projectData ) . projectRoot , constants . SRC_DIR , constants . MAIN_DIR ) ;
168
+ this . $fs . createDirectory ( mainPath ) ;
169
+ shell . cp ( "-R" , path . join ( path . resolve ( config . pathToTemplate ) , "*" ) , mainPath ) ;
170
+ } else {
171
+ this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , constants . SRC_DIR , "-R" ) ;
172
+ }
173
+ this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , "build.gradle settings.gradle build-tools" , "-Rf" ) ;
144
174
145
- this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , "gradle" , "-R" ) ;
146
- this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , "gradlew gradlew.bat" , "-f" ) ;
175
+ try {
176
+ this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , "gradle.properties" , "-Rf" ) ;
177
+ } catch ( e ) {
178
+ this . $logger . warn ( `\n${ e } \nIt's possible, the final .apk file will contain all architectures instead of the ones described in the abiFilters!\nYou can fix this by using the latest android platform.` ) ;
179
+ }
180
+
181
+ this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , "gradle" , "-R" ) ;
182
+ this . copy ( this . getPlatformData ( projectData ) . projectRoot , frameworkDir , "gradlew gradlew.bat" , "-f" ) ;
183
+ }
147
184
148
185
this . cleanResValues ( targetSdkVersion , projectData , frameworkVersion ) ;
149
186
@@ -269,8 +306,11 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
269
306
buildOptions . unshift ( "--stacktrace" ) ;
270
307
buildOptions . unshift ( "--debug" ) ;
271
308
}
272
-
273
- buildOptions . unshift ( "buildapk" ) ;
309
+ if ( buildConfig . release ) {
310
+ buildOptions . unshift ( "assembleRelease" ) ;
311
+ } else {
312
+ buildOptions . unshift ( "assembleDebug" ) ;
313
+ }
274
314
275
315
const handler = ( data : any ) => {
276
316
this . emit ( constants . BUILD_OUTPUT_EVENT_NAME , data ) ;
@@ -380,7 +420,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
380
420
const flattenedDependencyName = isScoped ? pluginData . name . replace ( "/" , "_" ) : pluginData . name ;
381
421
382
422
// Copy all resources from plugin
383
- const resourcesDestinationDirectoryPath = path . join ( this . getPlatformData ( projectData ) . projectRoot , "src" , flattenedDependencyName ) ;
423
+ const resourcesDestinationDirectoryPath = path . join ( this . getPlatformData ( projectData ) . projectRoot , constants . SRC_DIR , flattenedDependencyName ) ;
384
424
this . $fs . ensureDirectoryExists ( resourcesDestinationDirectoryPath ) ;
385
425
shell . cp ( "-Rf" , path . join ( pluginPlatformsFolderPath , "*" ) , resourcesDestinationDirectoryPath ) ;
386
426
@@ -593,6 +633,28 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
593
633
const normalizedPlatformVersion = `${ semver . major ( platformVersion ) } .${ semver . minor ( platformVersion ) } .0` ;
594
634
return semver . gte ( normalizedPlatformVersion , newRuntimeGradleRoutineVersion ) ;
595
635
}
636
+
637
+ private isAndroidStudioCompatibleTemplate ( projectData : IProjectData ) : boolean {
638
+ const currentPlatformData : IDictionary < any > = this . $projectDataService . getNSValue ( projectData . projectDir , constants . TNS_ANDROID_RUNTIME_NAME ) ;
639
+ let platformVersion = currentPlatformData && currentPlatformData [ constants . VERSION_STRING ] ;
640
+
641
+ if ( ! platformVersion ) {
642
+ const tnsAndroidPackageJsonPath = path . join ( projectData . projectDir , constants . NODE_MODULES_FOLDER_NAME , constants . TNS_ANDROID_RUNTIME_NAME , constants . PACKAGE_JSON_FILE_NAME ) ;
643
+ if ( this . $fs . exists ( tnsAndroidPackageJsonPath ) ) {
644
+ const projectPackageJson : any = this . $fs . readJson ( tnsAndroidPackageJsonPath ) ;
645
+ if ( projectPackageJson && projectPackageJson . version ) {
646
+ platformVersion = projectPackageJson . version ;
647
+ }
648
+ } else {
649
+ return false ;
650
+ }
651
+ }
652
+
653
+ const androidStudioCompatibleTemplate = "3.4.0" ;
654
+ const normalizedPlatformVersion = `${ semver . major ( platformVersion ) } .${ semver . minor ( platformVersion ) } .0` ;
655
+
656
+ return semver . gte ( normalizedPlatformVersion , androidStudioCompatibleTemplate ) ;
657
+ }
596
658
}
597
659
598
660
$injector . register ( "androidProjectService" , AndroidProjectService ) ;
0 commit comments