cordova.gradle 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. import java.util.regex.Pattern
  18. import groovy.swing.SwingBuilder
  19. String doEnsureValueExists(filePath, props, key) {
  20. if (props.get(key) == null) {
  21. throw new GradleException(filePath + ': Missing key required "' + key + '"')
  22. }
  23. return props.get(key)
  24. }
  25. String doGetProjectTarget() {
  26. def props = new Properties()
  27. def propertiesFile = 'project.properties';
  28. if(!(file(propertiesFile).exists())) {
  29. propertiesFile = '../project.properties';
  30. }
  31. file(propertiesFile).withReader { reader ->
  32. props.load(reader)
  33. }
  34. return doEnsureValueExists('project.properties', props, 'target')
  35. }
  36. String[] getAvailableBuildTools() {
  37. def buildToolsDir = new File(getAndroidSdkDir(), "build-tools")
  38. buildToolsDir.list()
  39. .findAll { it ==~ /[0-9.]+/ }
  40. .sort { a, b -> compareVersions(b, a) }
  41. }
  42. String doFindLatestInstalledBuildTools(String minBuildToolsVersion) {
  43. def availableBuildToolsVersions
  44. try {
  45. availableBuildToolsVersions = getAvailableBuildTools()
  46. } catch (e) {
  47. println "An exception occurred while trying to find the Android build tools."
  48. throw e
  49. }
  50. if (availableBuildToolsVersions.length > 0) {
  51. def highestBuildToolsVersion = availableBuildToolsVersions[0]
  52. if (compareVersions(highestBuildToolsVersion, minBuildToolsVersion) < 0) {
  53. throw new RuntimeException(
  54. "No usable Android build tools found. Highest installed version is " +
  55. highestBuildToolsVersion + "; minimum version required is " +
  56. minBuildToolsVersion + ".")
  57. }
  58. highestBuildToolsVersion
  59. } else {
  60. throw new RuntimeException(
  61. "No installed build tools found. Install the Android build tools version " +
  62. minBuildToolsVersion + " or higher.")
  63. }
  64. }
  65. // Return the first non-zero result of subtracting version list elements
  66. // pairwise. If they are all identical, return the difference in length of
  67. // the two lists.
  68. int compareVersionList(Collection aParts, Collection bParts) {
  69. def pairs = ([aParts, bParts]).transpose()
  70. pairs.findResult(aParts.size()-bParts.size()) {it[0] - it[1] != 0 ? it[0] - it[1] : null}
  71. }
  72. // Compare two version strings, such as "19.0.0" and "18.1.1.0". If all matched
  73. // elements are identical, the longer version is the largest by this method.
  74. // Examples:
  75. // "19.0.0" > "19"
  76. // "19.0.1" > "19.0.0"
  77. // "19.1.0" > "19.0.1"
  78. // "19" > "18.999.999"
  79. int compareVersions(String a, String b) {
  80. def aParts = a.tokenize('.').collect {it.toInteger()}
  81. def bParts = b.tokenize('.').collect {it.toInteger()}
  82. compareVersionList(aParts, bParts)
  83. }
  84. String getAndroidSdkDir() {
  85. def rootDir = project.rootDir
  86. def androidSdkDir = null
  87. String envVar = System.getenv("ANDROID_HOME")
  88. def localProperties = new File(rootDir, 'local.properties')
  89. String systemProperty = System.getProperty("android.home")
  90. if (envVar != null) {
  91. androidSdkDir = envVar
  92. } else if (localProperties.exists()) {
  93. Properties properties = new Properties()
  94. localProperties.withInputStream { instr ->
  95. properties.load(instr)
  96. }
  97. def sdkDirProp = properties.getProperty('sdk.dir')
  98. if (sdkDirProp != null) {
  99. androidSdkDir = sdkDirProp
  100. } else {
  101. sdkDirProp = properties.getProperty('android.dir')
  102. if (sdkDirProp != null) {
  103. androidSdkDir = (new File(rootDir, sdkDirProp)).getAbsolutePath()
  104. }
  105. }
  106. }
  107. if (androidSdkDir == null && systemProperty != null) {
  108. androidSdkDir = systemProperty
  109. }
  110. if (androidSdkDir == null) {
  111. throw new RuntimeException(
  112. "Unable to determine Android SDK directory.")
  113. }
  114. androidSdkDir
  115. }
  116. def doExtractIntFromManifest(name) {
  117. def manifestFile = file(android.sourceSets.main.manifest.srcFile)
  118. def pattern = Pattern.compile(name + "=\"(\\d+)\"")
  119. def matcher = pattern.matcher(manifestFile.getText())
  120. matcher.find()
  121. return new BigInteger(matcher.group(1))
  122. }
  123. def doExtractStringFromManifest(name) {
  124. def manifestFile = file(android.sourceSets.main.manifest.srcFile)
  125. def pattern = Pattern.compile(name + "=\"(\\S+)\"")
  126. def matcher = pattern.matcher(manifestFile.getText())
  127. matcher.find()
  128. return matcher.group(1)
  129. }
  130. def doPromptForPassword(msg) {
  131. if (System.console() == null) {
  132. def ret = null
  133. new SwingBuilder().edt {
  134. dialog(modal: true, title: 'Enter password', alwaysOnTop: true, resizable: false, locationRelativeTo: null, pack: true, show: true) {
  135. vbox {
  136. label(text: msg)
  137. def input = passwordField()
  138. button(defaultButton: true, text: 'OK', actionPerformed: {
  139. ret = input.password;
  140. dispose();
  141. })
  142. }
  143. }
  144. }
  145. if (!ret) {
  146. throw new GradleException('User canceled build')
  147. }
  148. return new String(ret)
  149. } else {
  150. return System.console().readPassword('\n' + msg);
  151. }
  152. }
  153. def doGetConfigXml() {
  154. def xml = file("src/main/res/xml/config.xml").getText()
  155. // Disable namespace awareness since Cordova doesn't use them properly
  156. return new XmlParser(false, false).parseText(xml)
  157. }
  158. def doGetConfigPreference(name, defaultValue) {
  159. name = name.toLowerCase()
  160. def root = doGetConfigXml()
  161. def ret = defaultValue
  162. root.preference.each { it ->
  163. def attrName = it.attribute("name")
  164. if (attrName && attrName.toLowerCase() == name) {
  165. ret = it.attribute("value")
  166. }
  167. }
  168. return ret
  169. }
  170. // Properties exported here are visible to all plugins.
  171. ext {
  172. // These helpers are shared, but are not guaranteed to be stable / unchanged.
  173. privateHelpers = {}
  174. privateHelpers.getProjectTarget = { doGetProjectTarget() }
  175. privateHelpers.findLatestInstalledBuildTools = { doFindLatestInstalledBuildTools('19.1.0') }
  176. privateHelpers.extractIntFromManifest = { name -> doExtractIntFromManifest(name) }
  177. privateHelpers.extractStringFromManifest = { name -> doExtractStringFromManifest(name) }
  178. privateHelpers.promptForPassword = { msg -> doPromptForPassword(msg) }
  179. privateHelpers.ensureValueExists = { filePath, props, key -> doEnsureValueExists(filePath, props, key) }
  180. // These helpers can be used by plugins / projects and will not change.
  181. cdvHelpers = {}
  182. // Returns a XmlParser for the config.xml. Added in 4.1.0.
  183. cdvHelpers.getConfigXml = { doGetConfigXml() }
  184. // Returns the value for the desired <preference>. Added in 4.1.0.
  185. cdvHelpers.getConfigPreference = { name, defaultValue -> doGetConfigPreference(name, defaultValue) }
  186. }