Welcome to mirror list, hosted at ThFree Co, Russian Federation.

build.gradle « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fdeeb9bcbfc0fba5fbc000fe8c018d42deb786c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
buildscript {
  repositories {
    google()
    jcenter()
    maven {
      url 'https://maven.fabric.io/public'
    }
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:3.5.0'
    classpath 'io.fabric.tools:gradle:1.27.0'
  }
}

allprojects {
  repositories {
    google()
    mavenCentral()
    jcenter()
    maven { url 'https://www.jitpack.io' }
  }
}

repositories {
  maven {
    url 'https://maven.fabric.io/public'
  }
  maven {
    url 'https://s3.amazonaws.com/moat-sdk-builds'
  }
  maven { url 'https://dl.bintray.com/google/flexbox-layout/' }
  maven { url 'https://dl.bintray.com/google/exoplayer/' }
}

apply plugin: 'com.android.application'
apply from: 'secure.properties'
apply plugin: 'io.fabric'

dependencies {

  implementation 'com.android.support:multidex:' + propMultiDexVersion

  implementation 'com.android.support:support-v4:' + propSupportLibraryVersion
  implementation 'com.android.support:appcompat-v7:' + propSupportLibraryVersion
  implementation 'com.android.support:recyclerview-v7:'+ propSupportLibraryVersion
  implementation 'com.android.support:design:'+ propSupportLibraryVersion
  implementation 'com.android.support:cardview-v7:'+ propSupportLibraryVersion
  implementation 'com.android.support:preference-v7:'+ propSupportLibraryVersion
  implementation 'com.android.support:preference-v14:'+ propSupportLibraryVersion
  implementation 'com.android.support:customtabs:'+ propSupportLibraryVersion
  implementation 'com.android.support:support-annotations:'+ propSupportLibraryVersion
  implementation 'com.android.support:support-compat:27.1.1'+ propSupportLibraryVersion

  implementation 'com.google.android.gms:play-services-location:17.0.0'
  implementation 'com.google.android.gms:play-services-analytics:17.0.0'
  implementation 'com.google.android.gms:play-services-gcm:17.0.0'
  implementation 'com.google.android.gms:play-services-auth:17.0.0'
  implementation 'com.google.android.gms:play-services-basement:17.1.1'

  // statistics
  implementation 'com.flurry.android:analytics:11.0.0@aar'
  // crash reporting
  implementation('com.crashlytics.sdk.android:crashlytics:2.9.4@aar') { transitive = true }
  implementation('com.crashlytics.sdk.android:crashlytics-ndk:2.0.4@aar') { transitive = true }
  // 3-party
  implementation 'com.facebook.android:facebook-android-sdk:4.40.0'
  implementation 'com.facebook.android:audience-network-sdk:4.28.2'
  implementation 'com.google.code.gson:gson:2.6.1'
  implementation 'com.pushwoosh:pushwoosh:5.19.1'
  implementation 'com.pushwoosh:pushwoosh-gcm:5.12.1'
  implementation 'com.my.tracker:mytracker-sdk:1.5.3'
  implementation ('com.my.target:mytarget-sdk:5.2.2') {
    exclude group: 'com.google.android.exoplayer', module:'exoplayer-core'
    exclude group: 'com.google.android.exoplayer', module:'exoplayer-dash'
    exclude group: 'com.google.android.exoplayer', module:'exoplayer-hls'
  }
  implementation('com.mopub:mopub-sdk-native-static:5.0.0@aar') {
    transitive = true;
  }
  implementation fileTree(dir: '3rd_party', include: '*.jar')
  // BottomSheet
  implementation 'com.cocosw:bottomsheet:1.5.0@aar'
  // LinearLayoutManager allowing wrap_content of RecyclerView-s
  // TODO remove this library when default LinearLayoutManager will be fixed.
  implementation 'org.solovyev.android.views:linear-layout-manager:0.5@aar'
  implementation 'com.timehop.stickyheadersrecyclerview:library:0.4.3@aar'
  // Glide
  implementation 'com.github.bumptech.glide:glide:3.7.0'
  // Java concurrency annotations
  implementation 'net.jcip:jcip-annotations:1.0'
  implementation 'com.appsflyer:af-android-sdk:4.9.0'
  implementation 'com.android.installreferrer:installreferrer:1.0'

  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  implementation 'com.android.billingclient:billing:1.1'
  implementation 'uk.co.samuelwall:material-tap-target-prompt:2.12.1'
  implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
  implementation 'com.google.android:flexbox:1.0.0'
  implementation 'com.trafi:anchor-bottom-sheet-behavior:0.13-alpha'
}

