diff options
author | Łukasz Paczos <lukas.paczos@gmail.com> | 2018-07-24 00:20:02 +0200 |
---|---|---|
committer | Łukasz Paczos <lukasz.paczos@mapbox.com> | 2018-07-26 19:47:11 +0200 |
commit | 94ad8e2ff8928bec316dfcc9790db7b522673d3e (patch) | |
tree | 1a7b0c7fb42aa6dba049c5d42305f6d5c4ac93b7 /platform/android/MapboxGLAndroidSDKLint | |
parent | 5fcd6e38a9e9ca755d997b83f007f8a2f435cf45 (diff) | |
download | qtlocation-mapboxgl-94ad8e2ff8928bec316dfcc9790db7b522673d3e.tar.gz |
[android] using @Keep annotation across the project with the help of lint checks
Diffstat (limited to 'platform/android/MapboxGLAndroidSDKLint')
5 files changed, 150 insertions, 0 deletions
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..64838f91db --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKLint/src/main/java/com/mapbox/mapboxsdk/lint/KeepDetector.kt @@ -0,0 +1,75 @@ +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, FileScanner { + 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 = "Element cannot be minified." + private const val ISSUE_EXPLANATION = "This class, method or field might contain native references. " + + "It has to be annotated with @Keep if it cannot be obfuscated/removed, otherwise, the warning should be suppressed. " + + "There can also be another methods/fields of this class that cannot be obfuscated/removed, " + + "look closely for any methods that are referenced from the JNI context." + 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<Class<out UElement>>? = + 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.") + } else if (node.isConstructor + && node.parameterList.parameters.find { it.type == PsiType.LONG && it.name!!.contains("native") } != null + && checkKeepAnnotation(node)) { + context.report(ISSUE_NOT_KEPT, node, + context.getNameLocation(node as UElement), + "This constructor might contain native references and will be minified. " + + "Either suppress the warning or use @Keep.") + } + } + + 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<Issue> + 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 |