You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

313 lines
11 KiB

8 years ago
  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. }
  23. // Switch the Android Gradle plugin version requirement depending on the
  24. // installed version of Gradle. This dependency is documented at
  25. // http://tools.android.com/tech-docs/new-build-system/version-compatibility
  26. // and https://issues.apache.org/jira/browse/CB-8143
  27. dependencies {
  28. classpath 'com.android.tools.build:gradle:2.1.0'
  29. }
  30. }
  31. // Allow plugins to declare Maven dependencies via build-extras.gradle.
  32. allprojects {
  33. repositories {
  34. mavenCentral();
  35. jcenter()
  36. }
  37. }
  38. task wrapper(type: Wrapper) {
  39. gradleVersion = '2.13'
  40. }
  41. // Configuration properties. Set these via environment variables, build-extras.gradle, or gradle.properties.
  42. // Refer to: http://www.gradle.org/docs/current/userguide/tutorial_this_and_that.html
  43. ext {
  44. apply from: 'CordovaLib/cordova.gradle'
  45. // The value for android.compileSdkVersion.
  46. if (!project.hasProperty('cdvCompileSdkVersion')) {
  47. cdvCompileSdkVersion = null;
  48. }
  49. // The value for android.buildToolsVersion.
  50. if (!project.hasProperty('cdvBuildToolsVersion')) {
  51. cdvBuildToolsVersion = null;
  52. }
  53. // Sets the versionCode to the given value.
  54. if (!project.hasProperty('cdvVersionCode')) {
  55. cdvVersionCode = null
  56. }
  57. // Sets the minSdkVersion to the given value.
  58. if (!project.hasProperty('cdvMinSdkVersion')) {
  59. cdvMinSdkVersion = null
  60. }
  61. // Whether to build architecture-specific APKs.
  62. if (!project.hasProperty('cdvBuildMultipleApks')) {
  63. cdvBuildMultipleApks = null
  64. }
  65. // .properties files to use for release signing.
  66. if (!project.hasProperty('cdvReleaseSigningPropertiesFile')) {
  67. cdvReleaseSigningPropertiesFile = null
  68. }
  69. // .properties files to use for debug signing.
  70. if (!project.hasProperty('cdvDebugSigningPropertiesFile')) {
  71. cdvDebugSigningPropertiesFile = null
  72. }
  73. // Set by build.js script.
  74. if (!project.hasProperty('cdvBuildArch')) {
  75. cdvBuildArch = null
  76. }
  77. // Plugin gradle extensions can append to this to have code run at the end.
  78. cdvPluginPostBuildExtras = []
  79. }
  80. // PLUGIN GRADLE EXTENSIONS START
  81. // PLUGIN GRADLE EXTENSIONS END
  82. def hasBuildExtras = file('build-extras.gradle').exists()
  83. if (hasBuildExtras) {
  84. apply from: 'build-extras.gradle'
  85. }
  86. // Set property defaults after extension .gradle files.
  87. if (ext.cdvCompileSdkVersion == null) {
  88. ext.cdvCompileSdkVersion = privateHelpers.getProjectTarget()
  89. }
  90. if (ext.cdvBuildToolsVersion == null) {
  91. ext.cdvBuildToolsVersion = privateHelpers.findLatestInstalledBuildTools()
  92. }
  93. if (ext.cdvDebugSigningPropertiesFile == null && file('debug-signing.properties').exists()) {
  94. ext.cdvDebugSigningPropertiesFile = 'debug-signing.properties'
  95. }
  96. if (ext.cdvReleaseSigningPropertiesFile == null && file('release-signing.properties').exists()) {
  97. ext.cdvReleaseSigningPropertiesFile = 'release-signing.properties'
  98. }
  99. // Cast to appropriate types.
  100. ext.cdvBuildMultipleApks = cdvBuildMultipleApks == null ? false : cdvBuildMultipleApks.toBoolean();
  101. ext.cdvMinSdkVersion = cdvMinSdkVersion == null ? null : Integer.parseInt('' + cdvMinSdkVersion)
  102. ext.cdvVersionCode = cdvVersionCode == null ? null : Integer.parseInt('' + cdvVersionCode)
  103. def computeBuildTargetName(debugBuild) {
  104. def ret = 'assemble'
  105. if (cdvBuildMultipleApks && cdvBuildArch) {
  106. def arch = cdvBuildArch == 'arm' ? 'armv7' : cdvBuildArch
  107. ret += '' + arch.toUpperCase().charAt(0) + arch.substring(1);
  108. }
  109. return ret + (debugBuild ? 'Debug' : 'Release')
  110. }
  111. // Make cdvBuild a task that depends on the debug/arch-sepecific task.
  112. task cdvBuildDebug
  113. cdvBuildDebug.dependsOn {
  114. return computeBuildTargetName(true)
  115. }
  116. task cdvBuildRelease
  117. cdvBuildRelease.dependsOn {
  118. return computeBuildTargetName(false)
  119. }
  120. task cdvPrintProps << {
  121. println('cdvCompileSdkVersion=' + cdvCompileSdkVersion)
  122. println('cdvBuildToolsVersion=' + cdvBuildToolsVersion)
  123. println('cdvVersionCode=' + cdvVersionCode)
  124. println('cdvMinSdkVersion=' + cdvMinSdkVersion)
  125. println('cdvBuildMultipleApks=' + cdvBuildMultipleApks)
  126. println('cdvReleaseSigningPropertiesFile=' + cdvReleaseSigningPropertiesFile)
  127. println('cdvDebugSigningPropertiesFile=' + cdvDebugSigningPropertiesFile)
  128. println('cdvBuildArch=' + cdvBuildArch)
  129. println('computedVersionCode=' + android.defaultConfig.versionCode)
  130. android.productFlavors.each { flavor ->
  131. println('computed' + flavor.name.capitalize() + 'VersionCode=' + flavor.versionCode)
  132. }
  133. }
  134. android {
  135. sourceSets {
  136. main {
  137. manifest.srcFile 'AndroidManifest.xml'
  138. java.srcDirs = ['src']
  139. resources.srcDirs = ['src']
  140. aidl.srcDirs = ['src']
  141. renderscript.srcDirs = ['src']
  142. res.srcDirs = ['res']
  143. assets.srcDirs = ['assets']
  144. jniLibs.srcDirs = ['libs']
  145. }
  146. }
  147. defaultConfig {
  148. versionCode cdvVersionCode ?: Integer.parseInt("" + privateHelpers.extractIntFromManifest("versionCode"))
  149. applicationId privateHelpers.extractStringFromManifest("package")
  150. if (cdvMinSdkVersion != null) {
  151. minSdkVersion cdvMinSdkVersion
  152. }
  153. }
  154. lintOptions {
  155. abortOnError false;
  156. }
  157. compileSdkVersion cdvCompileSdkVersion
  158. buildToolsVersion cdvBuildToolsVersion
  159. if (Boolean.valueOf(cdvBuildMultipleApks)) {
  160. productFlavors {
  161. armv7 {
  162. versionCode defaultConfig.versionCode*10 + 2
  163. ndk {
  164. abiFilters "armeabi-v7a", ""
  165. }
  166. }
  167. x86 {
  168. versionCode defaultConfig.versionCode*10 + 4
  169. ndk {
  170. abiFilters "x86", ""
  171. }
  172. }
  173. all {
  174. ndk {
  175. abiFilters "all", ""
  176. }
  177. }
  178. }
  179. }
  180. /*
  181. ELSE NOTHING! DON'T MESS WITH THE VERSION CODE IF YOU DON'T HAVE TO!
  182. else if (!cdvVersionCode) {
  183. def minSdkVersion = cdvMinSdkVersion ?: privateHelpers.extractIntFromManifest("minSdkVersion")
  184. // Vary versionCode by the two most common API levels:
  185. // 14 is ICS, which is the lowest API level for many apps.
  186. // 20 is Lollipop, which is the lowest API level for the updatable system webview.
  187. if (minSdkVersion >= 20) {
  188. defaultConfig.versionCode += 9
  189. } else if (minSdkVersion >= 14) {
  190. defaultConfig.versionCode += 8
  191. }
  192. }
  193. */
  194. compileOptions {
  195. sourceCompatibility JavaVersion.VERSION_1_6
  196. targetCompatibility JavaVersion.VERSION_1_6
  197. }
  198. if (cdvReleaseSigningPropertiesFile) {
  199. signingConfigs {
  200. release {
  201. // These must be set or Gradle will complain (even if they are overridden).
  202. keyAlias = ""
  203. keyPassword = "__unset" // And these must be set to non-empty in order to have the signing step added to the task graph.
  204. storeFile = null
  205. storePassword = "__unset"
  206. }
  207. }
  208. buildTypes {
  209. release {
  210. signingConfig signingConfigs.release
  211. }
  212. }
  213. addSigningProps(cdvReleaseSigningPropertiesFile, signingConfigs.release)
  214. }
  215. if (cdvDebugSigningPropertiesFile) {
  216. addSigningProps(cdvDebugSigningPropertiesFile, signingConfigs.debug)
  217. }
  218. }
  219. dependencies {
  220. compile fileTree(dir: 'libs', include: '*.jar')
  221. // SUB-PROJECT DEPENDENCIES START
  222. debugCompile project(path: "CordovaLib", configuration: "debug")
  223. releaseCompile project(path: "CordovaLib", configuration: "release")
  224. // SUB-PROJECT DEPENDENCIES END
  225. }
  226. def promptForReleaseKeyPassword() {
  227. if (!cdvReleaseSigningPropertiesFile) {
  228. return;
  229. }
  230. if ('__unset'.equals(android.signingConfigs.release.storePassword)) {
  231. android.signingConfigs.release.storePassword = privateHelpers.promptForPassword('Enter key store password: ')
  232. }
  233. if ('__unset'.equals(android.signingConfigs.release.keyPassword)) {
  234. android.signingConfigs.release.keyPassword = privateHelpers.promptForPassword('Enter key password: ');
  235. }
  236. }
  237. gradle.taskGraph.whenReady { taskGraph ->
  238. taskGraph.getAllTasks().each() { task ->
  239. if (task.name == 'validateReleaseSigning') {
  240. promptForReleaseKeyPassword()
  241. }
  242. }
  243. }
  244. def addSigningProps(propsFilePath, signingConfig) {
  245. def propsFile = file(propsFilePath)
  246. def props = new Properties()
  247. propsFile.withReader { reader ->
  248. props.load(reader)
  249. }
  250. def storeFile = new File(props.get('key.store') ?: privateHelpers.ensureValueExists(propsFilePath, props, 'storeFile'))
  251. if (!storeFile.isAbsolute()) {
  252. storeFile = RelativePath.parse(true, storeFile.toString()).getFile(propsFile.getParentFile())
  253. }
  254. if (!storeFile.exists()) {
  255. throw new FileNotFoundException('Keystore file does not exist: ' + storeFile.getAbsolutePath())
  256. }
  257. signingConfig.keyAlias = props.get('key.alias') ?: privateHelpers.ensureValueExists(propsFilePath, props, 'keyAlias')
  258. signingConfig.keyPassword = props.get('keyPassword', props.get('key.alias.password', signingConfig.keyPassword))
  259. signingConfig.storeFile = storeFile
  260. signingConfig.storePassword = props.get('storePassword', props.get('key.store.password', signingConfig.storePassword))
  261. def storeType = props.get('storeType', props.get('key.store.type', ''))
  262. if (!storeType) {
  263. def filename = storeFile.getName().toLowerCase();
  264. if (filename.endsWith('.p12') || filename.endsWith('.pfx')) {
  265. storeType = 'pkcs12'
  266. } else {
  267. storeType = signingConfig.storeType // "jks"
  268. }
  269. }
  270. signingConfig.storeType = storeType
  271. }
  272. for (def func : cdvPluginPostBuildExtras) {
  273. func()
  274. }
  275. // This can be defined within build-extras.gradle as:
  276. // ext.postBuildExtras = { ... code here ... }
  277. if (hasProperty('postBuildExtras')) {
  278. postBuildExtras()
  279. }