build.gradle 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. buildscript {
  19. repositories {
  20. mavenCentral()
  21. jcenter()
  22. maven {
  23. url "https://maven.google.com"
  24. }
  25. }
  26. dependencies {
  27. classpath 'com.android.tools.build:gradle:3.0.0'
  28. }
  29. }
  30. // Allow plugins to declare Maven dependencies via build-extras.gradle.
  31. allprojects {
  32. repositories {
  33. mavenCentral();
  34. jcenter()
  35. }
  36. }
  37. task wrapper(type: Wrapper) {
  38. gradleVersion = '4.1.0'
  39. }
  40. // Configuration properties. Set these via environment variables, build-extras.gradle, or gradle.properties.
  41. // Refer to: http://www.gradle.org/docs/current/userguide/tutorial_this_and_that.html
  42. ext {
  43. apply from: '../CordovaLib/cordova.gradle'
  44. // The value for android.compileSdkVersion.
  45. if (!project.hasProperty('cdvCompileSdkVersion')) {
  46. cdvCompileSdkVersion = null;
  47. }
  48. // The value for android.buildToolsVersion.
  49. if (!project.hasProperty('cdvBuildToolsVersion')) {
  50. cdvBuildToolsVersion = null;
  51. }
  52. // Sets the versionCode to the given value.
  53. if (!project.hasProperty('cdvVersionCode')) {
  54. cdvVersionCode = null
  55. }
  56. // Sets the minSdkVersion to the given value.
  57. if (!project.hasProperty('cdvMinSdkVersion')) {
  58. cdvMinSdkVersion = null
  59. }
  60. // Whether to build architecture-specific APKs.
  61. if (!project.hasProperty('cdvBuildMultipleApks')) {
  62. cdvBuildMultipleApks = null
  63. }
  64. // Whether to append a 0 "abi digit" to versionCode when only a single APK is build
  65. if (!project.hasProperty('cdvVersionCodeForceAbiDigit')) {
  66. cdvVersionCodeForceAbiDigit = null
  67. }
  68. // .properties files to use for release signing.
  69. if (!project.hasProperty('cdvReleaseSigningPropertiesFile')) {
  70. cdvReleaseSigningPropertiesFile = null
  71. }
  72. // .properties files to use for debug signing.
  73. if (!project.hasProperty('cdvDebugSigningPropertiesFile')) {
  74. cdvDebugSigningPropertiesFile = null
  75. }
  76. // Set by build.js script.
  77. if (!project.hasProperty('cdvBuildArch')) {
  78. cdvBuildArch = null
  79. }
  80. // Plugin gradle extensions can append to this to have code run at the end.
  81. cdvPluginPostBuildExtras = []
  82. }
  83. // PLUGIN GRADLE EXTENSIONS START
  84. // PLUGIN GRADLE EXTENSIONS END
  85. def hasBuildExtras = file('build-extras.gradle').exists()
  86. if (hasBuildExtras) {
  87. apply from: 'build-extras.gradle'
  88. }
  89. // Set property defaults after extension .gradle files.
  90. if (ext.cdvCompileSdkVersion == null) {
  91. ext.cdvCompileSdkVersion = privateHelpers.getProjectTarget()
  92. //ext.cdvCompileSdkVersion = project.ext.defaultCompileSdkVersion
  93. }
  94. if (ext.cdvBuildToolsVersion == null) {
  95. ext.cdvBuildToolsVersion = privateHelpers.findLatestInstalledBuildTools()
  96. //ext.cdvBuildToolsVersion = project.ext.defaultBuildToolsVersion
  97. }
  98. if (ext.cdvDebugSigningPropertiesFile == null && file('../debug-signing.properties').exists()) {
  99. ext.cdvDebugSigningPropertiesFile = '../debug-signing.properties'
  100. }
  101. if (ext.cdvReleaseSigningPropertiesFile == null && file('../release-signing.properties').exists()) {
  102. ext.cdvReleaseSigningPropertiesFile = '../release-signing.properties'
  103. }
  104. // Cast to appropriate types.
  105. ext.cdvBuildMultipleApks = cdvBuildMultipleApks == null ? false : cdvBuildMultipleApks.toBoolean();
  106. ext.cdvVersionCodeForceAbiDigit = cdvVersionCodeForceAbiDigit == null ? false : cdvVersionCodeForceAbiDigit.toBoolean();
  107. ext.cdvMinSdkVersion = cdvMinSdkVersion == null ? null : defaultMinSdkVersion
  108. ext.cdvVersionCode = cdvVersionCode == null ? null : Integer.parseInt('' + cdvVersionCode)
  109. def computeBuildTargetName(debugBuild) {
  110. def ret = 'assemble'
  111. if (cdvBuildMultipleApks && cdvBuildArch) {
  112. def arch = cdvBuildArch == 'arm' ? 'armv7' : cdvBuildArch
  113. ret += '' + arch.toUpperCase().charAt(0) + arch.substring(1);
  114. }
  115. return ret + (debugBuild ? 'Debug' : 'Release')
  116. }
  117. // Make cdvBuild a task that depends on the debug/arch-sepecific task.
  118. task cdvBuildDebug
  119. cdvBuildDebug.dependsOn {
  120. return computeBuildTargetName(true)
  121. }
  122. task cdvBuildRelease
  123. cdvBuildRelease.dependsOn {
  124. return computeBuildTargetName(false)
  125. }
  126. task cdvPrintProps << {
  127. println('cdvCompileSdkVersion=' + cdvCompileSdkVersion)
  128. println('cdvBuildToolsVersion=' + cdvBuildToolsVersion)
  129. println('cdvVersionCode=' + cdvVersionCode)
  130. println('cdvVersionCodeForceAbiDigit=' + cdvVersionCodeForceAbiDigit)
  131. println('cdvMinSdkVersion=' + cdvMinSdkVersion)
  132. println('cdvBuildMultipleApks=' + cdvBuildMultipleApks)
  133. println('cdvReleaseSigningPropertiesFile=' + cdvReleaseSigningPropertiesFile)
  134. println('cdvDebugSigningPropertiesFile=' + cdvDebugSigningPropertiesFile)
  135. println('cdvBuildArch=' + cdvBuildArch)
  136. println('computedVersionCode=' + android.defaultConfig.versionCode)
  137. android.productFlavors.each { flavor ->
  138. println('computed' + flavor.name.capitalize() + 'VersionCode=' + flavor.versionCode)
  139. }
  140. }
  141. android {
  142. defaultConfig {
  143. versionCode cdvVersionCode ?: new BigInteger("" + privateHelpers.extractIntFromManifest("versionCode"))
  144. applicationId privateHelpers.extractStringFromManifest("package")
  145. if (cdvMinSdkVersion != null) {
  146. minSdkVersion cdvMinSdkVersion
  147. }
  148. }
  149. lintOptions {
  150. abortOnError false;
  151. }
  152. compileSdkVersion cdvCompileSdkVersion
  153. buildToolsVersion cdvBuildToolsVersion
  154. //This code exists for Crosswalk and other Native APIs.
  155. //By default, we multiply the existing version code in the Android Manifest by 10 and
  156. //add a number for each architecture. If you are not using Crosswalk or SQLite, you can
  157. //ignore this chunk of code, and your version codes will be respected.
  158. if (Boolean.valueOf(cdvBuildMultipleApks)) {
  159. flavorDimensions "default"
  160. productFlavors {
  161. armeabi {
  162. versionCode defaultConfig.versionCode*10 + 1
  163. ndk {
  164. abiFilters = ["armeabi"]
  165. }
  166. }
  167. armv7 {
  168. versionCode defaultConfig.versionCode*10 + 2
  169. ndk {
  170. abiFilters = ["armeabi-v7a"]
  171. }
  172. }
  173. arm64 {
  174. versionCode defaultConfig.versionCode*10 + 3
  175. ndk {
  176. abiFilters = ["arm64-v8a"]
  177. }
  178. }
  179. x86 {
  180. versionCode defaultConfig.versionCode*10 + 4
  181. ndk {
  182. abiFilters = ["x86"]
  183. }
  184. }
  185. x86_64 {
  186. versionCode defaultConfig.versionCode*10 + 5
  187. ndk {
  188. abiFilters = ["x86_64"]
  189. }
  190. }
  191. }
  192. } else if (Boolean.valueOf(cdvVersionCodeForceAbiDigit)) {
  193. // This provides compatibility to the default logic for versionCode before cordova-android 5.2.0
  194. defaultConfig {
  195. versionCode defaultConfig.versionCode*10
  196. }
  197. }
  198. compileOptions {
  199. sourceCompatibility JavaVersion.VERSION_1_8
  200. targetCompatibility JavaVersion.VERSION_1_8
  201. }
  202. if (cdvReleaseSigningPropertiesFile) {
  203. signingConfigs {
  204. release {
  205. // These must be set or Gradle will complain (even if they are overridden).
  206. keyAlias = ""
  207. keyPassword = "__unset" // And these must be set to non-empty in order to have the signing step added to the task graph.
  208. storeFile = null
  209. storePassword = "__unset"
  210. }
  211. }
  212. buildTypes {
  213. release {
  214. signingConfig signingConfigs.release
  215. }
  216. }
  217. addSigningProps(cdvReleaseSigningPropertiesFile, signingConfigs.release)
  218. }
  219. if (cdvDebugSigningPropertiesFile) {
  220. addSigningProps(cdvDebugSigningPropertiesFile, signingConfigs.debug)
  221. }
  222. }
  223. /*
  224. * WARNING: Cordova Lib and platform scripts do management inside of this code here,
  225. * if you are adding the dependencies manually, do so outside the comments, otherwise
  226. * the Cordova tools will overwrite them
  227. */
  228. dependencies {
  229. implementation fileTree(dir: 'libs', include: '*.jar')
  230. // SUB-PROJECT DEPENDENCIES START
  231. implementation(project(path: ":CordovaLib"))
  232. // SUB-PROJECT DEPENDENCIES END
  233. }
  234. def promptForReleaseKeyPassword() {
  235. if (!cdvReleaseSigningPropertiesFile) {
  236. return;
  237. }
  238. if ('__unset'.equals(android.signingConfigs.release.storePassword)) {
  239. android.signingConfigs.release.storePassword = privateHelpers.promptForPassword('Enter key store password: ')
  240. }
  241. if ('__unset'.equals(android.signingConfigs.release.keyPassword)) {
  242. android.signingConfigs.release.keyPassword = privateHelpers.promptForPassword('Enter key password: ');
  243. }
  244. }
  245. gradle.taskGraph.whenReady { taskGraph ->
  246. taskGraph.getAllTasks().each() { task ->
  247. if(['validateReleaseSigning', 'validateSigningRelease', 'validateSigningArmv7Release', 'validateSigningX76Release'].contains(task.name)) {
  248. promptForReleaseKeyPassword()
  249. }
  250. }
  251. }
  252. def addSigningProps(propsFilePath, signingConfig) {
  253. def propsFile = file(propsFilePath)
  254. def props = new Properties()
  255. propsFile.withReader { reader ->
  256. props.load(reader)
  257. }
  258. def storeFile = new File(props.get('key.store') ?: privateHelpers.ensureValueExists(propsFilePath, props, 'storeFile'))
  259. if (!storeFile.isAbsolute()) {
  260. storeFile = RelativePath.parse(true, storeFile.toString()).getFile(propsFile.getParentFile())
  261. }
  262. if (!storeFile.exists()) {
  263. throw new FileNotFoundException('Keystore file does not exist: ' + storeFile.getAbsolutePath())
  264. }
  265. signingConfig.keyAlias = props.get('key.alias') ?: privateHelpers.ensureValueExists(propsFilePath, props, 'keyAlias')
  266. signingConfig.keyPassword = props.get('keyPassword', props.get('key.alias.password', signingConfig.keyPassword))
  267. signingConfig.storeFile = storeFile
  268. signingConfig.storePassword = props.get('storePassword', props.get('key.store.password', signingConfig.storePassword))
  269. def storeType = props.get('storeType', props.get('key.store.type', ''))
  270. if (!storeType) {
  271. def filename = storeFile.getName().toLowerCase();
  272. if (filename.endsWith('.p12') || filename.endsWith('.pfx')) {
  273. storeType = 'pkcs12'
  274. } else {
  275. storeType = signingConfig.storeType // "jks"
  276. }
  277. }
  278. signingConfig.storeType = storeType
  279. }
  280. for (def func : cdvPluginPostBuildExtras) {
  281. func()
  282. }
  283. // This can be defined within build-extras.gradle as:
  284. // ext.postBuildExtras = { ... code here ... }
  285. if (hasProperty('postBuildExtras')) {
  286. postBuildExtras()
  287. }