def getDate() {
  def date = new Date()
  def formattedDate = date.format('yyMMdd')
  return formattedDate
}

def osName = System.properties['os.name'].toLowerCase()

project.ext.versionCodes = ['armeabi-v7a': 30, 'x86': 40, 'arm64-v8a': 50, 'x86_64' : 60]
project.ext.appId = 'com.mapswithme.maps.pro'

crashlytics {
  enableNdk true
}

configurations.all {
  if (it.getName().contains("lint"))
    return

  resolutionStrategy {
    force 'com.android.support:multidex:' + propMultiDexVersion

    force 'com.android.support:support-v4:' + propSupportLibraryVersion
    force 'com.android.support:appcompat-v7:' + propSupportLibraryVersion
    force 'com.android.support:recyclerview-v7:' + propSupportLibraryVersion
    force 'com.android.support:design:' + propSupportLibraryVersion
    force 'com.android.support:cardview-v7:' + propSupportLibraryVersion
    force 'com.android.support:preference-v7:' + propSupportLibraryVersion
    force 'com.android.support:preference-v14:' + propSupportLibraryVersion
    force 'com.android.support:customtabs:' + propSupportLibraryVersion
    force 'com.android.support:support-core-utils:' + propSupportLibraryVersion
    force 'com.android.support:support-annotations:' + propSupportLibraryVersion
    force 'com.android.support:support-compat:' + propSupportLibraryVersion
    force 'com.android.support:support-fragment:' + propSupportLibraryVersion

    force 'com.google.android.gms:play-services-location:17.0.0'
    force 'com.google.android.gms:play-services-analytics:17.0.0'
    force 'com.google.android.gms:play-services-plus:17.0.0'
    force 'com.google.android.gms:play-services-gcm:17.0.0'
    force 'com.google.android.gms:play-services-auth:17.0.0'
    force 'com.google.android.gms:play-services-basement:17.1.1'
    force 'com.google.android.gms:play-services-base:17.0.0'

    force 'android.arch.core:runtime:1.1.1'
    force 'android.arch.core:common:1.1.1'

    failOnVersionConflict();
  }
}

