No Description

build.gradle 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. apply plugin: 'com.android.application'
  18. if (cdvHelpers.getConfigPreference('GradlePluginKotlinEnabled', 'false').toBoolean()) {
  19. apply plugin: 'kotlin-android'
  20. apply plugin: 'kotlin-android-extensions'
  21. }
  22. buildscript {
  23. apply from: '../CordovaLib/cordova.gradle'
  24. if(cdvHelpers.getConfigPreference('GradlePluginKotlinEnabled', 'false').toBoolean()) {
  25. String defaultGradlePluginKotlinVersion = kotlin_version
  26. /**
  27. * Fetches the user's defined Kotlin Version from config.xml.
  28. * If the version is not set or invalid, it will default to the ${defaultGradlePluginKotlinVersion}
  29. */
  30. String gradlePluginKotlinVersion = cdvHelpers.getConfigPreference('GradlePluginKotlinVersion', defaultGradlePluginKotlinVersion)
  31. if(!cdvHelpers.isVersionValid(gradlePluginKotlinVersion)) {
  32. println("The defined Kotlin version (${gradlePluginKotlinVersion}) does not appear to be a valid version. Falling back to version: ${defaultGradlePluginKotlinVersion}.")
  33. gradlePluginKotlinVersion = defaultGradlePluginKotlinVersion
  34. }
  35. // Change the version to be used.
  36. ext.kotlin_version = gradlePluginKotlinVersion
  37. }
  38. apply from: 'repositories.gradle'
  39. repositories repos
  40. dependencies {
  41. apply from: '../CordovaLib/cordova.gradle'
  42. classpath 'com.android.tools.build:gradle:4.0.0'
  43. if (cdvHelpers.getConfigPreference('GradlePluginKotlinEnabled', 'false').toBoolean()) {
  44. classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  45. }
  46. if(cdvHelpers.getConfigPreference('GradlePluginGoogleServicesEnabled', 'false').toBoolean()) {
  47. String defaultGradlePluginGoogleServicesVersion = '4.2.0'
  48. /**
  49. * Fetches the user's defined Google Services Plugin Version from config.xml.
  50. * If the version is not set or invalid, it will default to the ${defaultGradlePluginGoogleServicesVersion}
  51. */
  52. String gradlePluginGoogleServicesVersion = cdvHelpers.getConfigPreference('GradlePluginGoogleServicesVersion', defaultGradlePluginGoogleServicesVersion)
  53. if(!cdvHelpers.isVersionValid(gradlePluginGoogleServicesVersion)) {
  54. println("The defined Google Services plugin version (${gradlePluginGoogleServicesVersion}) does not appear to be a valid version. Falling back to version: ${defaultGradlePluginGoogleServicesVersion}.")
  55. gradlePluginGoogleServicesVersion = defaultGradlePluginGoogleServicesVersion
  56. }
  57. // Create the Google Services classpath and set it.
  58. String gradlePluginGoogleServicesClassPath = "com.google.gms:google-services:${gradlePluginGoogleServicesVersion}"
  59. println "Adding classpath: ${gradlePluginGoogleServicesClassPath}"
  60. classpath gradlePluginGoogleServicesClassPath
  61. }
  62. }
  63. }
  64. // Allow plugins to declare Maven dependencies via build-extras.gradle.
  65. allprojects {
  66. apply from: 'repositories.gradle'
  67. repositories repos
  68. }
  69. task wrapper(type: Wrapper) {
  70. gradleVersion = '6.5'
  71. }
  72. // Configuration properties. Set these via environment variables, build-extras.gradle, or gradle.properties.
  73. // Refer to: http://www.gradle.org/docs/current/userguide/tutorial_this_and_that.html
  74. ext {
  75. apply from: '../CordovaLib/cordova.gradle'
  76. // The value for android.compileSdkVersion.
  77. if (!project.hasProperty('cdvCompileSdkVersion')) {
  78. cdvCompileSdkVersion = null;
  79. }
  80. // The value for android.buildToolsVersion.
  81. if (!project.hasProperty('cdvBuildToolsVersion')) {
  82. cdvBuildToolsVersion = null;
  83. }
  84. // Sets the versionCode to the given value.
  85. if (!project.hasProperty('cdvVersionCode')) {
  86. cdvVersionCode = null
  87. }
  88. // Sets the minSdkVersion to the given value.
  89. if (!project.hasProperty('cdvMinSdkVersion')) {
  90. cdvMinSdkVersion = null
  91. }
  92. // Sets the maxSdkVersion to the given value.
  93. if (!project.hasProperty('cdvMaxSdkVersion')) {
  94. cdvMaxSdkVersion = null
  95. }
  96. // The value for android.targetSdkVersion.
  97. if (!project.hasProperty('cdvTargetSdkVersion')) {
  98. cdvTargetSdkVersion = null;
  99. }
  100. // Whether to build architecture-specific APKs.
  101. if (!project.hasProperty('cdvBuildMultipleApks')) {
  102. cdvBuildMultipleApks = null
  103. }
  104. // Whether to append a 0 "abi digit" to versionCode when only a single APK is build
  105. if (!project.hasProperty('cdvVersionCodeForceAbiDigit')) {
  106. cdvVersionCodeForceAbiDigit = null
  107. }
  108. // .properties files to use for release signing.
  109. if (!project.hasProperty('cdvReleaseSigningPropertiesFile')) {
  110. cdvReleaseSigningPropertiesFile = null
  111. }
  112. // .properties files to use for debug signing.
  113. if (!project.hasProperty('cdvDebugSigningPropertiesFile')) {
  114. cdvDebugSigningPropertiesFile = null
  115. }
  116. // Set by build.js script.
  117. if (!project.hasProperty('cdvBuildArch')) {
  118. cdvBuildArch = null
  119. }
  120. // Plugin gradle extensions can append to this to have code run at the end.
  121. cdvPluginPostBuildExtras = []
  122. }
  123. // PLUGIN GRADLE EXTENSIONS START
  124. // PLUGIN GRADLE EXTENSIONS END
  125. def hasBuildExtras1 = file('build-extras.gradle').exists()
  126. if (hasBuildExtras1) {
  127. apply from: 'build-extras.gradle'
  128. }
  129. def hasBuildExtras2 = file('../build-extras.gradle').exists()
  130. if (hasBuildExtras2) {
  131. apply from: '../build-extras.gradle'
  132. }
  133. // Set property defaults after extension .gradle files.
  134. ext.cdvCompileSdkVersion = cdvCompileSdkVersion == null ? (
  135. defaultCompileSdkVersion == null
  136. ? privateHelpers.getProjectTarget()
  137. : defaultCompileSdkVersion
  138. ) : Integer.parseInt('' + cdvCompileSdkVersion);
  139. if (ext.cdvBuildToolsVersion == null) {
  140. ext.cdvBuildToolsVersion = privateHelpers.findLatestInstalledBuildTools()
  141. //ext.cdvBuildToolsVersion = project.ext.defaultBuildToolsVersion
  142. }
  143. if (ext.cdvDebugSigningPropertiesFile == null && file('../debug-signing.properties').exists()) {
  144. ext.cdvDebugSigningPropertiesFile = '../debug-signing.properties'
  145. }
  146. if (ext.cdvReleaseSigningPropertiesFile == null && file('../release-signing.properties').exists()) {
  147. ext.cdvReleaseSigningPropertiesFile = '../release-signing.properties'
  148. }
  149. // Cast to appropriate types.
  150. ext.cdvBuildMultipleApks = cdvBuildMultipleApks == null ? false : cdvBuildMultipleApks.toBoolean();
  151. ext.cdvVersionCodeForceAbiDigit = cdvVersionCodeForceAbiDigit == null ? false : cdvVersionCodeForceAbiDigit.toBoolean();
  152. // minSdkVersion, maxSdkVersion and targetSdkVersion
  153. ext.cdvMinSdkVersion = cdvMinSdkVersion == null ? defaultMinSdkVersion : Integer.parseInt('' + cdvMinSdkVersion)
  154. if (cdvMaxSdkVersion != null) {
  155. ext.cdvMaxSdkVersion = Integer.parseInt('' + cdvMaxSdkVersion)
  156. }
  157. ext.cdvTargetSdkVersion = cdvTargetSdkVersion == null ? defaultTargetSdkVersion : Integer.parseInt('' + cdvTargetSdkVersion)
  158. ext.cdvVersionCode = cdvVersionCode == null ? null : Integer.parseInt('' + cdvVersionCode)
  159. def computeBuildTargetName(debugBuild) {
  160. def ret = 'assemble'
  161. if (cdvBuildMultipleApks && cdvBuildArch) {
  162. def arch = cdvBuildArch == 'arm' ? 'armv7' : cdvBuildArch
  163. ret += '' + arch.toUpperCase().charAt(0) + arch.substring(1);
  164. }
  165. return ret + (debugBuild ? 'Debug' : 'Release')
  166. }
  167. // Make cdvBuild a task that depends on the debug/arch-sepecific task.
  168. task cdvBuildDebug
  169. cdvBuildDebug.dependsOn {
  170. return computeBuildTargetName(true)
  171. }
  172. task cdvBuildRelease
  173. cdvBuildRelease.dependsOn {
  174. return computeBuildTargetName(false)
  175. }
  176. task cdvPrintProps {
  177. doLast {
  178. println('cdvCompileSdkVersion=' + cdvCompileSdkVersion)
  179. println('cdvBuildToolsVersion=' + cdvBuildToolsVersion)
  180. println('cdvVersionCode=' + cdvVersionCode)
  181. println('cdvVersionCodeForceAbiDigit=' + cdvVersionCodeForceAbiDigit)
  182. println('cdvMinSdkVersion=' + cdvMinSdkVersion)
  183. println('cdvMaxSdkVersion=' + cdvMaxSdkVersion)
  184. println('cdvTargetSdkVersion=' + cdvTargetSdkVersion)
  185. println('cdvBuildMultipleApks=' + cdvBuildMultipleApks)
  186. println('cdvReleaseSigningPropertiesFile=' + cdvReleaseSigningPropertiesFile)
  187. println('cdvDebugSigningPropertiesFile=' + cdvDebugSigningPropertiesFile)
  188. println('cdvBuildArch=' + cdvBuildArch)
  189. println('computedVersionCode=' + android.defaultConfig.versionCode)
  190. android.productFlavors.each { flavor ->
  191. println('computed' + flavor.name.capitalize() + 'VersionCode=' + flavor.versionCode)
  192. }
  193. }
  194. }
  195. android {
  196. defaultConfig {
  197. versionCode cdvVersionCode ?: new BigInteger("" + privateHelpers.extractIntFromManifest("versionCode"))
  198. applicationId privateHelpers.extractStringFromManifest("package")
  199. if (cdvMinSdkVersion != null) {
  200. minSdkVersion cdvMinSdkVersion
  201. }
  202. if (cdvMaxSdkVersion != null) {
  203. maxSdkVersion cdvMaxSdkVersion
  204. }
  205. if(cdvTargetSdkVersion != null) {
  206. targetSdkVersion cdvTargetSdkVersion
  207. }
  208. }
  209. lintOptions {
  210. abortOnError false;
  211. }
  212. compileSdkVersion cdvCompileSdkVersion
  213. buildToolsVersion cdvBuildToolsVersion
  214. // This code exists for Crosswalk and other Native APIs.
  215. // By default, we multiply the existing version code in the
  216. // Android Manifest by 10 and add a number for each architecture.
  217. // If you are not using Crosswalk or SQLite, you can
  218. // ignore this chunk of code, and your version codes will be respected.
  219. if (Boolean.valueOf(cdvBuildMultipleApks)) {
  220. flavorDimensions "default"
  221. productFlavors {
  222. armeabi {
  223. versionCode defaultConfig.versionCode*10 + 1
  224. ndk {
  225. abiFilters = ["armeabi"]
  226. }
  227. }
  228. armv7 {
  229. versionCode defaultConfig.versionCode*10 + 2
  230. ndk {
  231. abiFilters = ["armeabi-v7a"]
  232. }
  233. }
  234. arm64 {
  235. versionCode defaultConfig.versionCode*10 + 3
  236. ndk {
  237. abiFilters = ["arm64-v8a"]
  238. }
  239. }
  240. x86 {
  241. versionCode defaultConfig.versionCode*10 + 4
  242. ndk {
  243. abiFilters = ["x86"]
  244. }
  245. }
  246. x86_64 {
  247. versionCode defaultConfig.versionCode*10 + 5
  248. ndk {
  249. abiFilters = ["x86_64"]
  250. }
  251. }
  252. }
  253. } else if (Boolean.valueOf(cdvVersionCodeForceAbiDigit)) {
  254. // This provides compatibility to the default logic for versionCode before cordova-android 5.2.0
  255. defaultConfig {
  256. versionCode defaultConfig.versionCode*10
  257. }
  258. }
  259. compileOptions {
  260. sourceCompatibility JavaVersion.VERSION_1_8
  261. targetCompatibility JavaVersion.VERSION_1_8
  262. }
  263. if (cdvReleaseSigningPropertiesFile) {
  264. signingConfigs {
  265. release {
  266. // These must be set or Gradle will complain (even if they are overridden).
  267. keyAlias = ""
  268. keyPassword = "__unset" // And these must be set to non-empty in order to have the signing step added to the task graph.
  269. storeFile = null
  270. storePassword = "__unset"
  271. }
  272. }
  273. buildTypes {
  274. release {
  275. signingConfig signingConfigs.release
  276. }
  277. }
  278. addSigningProps(cdvReleaseSigningPropertiesFile, signingConfigs.release)
  279. }
  280. if (cdvDebugSigningPropertiesFile) {
  281. addSigningProps(cdvDebugSigningPropertiesFile, signingConfigs.debug)
  282. }
  283. sourceSets {
  284. main.java.srcDirs += 'src/main/kotlin'
  285. }
  286. }
  287. /*
  288. * WARNING: Cordova Lib and platform scripts do management inside of this code here,
  289. * if you are adding the dependencies manually, do so outside the comments, otherwise
  290. * the Cordova tools will overwrite them
  291. */
  292. dependencies {
  293. implementation fileTree(dir: 'libs', include: '*.jar')
  294. if (cdvHelpers.getConfigPreference('GradlePluginKotlinEnabled', 'false').toBoolean()) {
  295. implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
  296. }
  297. // SUB-PROJECT DEPENDENCIES START
  298. implementation(project(path: ":CordovaLib"))
  299. // SUB-PROJECT DEPENDENCIES END
  300. }
  301. def promptForReleaseKeyPassword() {
  302. if (!cdvReleaseSigningPropertiesFile) {
  303. return;
  304. }
  305. if ('__unset'.equals(android.signingConfigs.release.storePassword)) {
  306. android.signingConfigs.release.storePassword = privateHelpers.promptForPassword('Enter key store password: ')
  307. }
  308. if ('__unset'.equals(android.signingConfigs.release.keyPassword)) {
  309. android.signingConfigs.release.keyPassword = privateHelpers.promptForPassword('Enter key password: ');
  310. }
  311. }
  312. gradle.taskGraph.whenReady { taskGraph ->
  313. taskGraph.getAllTasks().each() { task ->
  314. if(['validateReleaseSigning', 'validateSigningRelease', 'validateSigningArmv7Release', 'validateSigningX76Release'].contains(task.name)) {
  315. promptForReleaseKeyPassword()
  316. }
  317. }
  318. }
  319. def addSigningProps(propsFilePath, signingConfig) {
  320. def propsFile = file(propsFilePath)
  321. def props = new Properties()
  322. propsFile.withReader { reader ->
  323. props.load(reader)
  324. }
  325. def storeFile = new File(props.get('key.store') ?: privateHelpers.ensureValueExists(propsFilePath, props, 'storeFile'))
  326. if (!storeFile.isAbsolute()) {
  327. storeFile = RelativePath.parse(true, storeFile.toString()).getFile(propsFile.getParentFile())
  328. }
  329. if (!storeFile.exists()) {
  330. throw new FileNotFoundException('Keystore file does not exist: ' + storeFile.getAbsolutePath())
  331. }
  332. signingConfig.keyAlias = props.get('key.alias') ?: privateHelpers.ensureValueExists(propsFilePath, props, 'keyAlias')
  333. signingConfig.keyPassword = props.get('keyPassword', props.get('key.alias.password', signingConfig.keyPassword))
  334. signingConfig.storeFile = storeFile
  335. signingConfig.storePassword = props.get('storePassword', props.get('key.store.password', signingConfig.storePassword))
  336. def storeType = props.get('storeType', props.get('key.store.type', ''))
  337. if (!storeType) {
  338. def filename = storeFile.getName().toLowerCase();
  339. if (filename.endsWith('.p12') || filename.endsWith('.pfx')) {
  340. storeType = 'pkcs12'
  341. } else {
  342. storeType = signingConfig.storeType // "jks"
  343. }
  344. }
  345. signingConfig.storeType = storeType
  346. }
  347. for (def func : cdvPluginPostBuildExtras) {
  348. func()
  349. }
  350. // This can be defined within build-extras.gradle as:
  351. // ext.postBuildExtras = { ... code here ... }
  352. if (hasProperty('postBuildExtras')) {
  353. postBuildExtras()
  354. }
  355. if (cdvHelpers.getConfigPreference('GradlePluginGoogleServicesEnabled', 'false').toBoolean()) {
  356. apply plugin: 'com.google.gms.google-services'
  357. }