From 11eca0942b7d7991e70a3666f1e0418dd5984005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Tue, 24 Jul 2018 00:20:02 +0200 Subject: wip --- platform/android/MapboxGLAndroidSDK/build.gradle | 1 + .../java/com/mapbox/mapboxsdk/maps/MapboxMap.java | 24 ++++++-- platform/android/MapboxGLAndroidSDKLint/.gitignore | 1 + .../android/MapboxGLAndroidSDKLint/build.gradle | 37 ++++++++++++ .../java/com/mapbox/mapboxsdk/lint/KeepDetector.kt | 67 ++++++++++++++++++++++ .../mapbox/mapboxsdk/lint/MapboxIssueRegistry.kt | 9 +++ .../com/mapbox/mapboxsdk/lint/KeepDetectorTest.kt | 28 +++++++++ platform/android/gradle/dependencies.gradle | 10 +++- platform/android/settings.gradle | 2 +- 9 files changed, 170 insertions(+), 9 deletions(-) create mode 100644 platform/android/MapboxGLAndroidSDKLint/.gitignore create mode 100644 platform/android/MapboxGLAndroidSDKLint/build.gradle create mode 100644 platform/android/MapboxGLAndroidSDKLint/src/main/java/com/mapbox/mapboxsdk/lint/KeepDetector.kt create mode 100644 platform/android/MapboxGLAndroidSDKLint/src/main/java/com/mapbox/mapboxsdk/lint/MapboxIssueRegistry.kt create mode 100644 platform/android/MapboxGLAndroidSDKLint/src/test/java/com/mapbox/mapboxsdk/lint/KeepDetectorTest.kt diff --git a/platform/android/MapboxGLAndroidSDK/build.gradle b/platform/android/MapboxGLAndroidSDK/build.gradle index 35b4de003b..0529489783 100644 --- a/platform/android/MapboxGLAndroidSDK/build.gradle +++ b/platform/android/MapboxGLAndroidSDK/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.library' apply plugin: "com.jaredsburrows.license" dependencies { + lintChecks project(":MapboxGLAndroidSDKLint") api (dependenciesList.mapboxAndroidTelemetry) { exclude group: 'com.android.support', module: 'appcompat-v7' } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index 64ad0d29b3..bfb012aeb7 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -15,6 +15,7 @@ import android.support.v4.util.Pools; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; + import com.mapbox.android.gestures.AndroidGesturesManager; import com.mapbox.android.gestures.MoveGestureDetector; import com.mapbox.android.gestures.RotateGestureDetector; @@ -44,11 +45,12 @@ import com.mapbox.mapboxsdk.style.expressions.Expression; import com.mapbox.mapboxsdk.style.layers.Layer; import com.mapbox.mapboxsdk.style.light.Light; import com.mapbox.mapboxsdk.style.sources.Source; -import timber.log.Timber; import java.util.HashMap; import java.util.List; +import timber.log.Timber; + /** * The general class to interact with in the Android Mapbox SDK. It exposes the entry point for all * methods related to the MapView. You cannot instantiate {@link MapboxMap} object directly, rather, @@ -1138,7 +1140,9 @@ public final class MapboxMap { * @param markerOptions A marker options object that defines how to render the marker * @return The {@code Marker} that was added to the map * @deprecated Use a {@link com.mapbox.mapboxsdk.style.layers.SymbolLayer} instead. An example of converting Android - * SDK views to be used as a symbol see https://github.com/mapbox/mapbox-gl-native/blob/68f32bc104422207c64da8d90e8411b138d87f04/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java + * SDK views to be used as a symbol see https://github + * .com/mapbox/mapbox-gl-native/blob/68f32bc104422207c64da8d90e8411b138d87f04/platform/android + * /MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java */ @NonNull @Deprecated @@ -1157,7 +1161,9 @@ public final class MapboxMap { * @param onMarkerViewAddedListener Callback invoked when the View has been added to the map * @return The {@code Marker} that was added to the map * @deprecated Use a {@link com.mapbox.mapboxsdk.style.layers.SymbolLayer} instead. An example of converting Android - * SDK views to be used as a symbol see https://github.com/mapbox/mapbox-gl-native/blob/68f32bc104422207c64da8d90e8411b138d87f04/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java + * SDK views to be used as a symbol see https://github + * .com/mapbox/mapbox-gl-native/blob/68f32bc104422207c64da8d90e8411b138d87f04/platform/android + * /MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java */ @Deprecated @NonNull @@ -1176,7 +1182,9 @@ public final class MapboxMap { * @param markerViewOptions A list of markerView options objects that defines how to render the markers * @return A list of the {@code MarkerView}s that were added to the map * @deprecated Use a {@link com.mapbox.mapboxsdk.style.layers.SymbolLayer} instead. An example of converting Android - * SDK views to be used as a symbol see https://github.com/mapbox/mapbox-gl-native/blob/68f32bc104422207c64da8d90e8411b138d87f04/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java + * SDK views to be used as a symbol see https://github + * .com/mapbox/mapbox-gl-native/blob/68f32bc104422207c64da8d90e8411b138d87f04/platform/android + * /MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java */ @NonNull @Deprecated @@ -1191,7 +1199,9 @@ public final class MapboxMap { * @param rect the rectangular area on the map to query for markerViews * @return A list of the markerViews that were found in the rectangle * @deprecated Use a {@link com.mapbox.mapboxsdk.style.layers.SymbolLayer} instead. An example of converting Android - * SDK views to be used as a symbol see https://github.com/mapbox/mapbox-gl-native/blob/68f32bc104422207c64da8d90e8411b138d87f04/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java + * SDK views to be used as a symbol see https://github + * .com/mapbox/mapbox-gl-native/blob/68f32bc104422207c64da8d90e8411b138d87f04/platform/android + * /MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java */ @NonNull @Deprecated @@ -2625,7 +2635,9 @@ public final class MapboxMap { * * @param the instance type of MarkerView * @deprecated Use a {@link com.mapbox.mapboxsdk.style.layers.SymbolLayer} instead. An example of converting Android - * SDK views to be used as a symbol see https://github.com/mapbox/mapbox-gl-native/blob/68f32bc104422207c64da8d90e8411b138d87f04/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java + * SDK views to be used as a symbol see https://github + * .com/mapbox/mapbox-gl-native/blob/68f32bc104422207c64da8d90e8411b138d87f04/platform/android + * /MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java */ @Deprecated public abstract static class MarkerViewAdapter { diff --git a/platform/android/MapboxGLAndroidSDKLint/.gitignore b/platform/android/MapboxGLAndroidSDKLint/.gitignore new file mode 100644 index 0000000000..796b96d1c4 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKLint/.gitignore @@ -0,0 +1 @@ +/build diff --git a/platform/android/MapboxGLAndroidSDKLint/build.gradle b/platform/android/MapboxGLAndroidSDKLint/build.gradle new file mode 100644 index 0000000000..855e1bc7ca --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKLint/build.gradle @@ -0,0 +1,37 @@ +apply plugin: 'java-library' +apply plugin: 'kotlin' + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + compileOnly dependenciesList.kotlinLib + + compileOnly dependenciesList.lint + compileOnly dependenciesList.lintApi + compileOnly dependenciesList.lintChecks + compileOnly dependenciesList.supportAnnotations + + testImplementation dependenciesList.junit + testImplementation dependenciesList.robolectric + testImplementation dependenciesList.lintTests +} + +sourceCompatibility = "1.8" +targetCompatibility = "1.8" + +compileKotlin { + kotlinOptions { + jvmTarget = "1.8" + } +} + +compileTestKotlin { + kotlinOptions { + jvmTarget = "1.8" + } +} + +jar { + manifest { + attributes("Lint-Registry-v2": "com.mapbox.mapboxsdk.lint.MapboxIssueRegistry") + } +} diff --git a/platform/android/MapboxGLAndroidSDKLint/src/main/java/com/mapbox/mapboxsdk/lint/KeepDetector.kt b/platform/android/MapboxGLAndroidSDKLint/src/main/java/com/mapbox/mapboxsdk/lint/KeepDetector.kt new file mode 100644 index 0000000000..1576090b22 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKLint/src/main/java/com/mapbox/mapboxsdk/lint/KeepDetector.kt @@ -0,0 +1,67 @@ +package com.mapbox.mapboxsdk.lint + +import com.android.tools.lint.client.api.UElementHandler +import com.android.tools.lint.detector.api.* +import com.intellij.lang.jvm.JvmModifier +import com.intellij.psi.PsiType +import org.jetbrains.uast.* + +class KeepDetector : Detector(), SourceCodeScanner { + companion object { + private val DETECTOR_CLASS = KeepDetector::class.java + private val DETECTOR_SCOPE = Scope.JAVA_FILE_SCOPE + private val IMPLEMENTATION = Implementation(DETECTOR_CLASS, DETECTOR_SCOPE) + + private const val ISSUE_ID = "KeepMissing" + private const val ISSUE_DESCRIPTION = "Class cannot be minified." + private const val ISSUE_EXPLANATION = "This class, method or a field might contain native references. " + + "It has to be annotated with @Keep if it cannot be obfuscated or the warning should be suppressed otherwise. " + + "There can also be another methods/fields of this class that cannot be obfuscated." + private val ISSUE_CATEGORY = Category.CORRECTNESS + private const val ISSUE_PRIORITY = 9 + private val ISSUE_SEVERITY = Severity.ERROR + + var ISSUE_NOT_KEPT = Issue.create( + ISSUE_ID, + ISSUE_DESCRIPTION, + ISSUE_EXPLANATION, + ISSUE_CATEGORY, + ISSUE_PRIORITY, + ISSUE_SEVERITY, + IMPLEMENTATION + ) + } + + override fun getApplicableUastTypes(): List>? = + listOf(UMethod::class.java, UField::class.java) + + override fun createUastHandler(context: JavaContext): UElementHandler? = KeepHandler(context) + + class KeepHandler(private val context: JavaContext) : UElementHandler() { + + override fun visitMethod(node: UMethod) { + if (node.hasModifier(JvmModifier.NATIVE) && checkKeepAnnotation(node)) { + context.report(ISSUE_NOT_KEPT, node, + context.getNameLocation(node), + "This method contains native references and will be minified.") + } + } + + override fun visitField(node: UField) { + if (node.type == PsiType.LONG && node.name.contains("native") && checkKeepAnnotation(node)) { + context.report(ISSUE_NOT_KEPT, node, + context.getNameLocation(node as UElement), + "This field might contain native references and will be minified. " + + "Either suppress the warning or use @Keep.") + } + } + + private fun checkKeepAnnotation(uAnnotated: UAnnotated): Boolean { + return !hasKeepAnnotation(uAnnotated) && !hasKeepAnnotation(uAnnotated.getContainingUClass()!!) + } + + private fun hasKeepAnnotation(uAnnotated: UAnnotated): Boolean { + return uAnnotated.findAnnotation("android.support.annotation.Keep") != null + } + } +} \ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKLint/src/main/java/com/mapbox/mapboxsdk/lint/MapboxIssueRegistry.kt b/platform/android/MapboxGLAndroidSDKLint/src/main/java/com/mapbox/mapboxsdk/lint/MapboxIssueRegistry.kt new file mode 100644 index 0000000000..790fab99ea --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKLint/src/main/java/com/mapbox/mapboxsdk/lint/MapboxIssueRegistry.kt @@ -0,0 +1,9 @@ +package com.mapbox.mapboxsdk.lint + +import com.android.tools.lint.client.api.IssueRegistry +import com.android.tools.lint.detector.api.Issue + +class MapboxIssueRegistry : IssueRegistry() { + override val issues: List + get() = listOf(KeepDetector.ISSUE_NOT_KEPT) +} \ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKLint/src/test/java/com/mapbox/mapboxsdk/lint/KeepDetectorTest.kt b/platform/android/MapboxGLAndroidSDKLint/src/test/java/com/mapbox/mapboxsdk/lint/KeepDetectorTest.kt new file mode 100644 index 0000000000..491769df93 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKLint/src/test/java/com/mapbox/mapboxsdk/lint/KeepDetectorTest.kt @@ -0,0 +1,28 @@ +package com.mapbox.mapboxsdk.lint + +import com.android.tools.lint.checks.infrastructure.TestFiles.java +import com.android.tools.lint.checks.infrastructure.TestLintTask.lint +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class KeepDetectorTest { + + @Test + fun correctClassName() { + lint() + .allowMissingSdk() + .files(java(""" + |package foo; + | + |import android.support.annotation.Keep; + | + |@Keep + |class TestClass { + |}""".trimMargin())) + .issues(KeepDetector.ISSUE_NOT_KEPT) + .run() + .expectClean() + } +} \ No newline at end of file diff --git a/platform/android/gradle/dependencies.gradle b/platform/android/gradle/dependencies.gradle index fccaea9f71..dbd64a6a05 100644 --- a/platform/android/gradle/dependencies.gradle +++ b/platform/android/gradle/dependencies.gradle @@ -21,7 +21,8 @@ ext { timber : '4.7.1', okhttp : '3.11.0', kotlin : '1.2.51', - licenses : '0.8.41' + licenses : '0.8.41', + lint : '26.1.3' ] dependenciesList = [ @@ -57,6 +58,11 @@ ext { kotlinLib : "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${versions.kotlin}", kotlinPlugin : "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}", - licensesPlugin : "com.jaredsburrows:gradle-license-plugin:${versions.licenses}" + licensesPlugin : "com.jaredsburrows:gradle-license-plugin:${versions.licenses}", + + lint : "com.android.tools.lint:lint:${versions.lint}", + lintApi : "com.android.tools.lint:lint-api:${versions.lint}", + lintChecks : "com.android.tools.lint:lint-checks:${versions.lint}", + lintTests : "com.android.tools.lint:lint-tests:${versions.lint}" ] } diff --git a/platform/android/settings.gradle b/platform/android/settings.gradle index b5ab80b5ec..c0315fed04 100644 --- a/platform/android/settings.gradle +++ b/platform/android/settings.gradle @@ -1 +1 @@ -include ':MapboxGLAndroidSDK', ':MapboxGLAndroidSDKTestApp' \ No newline at end of file +include ':MapboxGLAndroidSDK', ':MapboxGLAndroidSDKTestApp', ':MapboxGLAndroidSDKLint' \ No newline at end of file -- cgit v1.2.1