android {
  // All properties are read from gradle.properties file
  compileSdkVersion propCompileSdkVersion.toInteger()

  defaultConfig {
    // Default package name is taken from the manifest and should be com.mapswithme.maps
    versionCode propVersionCode.toInteger()
    versionName propVersionName
    minSdkVersion propMinSdkVersion.toInteger()
    targetSdkVersion propTargetSdkVersion.toInteger()
    applicationId project.ext.appId
    buildConfigField 'String', 'SUPPORT_MAIL', '"android@maps.me"'
    buildConfigField 'String', 'REVIEW_URL', '"market://details?id=com.mapswithme.maps.pro"'
    buildConfigField 'int', 'RATING_THRESHOLD', '5'

    // Crashlytics API key
    Properties props = new Properties()
    props.load(new FileInputStream("${projectDir}/fabric.properties"));
    manifestPlaceholders = ['FABRIC_API_KEY': props['apiKey']]
    buildConfigField 'String', 'FABRIC_API_KEY', /"${props['apiKey']}"/

    // PushWoosh keys
    Properties pwProps = new Properties()
    pwProps.load(new FileInputStream("${projectDir}/pushwoosh.properties"));
    manifestPlaceholders += ['PW_APPID': pwProps['pwAppId']]
    buildConfigField 'String', 'PW_APPID', /"${pwProps['pwAppId']}"/
    def pwProjectIdKey = 'PW_PROJECT_ID'
    manifestPlaceholders += [pwProjectId : pwProps['pwProjectId']]
    manifestPlaceholders += ['PW_PROJECT_ID_KEY' : pwProjectIdKey]
    resValue 'string', 'pw_project_id', pwProps['pwProjectId']
    buildConfigField "String", 'PW_PROJECT_ID',  "\"" + pwProjectIdKey + "\""

    multiDexEnabled true
    multiDexKeepFile file('multidex-config.txt')

    externalNativeBuild {
      def pchFlag = 'OFF'
      if (project.hasProperty('pch')) pchFlag = 'ON'

      cmake {
        cppFlags '-fexceptions', '-frtti'
        cFlags '-ffunction-sections', '-fdata-sections',
               '-Wno-extern-c-compat'
        arguments '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=c++_static',
                  "-DOS=$osName", '-DSKIP_TESTS=ON', "-DUSE_PCH=$pchFlag"
      }
    }

    ndk {
      abiFilters = new HashSet<>()
      if (project.hasProperty("arm32")) {
        println("Building for the ARM32 architecture")
        abiFilters.add("armeabi-v7a")
      } else if (project.hasProperty("arm64")) {
        println("Building for the ARM64 architecture")
        abiFilters.add("arm64-v8a")
      } else if (project.hasProperty("x86_64")) {
        println("Building for the x86_64 architecture")
        abiFilters.add("x86_64")
      } else if (project.hasProperty("x86")) {
        println("Building for the x86 architecture")
        abiFilters.add("x86")
      } else {
        if (!project.hasProperty('splitApk')) {
          println("Building for the ARM and x86 architectures")
          abiFilters.add("x86")
          abiFilters.add("x86_64")
          abiFilters.add("armeabi-v7a")
          abiFilters.add("arm64-v8a")
        }
      }
    }
  }

  sourceSets.main {
    manifest.srcFile 'AndroidManifest.xml'
    res.srcDirs = ['res']
    java.srcDirs = ['src', '../3party/Alohalytics/src/android/java']
    // assets folder is auto-generated by tools/android/update_assets.sh, so we keep all static resources in separate folders.
    assets.srcDirs = ['assets']
    jniLibs.srcDirs = [android.getNdkDirectory().toString() + '/sources/third_party/vulkan/src/build-android/jniLibs']
  }

  flavorDimensions "default"

  productFlavors {
    final int HUAWEI_VERSION_CODE_BASE = 20000;
    final int TENCENT_VERSION_CODE_BASE = 10000;

    google {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-Google'
      buildConfigField 'String', 'SUPPORT_MAIL', '"googleplay@maps.me"'
    }

    samsung {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-Samsung'
      buildConfigField 'String', 'REVIEW_URL', '"samsungapps://ProductDetail/com.mapswithme.maps.pro"'
      buildConfigField 'String', 'SUPPORT_MAIL', '"samsung@maps.me"'
      android.sourceSets.samsung.assets.srcDirs = ['flavors/mwm-ttf-assets']
    }

    amazon {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-Amazon'
      buildConfigField 'String', 'REVIEW_URL', '"amzn://apps/android?p=com.mapswithme.maps.pro"'
      buildConfigField 'String', 'SUPPORT_MAIL', '"amazon@maps.me"'
      buildConfigField 'int', 'RATING_THRESHOLD', '4'
      android.sourceSets.amazon.assets.srcDirs = ['flavors/mwm-ttf-assets']
    }

    yandex {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-Yandex'
      buildConfigField 'String', 'REVIEW_URL', '"yastore://details?id=com.mapswithme.maps.pro"'
      android.sourceSets.yandex.assets.srcDirs = ['flavors/mwm-ttf-assets']
    }

    preinstall {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-Preinstall'
      buildConfigField 'String', 'SUPPORT_MAIL', '"googleplay@maps.me"'
    }

    cipherlab {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-CipherLab'
      android.sourceSets.cipherlab.assets.srcDirs = ['flavors/mwm-ttf-assets']
    }

    xiaomi {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-Xiaomi'
      buildConfigField 'String', 'REVIEW_URL', '"http://app.mi.com/detail/85835?ref=search"'
      android.sourceSets.xiaomi.assets.srcDirs = ['flavors/mwm-ttf-assets']
    }

    lenovo {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-Lenovo'
      android.sourceSets.lenovo.assets.srcDirs = ['flavors/mwm-ttf-assets']
    }

    web {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-Web'
      buildConfigField 'String', 'SUPPORT_MAIL', '"googleplay@maps.me"'
      android.sourceSets.web.assets.srcDirs = ['flavors/mwm-ttf-assets']
    }

    nineApp {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-9app'
      buildConfigField 'String', 'REVIEW_URL', '"http://www.9apps.com/android-apps/mapswithme-maps-pro/"'
      android.sourceSets.nineApp.assets.srcDirs = ['flavors/mwm-ttf-assets']
    }

    mobogenie {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-Mobogenie'
      android.sourceSets.mobogenie.assets.srcDirs = ['flavors/mwm-ttf-assets']
      buildConfigField 'String', 'REVIEW_URL', '"http://www.mobogenie.com/download-maps-me-4240212.html"'
    }

    tencent {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-Tencent'
      versionCode TENCENT_VERSION_CODE_BASE + android.defaultConfig.versionCode
      android.sourceSets.tencent.assets.srcDirs = ['flavors/mwm-ttf-assets']
      buildConfigField 'String', 'REVIEW_URL', '"http://android.myapp.com/myapp/detail.htm?apkName=com.mapswithme.maps.pro"'
    }

    baidu {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-Baidu'
      android.sourceSets.baidu.assets.srcDirs = ['flavors/mwm-ttf-assets']
      buildConfigField 'String', 'REVIEW_URL', '"http://shouji.baidu.com/software/item?docid=7999892&f=sug@software"'
    }

    appChina {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-AppChina'
      android.sourceSets.appChina.assets.srcDirs = ['flavors/mwm-ttf-assets']
      buildConfigField 'String', 'REVIEW_URL', '"http://www.appchina.com/app/com.mapswithme.maps.pro"'
    }

    moboMarket {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-MoboMarket'
      android.sourceSets.moboMarket.assets.srcDirs = ['flavors/mwm-ttf-assets']
      // TODO While store url is unknown, force all rating reviews to be sent to our email.
      // Change it in the next version after publishing app to the store.
      buildConfigField 'int', 'RATING_THRESHOLD', '6'
    }

    blackberry {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-Blackberry'
      android.sourceSets.blackberry.assets.srcDirs = ['flavors/mwm-ttf-assets']
      buildConfigField 'String', 'REVIEW_URL', '"https://appworld.blackberry.com/webstore/content/51013892"'
    }

    huawei {
      dimension "default"
      versionName = android.defaultConfig.versionName + '-Huawei'
      versionCode = HUAWEI_VERSION_CODE_BASE + android.defaultConfig.versionCode
      buildConfigField 'String', 'SUPPORT_MAIL', '"huawei@maps.me"'
      android.sourceSets.huawei.assets.srcDirs = ['flavors/mwm-ttf-assets']
    }
  }

  variantFilter { variant ->
    if (!variant.getFlavors().get(0).name.equals("web")
            && !variant.getFlavors().get(0).name.equals("google")
            && !project.hasProperty("anyVariant")) {
      println("Variant '" + variant.name + "' is ignored. If you want to build it please " +
              "add gradle parameter '-PanyVariant'")
      variant.setIgnore(true)
    }
  }

  splits.abi {
    boolean enabled = project.hasProperty('splitApk');
    println ("Create separate apks: " + enabled)
    enable enabled
    reset()
    include 'x86', 'armeabi-v7a', 'arm64-v8a', 'x86_64'
    universalApk true
  }

  // TODO: Fix a lot of lint errors in our code
  lintOptions {
    abortOnError false
  }

  gradle.projectsEvaluated {
    android.applicationVariants.all { variant ->
      def task = variant.name.capitalize()
      project.task(type: Exec, "run${task}", dependsOn: "install${task}") {
        commandLine android.getAdbExe(), 'shell', 'am', 'start', '-n', "${applicationId}/com.mapswithme.maps.SplashActivity"
      }

      variant.outputs.all { output ->
        if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
          def file = output.outputFile
          // version at the end of each built apk
          outputFileName = "../../" + file.name.replace(".apk", "-" + android.defaultConfig.versionName + "-" + getDate() + ".apk")
          // set different versionCodes for different ABIs
          int abiVersionCode = project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI)) ?: 0
          output.versionCodeOverride = (abiVersionCode * 1000) + variant.versionCode
        }
      }
    }
  }

  signingConfigs {
    mapswithme {
      storeFile file(spropStoreFile)
      storePassword spropStorePassword
      keyAlias spropKeyAlias
      keyPassword spropKeyPassword
    }

    mapswithmeDebug {
      storeFile file('../tools/android/debug.keystore')
      storePassword '12345678'
      keyAlias 'debug'
      keyPassword '12345678'
    }
  }

  buildTypes {
    debug {
      applicationIdSuffix '.debug'   // Allows to install debug and release builds together
      versionNameSuffix '-debug'
      jniDebuggable true           // Enable jni debug build
      zipAlignEnabled true
      signingConfig signingConfigs.mapswithmeDebug
      android.sourceSets.debug.setRoot('flavors/debug')
      resValue 'string', 'app_id', android.defaultConfig.applicationId + applicationIdSuffix
    }

    release {
      signingConfig signingConfigs.mapswithme
      minifyEnabled true
      shrinkResources true
      proguardFile('proguard-mwm.txt')
      resValue 'string', 'app_id', android.defaultConfig.applicationId
    }

    beta {
      applicationIdSuffix '.beta'
      versionNameSuffix '-beta'
      signingConfig signingConfigs.mapswithme
      android.sourceSets.beta.setRoot('flavors/beta')
      matchingFallbacks = ['debug', 'release']
      resValue 'string', 'app_id', android.defaultConfig.applicationId + applicationIdSuffix
    }
  }

  externalNativeBuild {
    cmake {
      buildStagingDirectory "./nativeOutputs"
      path "../CMakeLists.txt"
    }
  }

  // We don't compress these extensions in assets/ because our random FileReader can't read zip-compressed files from apk.
  // TODO: Load all minor files via separate call to ReadAsString which can correctly handle compressed files in zip containers.
  aaptOptions {
    noCompress 'txt', 'bin', 'html', 'png', 'json', 'mwm', 'ttf', 'sdf', 'ui', 'config', 'csv', 'spv'
    ignoreAssetsPattern "!.svn:!.git:!.DS_Store:!*.scc:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"
  }

  // Enables new Java features in KitKat+ but needs Java 1.7 installed
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

  packagingOptions {
    exclude 'lib/armeabi/libcrashlytics-envelope.so'
    exclude 'lib/armeabi/libcrashlytics.so'
    exclude 'lib/mips64/libcrashlytics-envelope.so'
    exclude 'lib/mips64/libcrashlytics.so'
    exclude 'lib/mips/libcrashlytics-envelope.so'
    exclude 'lib/mips/libcrashlytics.so'
    exclude 'lib/armeabi/libVkLayer_core_validation.so'
    exclude 'lib/armeabi/libVkLayer_threading.so'
    exclude 'lib/armeabi/libVkLayer_image.so'
    exclude 'lib/armeabi/libVkLayer_parameter_validation.so'
    exclude 'lib/armeabi/libVkLayer_object_tracker.so'
    exclude 'lib/armeabi/libVkLayer_swapchain.so'
    exclude 'lib/armeabi/libVkLayer_unique_objects.so'
    exclude 'lib/mips64/libVkLayer_core_validation.so'
    exclude 'lib/mips64/libVkLayer_threading.so'
    exclude 'lib/mips64/libVkLayer_image.so'
    exclude 'lib/mips64/libVkLayer_parameter_validation.so'
    exclude 'lib/mips64/libVkLayer_object_tracker.so'
    exclude 'lib/mips64/libVkLayer_swapchain.so'
    exclude 'lib/mips64/libVkLayer_unique_objects.so'
    exclude 'lib/mips/libVkLayer_core_validation.so'
    exclude 'lib/mips/libVkLayer_threading.so'
    exclude 'lib/mips/libVkLayer_image.so'
    exclude 'lib/mips/libVkLayer_parameter_validation.so'
    exclude 'lib/mips/libVkLayer_object_tracker.so'
    exclude 'lib/mips/libVkLayer_swapchain.so'
    exclude 'lib/mips/libVkLayer_unique_objects.so'
  }
}

