build.gradle 16 KB

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