From dee1e77d0b69aaa1069fd6e21400e3f617ea6e0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A8=E3=83=AA=E3=82=B9?= Date: Mon, 27 Jan 2020 16:14:58 +0900 Subject: [PATCH] feat: add kotlin support (#896) Co-authored-by: Joshua Chandler --- .gitignore | 1 + bin/templates/cordova/lib/prepare.js | 5 ++++ bin/templates/project/app/build.gradle | 37 ++++++++++++++++++++++++++ bin/templates/project/build.gradle | 6 ++--- test/android/app/build.gradle | 7 +++++ test/android/build.gradle | 3 +++ 6 files changed, 56 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 4c093a9b..e7a91562 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,4 @@ package-lock.json # Eclipse Buildship files .project .settings +.classpath diff --git a/bin/templates/cordova/lib/prepare.js b/bin/templates/cordova/lib/prepare.js index a8d120d4..14abcee4 100644 --- a/bin/templates/cordova/lib/prepare.js +++ b/bin/templates/cordova/lib/prepare.js @@ -46,11 +46,16 @@ module.exports.prepare = function (cordovaProject, options) { const maxSdkVersion = this._config.getPreference('android-maxSdkVersion', 'android'); const targetSdkVersion = this._config.getPreference('android-targetSdkVersion', 'android'); const androidXEnabled = this._config.getPreference('AndroidXEnabled', 'android'); + const isGradlePluginKotlinEnabled = this._config.getPreference('GradlePluginKotlinEnabled', 'android'); + const gradlePluginKotlinCodeStyle = this._config.getPreference('GradlePluginKotlinCodeStyle', 'android'); let gradlePropertiesUserConfig = {}; if (minSdkVersion) gradlePropertiesUserConfig.cdvMinSdkVersion = minSdkVersion; if (maxSdkVersion) gradlePropertiesUserConfig.cdvMaxSdkVersion = maxSdkVersion; if (targetSdkVersion) gradlePropertiesUserConfig.cdvTargetSdkVersion = targetSdkVersion; + if (isGradlePluginKotlinEnabled) { + gradlePropertiesUserConfig['kotlin.code.style'] = gradlePluginKotlinCodeStyle || 'official'; + } // Both 'useAndroidX' and 'enableJetifier' are linked together. if (androidXEnabled) { diff --git a/bin/templates/project/app/build.gradle b/bin/templates/project/app/build.gradle index 29503bdb..fdbb7254 100644 --- a/bin/templates/project/app/build.gradle +++ b/bin/templates/project/app/build.gradle @@ -19,7 +19,31 @@ apply plugin: 'com.android.application' +if (cdvHelpers.getConfigPreference('GradlePluginKotlinEnabled', 'false').toBoolean()) { + apply plugin: 'kotlin-android' + apply plugin: 'kotlin-android-extensions' +} + buildscript { + apply from: '../CordovaLib/cordova.gradle' + + if(cdvHelpers.getConfigPreference('GradlePluginKotlinEnabled', 'false').toBoolean()) { + String defaultGradlePluginKotlinVersion = kotlin_version + + /** + * Fetches the user's defined Kotlin Version from config.xml. + * If the version is not set or invalid, it will default to the ${defaultGradlePluginKotlinVersion} + */ + String gradlePluginKotlinVersion = cdvHelpers.getConfigPreference('GradlePluginKotlinVersion', defaultGradlePluginKotlinVersion) + if(!cdvHelpers.isVersionValid(gradlePluginKotlinVersion)) { + println("The defined Kotlin version (${gradlePluginKotlinVersion}) does not appear to be a valid version. Falling back to version: ${defaultGradlePluginKotlinVersion}.") + gradlePluginKotlinVersion = defaultGradlePluginKotlinVersion + } + + // Change the version to be used. + ext.kotlin_version = gradlePluginKotlinVersion + } + repositories { mavenCentral() google() @@ -31,6 +55,10 @@ buildscript { classpath 'com.android.tools.build:gradle:3.5.3' + if (cdvHelpers.getConfigPreference('GradlePluginKotlinEnabled', 'false').toBoolean()) { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } + if(cdvHelpers.getConfigPreference('GradlePluginGoogleServicesEnabled', 'false').toBoolean()) { String defaultGradlePluginGoogleServicesVersion = '4.2.0' @@ -301,6 +329,10 @@ android { if (cdvDebugSigningPropertiesFile) { addSigningProps(cdvDebugSigningPropertiesFile, signingConfigs.debug) } + + sourceSets { + main.java.srcDirs += 'src/main/kotlin' + } } /* @@ -312,6 +344,11 @@ android { dependencies { implementation fileTree(dir: 'libs', include: '*.jar') + + if (cdvHelpers.getConfigPreference('GradlePluginKotlinEnabled', 'false').toBoolean()) { + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + } + // SUB-PROJECT DEPENDENCIES START debugCompile(project(path: ":CordovaLib", configuration: "debug")) releaseCompile(project(path: ":CordovaLib", configuration: "release")) diff --git a/bin/templates/project/build.gradle b/bin/templates/project/build.gradle index 3c9aa712..b39038e9 100644 --- a/bin/templates/project/build.gradle +++ b/bin/templates/project/build.gradle @@ -19,16 +19,16 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { + ext.kotlin_version = '1.3.50' repositories { google() jcenter() } - dependencies { + classpath 'com.android.tools.build:gradle:3.5.3' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files - - classpath 'com.android.tools.build:gradle:3.5.3' } } diff --git a/test/android/app/build.gradle b/test/android/app/build.gradle index 33eb937d..88698a35 100644 --- a/test/android/app/build.gradle +++ b/test/android/app/build.gradle @@ -17,6 +17,8 @@ */ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 29 @@ -51,4 +53,9 @@ dependencies { implementation 'com.android.support:appcompat-v7:26.1.0' testImplementation 'junit:junit:4.12' testImplementation 'org.json:json:20140107' + + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} +repositories { + jcenter() } diff --git a/test/android/build.gradle b/test/android/build.gradle index fbfcb272..83d8f635 100644 --- a/test/android/build.gradle +++ b/test/android/build.gradle @@ -19,6 +19,8 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { + ext.kotlin_version = '1.3.50' + repositories { google() jcenter() @@ -29,6 +31,7 @@ buildscript { // in the individual module build.gradle files classpath 'com.android.tools.build:gradle:3.5.3' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }