From 01729b9a49af9836686658a525ddae3cdc6698cb Mon Sep 17 00:00:00 2001 From: Bart Louwers Date: Wed, 11 Jan 2023 15:09:58 +0000 Subject: [PATCH] [android] Fix Ktlint Gradle intergration, add documentation about linting (#685) --- platform/android/CONTRIBUTING.md | 23 ++++++++++++++ platform/android/Makefile | 2 +- .../android/MapboxGLAndroidSDK/build.gradle | 4 +++ .../MapboxGLAndroidSDKLint/build.gradle | 4 +++ .../MapboxGLAndroidSDKTestApp/build.gradle | 6 +++- .../mapboxsdk/offline/OfflineDownloadTest.kt | 4 +-- ...CustomizedLocationPulsingCircleActivity.kt | 18 +++++------ platform/android/build.gradle | 8 +++-- platform/android/gradle/ktlint.gradle | 31 ------------------- 9 files changed, 53 insertions(+), 47 deletions(-) create mode 100644 platform/android/CONTRIBUTING.md delete mode 100644 platform/android/gradle/ktlint.gradle diff --git a/platform/android/CONTRIBUTING.md b/platform/android/CONTRIBUTING.md new file mode 100644 index 000000000..ee2e081f5 --- /dev/null +++ b/platform/android/CONTRIBUTING.md @@ -0,0 +1,23 @@ +# Contributing - MapLibre GL Native for Android + +## Kotlin + +All new code should be written in [Kotlin](https://kotlinlang.org/). + +### Style Checking + +To check Kotlin style, we use [ktlint](https://pinterest.github.io/ktlint/). This linter is based on the [official Kotlin coding conventions](https://kotlinlang.org/docs/coding-conventions.html). We intergrate with it using the [kotlinder](https://github.com/jeremymailen/kotlinter-gradle) Gradle plugin. + +To check the style of all Kotlin source files, use: + +``` +$ ./gradlew checkStyle +``` + +To format all Kotlin source files, use: + +``` +$ ./gradlew formatKotlin +``` + +You might find it helpful to install the [Ktlint](https://plugins.jetbrains.com/plugin/15057-ktlint-unofficial-) Android Studio plugin. diff --git a/platform/android/Makefile b/platform/android/Makefile index fc8f4277a..ad58a70e2 100644 --- a/platform/android/Makefile +++ b/platform/android/Makefile @@ -276,7 +276,7 @@ android-checkstyle: gradle/configuration.gradle # Runs checkstyle on the kotlin code .PHONY: android-ktlint android-ktlint: gradle/configuration.gradle - $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=none ktlint + $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=none checkstyle # Runs lint on the Android SDK java code .PHONY: android-lint-sdk diff --git a/platform/android/MapboxGLAndroidSDK/build.gradle b/platform/android/MapboxGLAndroidSDK/build.gradle index bea1f00a8..b3a9e2ffe 100644 --- a/platform/android/MapboxGLAndroidSDK/build.gradle +++ b/platform/android/MapboxGLAndroidSDK/build.gradle @@ -1,3 +1,7 @@ +plugins { + id 'org.jmailen.kotlinter' +} + apply plugin: 'com.android.library' apply plugin: "com.jaredsburrows.license" apply plugin: 'kotlin-android' diff --git a/platform/android/MapboxGLAndroidSDKLint/build.gradle b/platform/android/MapboxGLAndroidSDKLint/build.gradle index 855e1bc7c..1e0952d91 100644 --- a/platform/android/MapboxGLAndroidSDKLint/build.gradle +++ b/platform/android/MapboxGLAndroidSDKLint/build.gradle @@ -1,3 +1,7 @@ +plugins { + id 'org.jmailen.kotlinter' +} + apply plugin: 'java-library' apply plugin: 'kotlin' diff --git a/platform/android/MapboxGLAndroidSDKTestApp/build.gradle b/platform/android/MapboxGLAndroidSDKTestApp/build.gradle index f24fd9709..1d06c6954 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/build.gradle +++ b/platform/android/MapboxGLAndroidSDKTestApp/build.gradle @@ -1,4 +1,8 @@ -apply plugin: 'com.android.application' +plugins { + id 'org.jmailen.kotlinter' + id 'com.android.application' +} + apply from: "${rootDir}/gradle/native-build.gradle" android { diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/offline/OfflineDownloadTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/offline/OfflineDownloadTest.kt index e3eefd6d4..e0dbf9d05 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/offline/OfflineDownloadTest.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/offline/OfflineDownloadTest.kt @@ -42,7 +42,7 @@ class OfflineDownloadTest : OfflineRegion.OfflineRegionObserver { override fun onError(error: String) { Logger.e(TAG, "Error while creating offline region: $error") } - }, + } ) } @@ -74,7 +74,7 @@ class OfflineDownloadTest : OfflineRegion.OfflineRegionObserver { 17.0, 17.0, 1.0f, - false, + false ) } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/location/CustomizedLocationPulsingCircleActivity.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/location/CustomizedLocationPulsingCircleActivity.kt index 4380d93be..28b53e1cf 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/location/CustomizedLocationPulsingCircleActivity.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/location/CustomizedLocationPulsingCircleActivity.kt @@ -243,9 +243,9 @@ class CustomizedLocationPulsingCircleActivity : AppCompatActivity(), OnMapReadyC pulsingCircleDurationButton!!.text = selectedMode if (selectedMode.contentEquals( String.format( - "%sms", - DEFAULT_LOCATION_CIRCLE_PULSE_DURATION_MS.toString() - ) + "%sms", + DEFAULT_LOCATION_CIRCLE_PULSE_DURATION_MS.toString() + ) ) ) { LOCATION_CIRCLE_PULSE_DURATION = DEFAULT_LOCATION_CIRCLE_PULSE_DURATION_MS @@ -255,9 +255,9 @@ class CustomizedLocationPulsingCircleActivity : AppCompatActivity(), OnMapReadyC ) } else if (selectedMode.contentEquals( String.format( - "%sms", - SECOND_LOCATION_CIRCLE_PULSE_DURATION_MS.toString() - ) + "%sms", + SECOND_LOCATION_CIRCLE_PULSE_DURATION_MS.toString() + ) ) ) { LOCATION_CIRCLE_PULSE_DURATION = SECOND_LOCATION_CIRCLE_PULSE_DURATION_MS @@ -267,9 +267,9 @@ class CustomizedLocationPulsingCircleActivity : AppCompatActivity(), OnMapReadyC ) } else if (selectedMode.contentEquals( String.format( - "%sms", - THIRD_LOCATION_CIRCLE_PULSE_DURATION_MS.toString() - ) + "%sms", + THIRD_LOCATION_CIRCLE_PULSE_DURATION_MS.toString() + ) ) ) { LOCATION_CIRCLE_PULSE_DURATION = THIRD_LOCATION_CIRCLE_PULSE_DURATION_MS diff --git a/platform/android/build.gradle b/platform/android/build.gradle index ab12a3a22..2f5e5de8f 100644 --- a/platform/android/build.gradle +++ b/platform/android/build.gradle @@ -1,5 +1,3 @@ -apply plugin: 'io.github.gradle-nexus.publish-plugin' - buildscript { apply from: "${rootDir}/gradle/dependencies.gradle" @@ -17,12 +15,16 @@ buildscript { } } +plugins { + id("io.github.gradle-nexus.publish-plugin") version "1.1.0" + id("org.jmailen.kotlinter") version "3.13.0" apply false +} + allprojects { repositories { mavenCentral() google() } - apply from: "${rootDir}/gradle/ktlint.gradle" } subprojects { diff --git a/platform/android/gradle/ktlint.gradle b/platform/android/gradle/ktlint.gradle deleted file mode 100644 index b2dab4ba2..000000000 --- a/platform/android/gradle/ktlint.gradle +++ /dev/null @@ -1,31 +0,0 @@ -apply from: "${rootDir}/gradle/dependencies.gradle" - -repositories { - mavenCentral() -} - -configurations { - ktlint -} - -dependencies { - ktlint("com.pinterest:ktlint:0.48.0") { - attributes { - attribute(Bundling.BUNDLING_ATTRIBUTE, getObjects().named(Bundling, Bundling.EXTERNAL)) - } - } -} - -task ktlint(type: JavaExec, group: "verification") { - description = "Check Kotlin code style." - classpath = configurations.ktlint - main = "com.pinterest.ktlint.Main" - args "--disabled_rules=no-wildcard-imports,trailing-comma-on-call-site,trailing-comma-on-declaration-site", "src/**/*.kt" -} - -task ktlintFormat(type: JavaExec, group: "formatting") { - description = "Fix Kotlin code style deviations." - classpath = configurations.ktlint - main = "com.pinterest.ktlint.Main" - args "-F", "src/**/*.kt" -} \ No newline at end of file