// Tasks needed to compile NDK part
apply plugin: 'base'

project.ext.PARAM_PARALLEL_TASK_COUNT = '-j' + (Runtime.runtime.availableProcessors() + 1)
project.ext.NDK_BUILD = android.getNdkDirectory().toString() + '/ndk-build'
if (System.properties['os.name'].toLowerCase().contains('windows'))
  project.ext.NDK_BUILD += ".cmd"

// Tasks for generating obb files for Google Play
def unalignedFonts = "${propObbFontsOutput}.unaligned"
def unalignedWorlds = "${propObbWorldsOutput}.unaligned"

task obbClean(type: Delete) {
  doLast {
    [propObbFontsOutput, propObbWorldsOutput, unalignedFonts, unalignedWorlds].each { file ->
      delete file
    }
  }
}

task obbGenerate() {
  createObbGenerateTask('Main', propObbFonts.split(' '), unalignedFonts)
  createObbGenerateTask('Patch', propObbWorlds.split(' '), unalignedWorlds)
  createObbAlignTask('Main', unalignedFonts, propObbFontsOutput)
  createObbAlignTask('Patch', unalignedWorlds, propObbWorldsOutput)
}

obbGenerate.dependsOn obbClean, obbMainGenerate, obbPatchGenerate, obbMainAlign, obbPatchAlign

