From 7d74969dd9a9bb2e774660ea5435e5b60867c59d Mon Sep 17 00:00:00 2001 From: Sean Mac Gillicuddy Date: Fri, 29 Nov 2019 13:54:28 +0000 Subject: [PATCH] #947 convert build.gradle to build.gradle.kts --- app/build.gradle | 83 ------------------- app/build.gradle.kts | 80 ++++++++++++++++++ buildSrc/src/main/kotlin/Versions.kt | 2 +- buildSrc/src/main/kotlin/custom/CustomApp.kt | 49 +++++++++++ buildSrc/src/main/kotlin/custom/CustomApps.kt | 78 +++++++++-------- core/{build.gradle => build.gradle.kts} | 37 ++++----- custom/build.gradle.kts | 5 +- custom/dynamic_flavors.gradle | 43 ---------- settings.gradle | 4 - settings.gradle.kts | 6 ++ 10 files changed, 193 insertions(+), 194 deletions(-) delete mode 100644 app/build.gradle create mode 100644 app/build.gradle.kts create mode 100644 buildSrc/src/main/kotlin/custom/CustomApp.kt rename core/{build.gradle => build.gradle.kts} (53%) delete mode 100644 custom/dynamic_flavors.gradle delete mode 100644 settings.gradle create mode 100644 settings.gradle.kts diff --git a/app/build.gradle b/app/build.gradle deleted file mode 100644 index c6dd145bc..000000000 --- a/app/build.gradle +++ /dev/null @@ -1,83 +0,0 @@ -import plugin.KiwixConfigurationPlugin - -plugins { - id("com.android.application") - id("com.github.triplet.play") version("2.5.0") -} -apply plugin: KiwixConfigurationPlugin - -apply from: rootProject.file("jacoco.gradle") - -ext { - versionMajor = 3 - versionMinor = 0 - versionPatch = 4 -} - -private String generateVersionName() { - "${ext.versionMajor}.${ext.versionMinor}.${ext.versionPatch}" -} - -/* -* max version code: 21-0-0-00-00-00 -* our template : UU-D-A-ZZ-YY-XX -* where: -* X = patch version -* Y = minor version -* Z = major version (+ 20 to distinguish from previous, non semantic, versions of the app) -* A = number representing ABI split -* D = number representing density split -* U = unused -*/ - -private Integer generateVersionCode() { - 20 * 10000 + - (ext.versionMajor * 10000) + - (ext.versionMinor * 100) + - (ext.versionPatch) -} - -def buildNumber = System.getenv('TRAVIS_BUILD_NUMBER') ?: "dev" - -android { - - defaultConfig { - archivesBaseName = "$buildNumber" - resValue "string", "app_name", "Kiwix" - resValue "string", "app_search_string", "Search Kiwix" - versionCode generateVersionCode() - versionName generateVersionName() - } - - buildTypes { - debug { - multiDexKeepProguard file("multidex-instrumentation-config.pro") - buildConfigField "boolean", "KIWIX_ERROR_ACTIVITY", "false" - } - - release { - buildConfigField "boolean", "KIWIX_ERROR_ACTIVITY", "true" - } - } - - sourceSets { - androidTest { - java.srcDirs += "$rootDir/core/src/sharedTestFunctions/java" - } - } -} - -play { - enabled = true - serviceAccountCredentials = file("../google.json") - track = "alpha" - releaseStatus = "draft" - resolutionStrategy = "fail" -} - -dependencies { - implementation(Libs.squidb) - implementation(Libs.squidb_annotations) - implementation(Libs.ink_page_indicator) - kapt(Libs.squidb_processor) -} diff --git a/app/build.gradle.kts b/app/build.gradle.kts new file mode 100644 index 000000000..457b341ff --- /dev/null +++ b/app/build.gradle.kts @@ -0,0 +1,80 @@ +import plugin.KiwixConfigurationPlugin + +plugins { + android + id("com.github.triplet.play") version Versions.com_github_triplet_play_gradle_plugin +} +plugins.apply(KiwixConfigurationPlugin::class) + +apply(from = rootProject.file("jacoco.gradle")) + +ext { + set("versionMajor", 3) + set("versionMinor", 0) + set("versionPatch", 4) +} + +fun generateVersionName() = "${ext["versionMajor"]}.${ext["versionMinor"]}.${ext["versionPatch"]}" + +/* +* max version code: 21-0-0-00-00-00 +* our template : UU-D-A-ZZ-YY-XX +* where: +* X = patch version +* Y = minor version +* Z = major version (+ 20 to distinguish from previous, non semantic, versions of the app) +* A = number representing ABI split +* D = number representing density split +* U = unused +*/ + +fun generateVersionCode() = + 20 * 10000 + + ((ext["versionMajor"] as Int) * 10000) + + ((ext["versionMinor"] as Int) * 100) + + ext["versionPatch"] as Int + +val buildNumber get() = System.getenv("TRAVIS_BUILD_NUMBER") ?: "dev" + +android { + + defaultConfig { + // archivesBaseName = "$buildNumber" + resValue("string", "app_name", "Kiwix") + resValue("string", "app_search_string", "Search Kiwix") + versionCode = generateVersionCode() + versionName = generateVersionName() + } + + buildTypes { + getByName("debug") { + multiDexKeepProguard = file("multidex-instrumentation-config.pro") + buildConfigField("boolean", "KIWIX_ERROR_ACTIVITY", "false") + } + + getByName("release") { + buildConfigField("boolean", "KIWIX_ERROR_ACTIVITY", "true") + } + } + + sourceSets { + getByName("androidTest") { + java.srcDirs("$rootDir/core/src/sharedTestFunctions/java") + } + } +} + +play { + isEnabled = true + serviceAccountCredentials = file("../google.json") + track = "alpha" + releaseStatus = "draft" + resolutionStrategy = "fail" +} + +dependencies { + implementation(Libs.squidb) + implementation(Libs.squidb_annotations) + implementation(Libs.ink_page_indicator) + add("kapt", Libs.squidb_processor) +} diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index efc4ef33a..87a2aa158 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -31,7 +31,7 @@ import org.gradle.plugin.use.PluginDependencySpec object Versions { const val de_fayard_buildsrcversions_gradle_plugin: String = "0.6.1" - const val com_github_triplet_play_gradle_plugin: String = "2.4.1" // available: "2.4.2" + const val com_github_triplet_play_gradle_plugin: String = "2.5.0" // available: "2.4.2" const val android_arch_lifecycle_extensions: String = "1.1.1" diff --git a/buildSrc/src/main/kotlin/custom/CustomApp.kt b/buildSrc/src/main/kotlin/custom/CustomApp.kt new file mode 100644 index 000000000..8146610d6 --- /dev/null +++ b/buildSrc/src/main/kotlin/custom/CustomApp.kt @@ -0,0 +1,49 @@ +/* + * Kiwix Android + * Copyright (c) 2019 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package custom + +import java.text.ParseException +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale + +data class CustomApp( + val name: String, + val url: String, + val enforcedLanguage: String, + val displayName: String, + val versionName: String = parseVersionNameFromUrlOrUsePattern(url, "YYYY-MM") +) { + val versionCode: Int = formatDate("YYDDD0").toInt() +} + +private fun parseVersionNameFromUrlOrUsePattern(url: String, pattern: String) = + url.substringAfterLast("_") + .substringBeforeLast(".") + .takeIf { + try { + SimpleDateFormat(pattern, Locale.ROOT).parse(it) != null + } catch (parseException: ParseException) { + false + } + } + ?: formatDate(pattern) + +private fun formatDate(pattern: String) = + Date().let(SimpleDateFormat(pattern, Locale.ROOT)::format) diff --git a/buildSrc/src/main/kotlin/custom/CustomApps.kt b/buildSrc/src/main/kotlin/custom/CustomApps.kt index db5cc7f62..a93a5ee03 100644 --- a/buildSrc/src/main/kotlin/custom/CustomApps.kt +++ b/buildSrc/src/main/kotlin/custom/CustomApps.kt @@ -19,12 +19,10 @@ package custom import com.android.build.gradle.internal.dsl.ProductFlavor -import custom.CustomApps.CustomApp import org.gradle.api.NamedDomainObjectContainer -import java.text.ParseException -import java.text.SimpleDateFormat -import java.util.Date -import java.util.Locale +import org.json.simple.JSONObject +import org.json.simple.parser.JSONParser +import java.io.File object CustomApps { private val example = CustomApp( @@ -143,7 +141,7 @@ object CustomApps { enforcedLanguage = "de", displayName = "Wikivoyage auf Deutsch" ) - val all = listOf( + private val all = listOf( example, phet, tunisie, @@ -165,49 +163,47 @@ object CustomApps { wikivoyagede ) - fun createCustomAppFromJson( - name: String, - url: String, - enforcedLanguage: String, - displayName: String, - versionName: String? - ) = if (versionName == null) CustomApp(name, url, enforcedLanguage, displayName) - else CustomApp(name, url, enforcedLanguage, displayName, versionName) - - data class CustomApp( - val name: String, - val url: String, - val enforcedLanguage: String, - val displayName: String, - val versionName: String = parseVersionNameFromUrlOrUsePattern(url, "YYYY-MM") - ) { - val versionCode: Int = formatDate("YYDDD0").toInt() - - fun createFlavor(namedDomainObjectContainer: NamedDomainObjectContainer) { - namedDomainObjectContainer.create(this) - } + fun createStatically(productFlavors: NamedDomainObjectContainer) { + productFlavors.create(all) } - private fun parseVersionNameFromUrlOrUsePattern(url: String, pattern: String) = - url.substringAfterLast("_") - .substringBeforeLast(".") - .takeIf { - try { - SimpleDateFormat(pattern, Locale.ROOT).parse(it) != null - } catch (parseException: ParseException) { - false - } - } - ?: formatDate(pattern) - - private fun formatDate(pattern: String) = - Date().let(SimpleDateFormat(pattern, Locale.ROOT)::format) + fun createDynamically( + srcFolder: File, + productFlavors: NamedDomainObjectContainer + ) { + productFlavors.create( + srcFolder.walk() + .filter { it.name == "info.json" } + .map { + val parsedJson = JSONParser().parse(it.readText()) as JSONObject + createCustomAppFromJson( + it.parentFile.name, + parsedJson.getAndCast("zim_url"), + parsedJson.getAndCast("enforced_lang"), + parsedJson.getAndCast("app_name"), + parsedJson.getAndCast("version_name") + ) + }.toList() + ) + } } fun NamedDomainObjectContainer.create(customApps: List) { customApps.forEach(this::create) } +private fun createCustomAppFromJson( + name: String, + url: String, + enforcedLanguage: String, + displayName: String, + versionName: String? +) = if (versionName == null) CustomApp(name, url, enforcedLanguage, displayName) +else CustomApp(name, url, enforcedLanguage, displayName, versionName) + +fun JSONObject.getAndCast(columnName: String): T = + getOrDefault(columnName, null) as T + private fun NamedDomainObjectContainer.create( customApp: CustomApp ) { diff --git a/core/build.gradle b/core/build.gradle.kts similarity index 53% rename from core/build.gradle rename to core/build.gradle.kts index f69424b00..ba9bfc9ef 100644 --- a/core/build.gradle +++ b/core/build.gradle.kts @@ -8,28 +8,26 @@ buildscript { } dependencies { - classpath Libs.objectbox_gradle_plugin - classpath Libs.butterknife_gradle_plugin + classpath(Libs.objectbox_gradle_plugin) + classpath(Libs.butterknife_gradle_plugin) } } - -apply plugin: 'com.android.library' -apply plugin: KiwixConfigurationPlugin -apply plugin: 'io.objectbox' -apply plugin: 'com.jakewharton.butterknife' +plugins { + `android-library` +} +plugins.apply(KiwixConfigurationPlugin::class) +apply(plugin = "io.objectbox") +apply(plugin = "com.jakewharton.butterknife") android { buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + getByName("release") { + isMinifyEnabled = false } } } -private boolean shouldUseLocalVersion() { - file("./libs").exists() -} +fun shouldUseLocalVersion() = File(projectDir, "libs").exists() dependencies { @@ -40,24 +38,23 @@ dependencies { if (!shouldUseLocalVersion()) { api(Libs.kiwixlib) } else { - implementation 'com.getkeepsafe.relinker:relinker:1.3.1' - implementation fileTree(include: ['*.aar'], dir: 'libs') + implementation("com.getkeepsafe.relinker:relinker:1.3.1") + implementation(fileTree(mapOf("include" to "*.aar", "dir" to "libs"))) } // Android Support implementation(Libs.cardview) - // SquiDB implementation(Libs.squidb) implementation(Libs.squidb_annotations) - kapt(Libs.squidb_processor) + add("kapt", Libs.squidb_processor) // Square implementation(Libs.converter_simplexml) { - exclude group: "xpp3", module: "xpp3" - exclude group: "stax", module: "stax-api" - exclude group: "stax", module: "stax" + exclude(group = "xpp3", module = "xpp3") + exclude(group = "stax", module = "stax-api") + exclude(group = "stax", module = "stax") } // Leak canary diff --git a/custom/build.gradle.kts b/custom/build.gradle.kts index 1573d0960..acb1cb61e 100644 --- a/custom/build.gradle.kts +++ b/custom/build.gradle.kts @@ -1,6 +1,7 @@ import com.android.build.gradle.api.ApkVariantOutput import com.android.build.gradle.api.ApplicationVariant import com.android.build.gradle.internal.dsl.ProductFlavor +import custom.CustomApps import custom.createPublisher import custom.transactionWithCommit import plugin.KiwixConfigurationPlugin @@ -22,8 +23,8 @@ android { productFlavors { // Uncomment for static productFlavors - // create(CustomApps.all) - apply(from = "dynamic_flavors.gradle") + // CustomApps.createStatically(this) + CustomApps.createDynamically(project.file("src"), this) all { File("$projectDir/src", "$name/$name.zim").let { createDownloadTask(it) diff --git a/custom/dynamic_flavors.gradle b/custom/dynamic_flavors.gradle deleted file mode 100644 index 7580acb08..000000000 --- a/custom/dynamic_flavors.gradle +++ /dev/null @@ -1,43 +0,0 @@ -import custom.CustomApps -import groovy.json.JsonSlurper - -apply plugin: 'com.android.application' - -// Set custom app import directory -def map = [:] -def custom = new File("custom/src") -if (project.hasProperty("customDir")) { - custom = file(project.property("customDir")) -} -// Set up flavours for each custom app in the directory -if (custom.listFiles()) { - custom.eachFile() { file -> - - def fileName = file.getName() - if (fileName.startsWith(".") || - fileName.contains("test") || - fileName == "main" || - fileName.contains("Test")) { - return - } - map.put(fileName, file.getAbsolutePath()) - } -} -android { - productFlavors { - // Custom apps built from a json file - map.each { name, directory -> - def jsonFile = file(directory + "/info.json") - if (jsonFile.exists()) { - def parsedJson = new JsonSlurper().parseText(jsonFile.text) - CustomApps.INSTANCE.createCustomAppFromJson( - name, - parsedJson.zim_url, - parsedJson.enforced_lang, - parsedJson.app_name, - parsedJson.version_name - ).createFlavor(it) - } - } - } -} diff --git a/settings.gradle b/settings.gradle deleted file mode 100644 index d4fb2c08c..000000000 --- a/settings.gradle +++ /dev/null @@ -1,4 +0,0 @@ -include ':core' -include ':app' -include ':custom' -rootProject.name='kiwix-android' diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 000000000..b684c2f5c --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,6 @@ +include( + ":core", + ":app", + ":custom" +) +rootProject.name = "kiwix-android"