def createObbGenerateTask(type, data, name) {
  return tasks.create(name: "obb${type}Generate", type: Exec, description: 'Generate obb files') {
    commandLine((['zip', '-0', '-j', name, data]).flatten())
  }
}

def createObbAlignTask(type, rawObb, alignedObb) {
  def sdkDir = "${android.getSdkDirectory().getAbsolutePath()}"
  def zipalignPath = sdkDir + File.separator + "build-tools" + File.separator +
          propBuildToolsVersion + File.separator + "zipalign";

  return tasks.create(name: "obb${type}Align", dependsOn: "obb${type}Generate", type: Exec, description: 'Align obb files') {
    commandLine zipalignPath, '-v', '8', rawObb, alignedObb
  }
}

task obbPush(dependsOn: ['obbGenerate', 'obbPushMain', 'obbPushPatch']) {
  def obbPath = "/mnt/sdcard/Android/obb/${project.ext.appId}/"
  tasks.create(type: Exec, name: 'obbRemoveOnDevice') {
    commandLine android.getAdbExe(), 'shell', 'rm', "${obbPath}*.obb"
  }
  tasks.create(type: Exec, name: 'obbPushMain', dependsOn: 'obbRemoveOnDevice') {
    commandLine android.getAdbExe(), 'push', propObbFontsOutput, "${obbPath}fonts.obb"
  }
  tasks.create(type: Exec, name: 'obbPushPatch', dependsOn: 'obbRemoveOnDevice') {
    commandLine android.getAdbExe(), 'push', propObbWorldsOutput, "${obbPath}worlds.obb"
  }
}

android.buildTypes.all { buildType ->
  def suffix = applicationIdSuffix != null ? applicationIdSuffix : "";
  def authorityValue = android.defaultConfig.applicationId + suffix + ".provider"
  def authority = "\"" + authorityValue + "\""
  buildConfigField 'String', 'FILE_PROVIDER_AUTHORITY', authority
  manifestPlaceholders += [FILE_PROVIDER_PLACEHOLDER : authorityValue]
}