Get java.lang.NoSuchFieldError: Companion when write custom lint rules with Kotlin

691 views
Skip to first unread message

高进

unread,
Apr 29, 2019, 1:21:30 PM4/29/19
to lint-dev
when i write custom lint rule with kotlin like this:
class AlarmDetector : Detector(), SourceCodeScanner {
    companion object Issues {
        private val IMPLEMENTATION = Implementation(
            AlarmDetector::class.java,
            Scope.JAVA_FILE_SCOPE
        )

        /** Alarm set too soon/frequently   */
        @JvmField
        val ISSUE = Issue.create(
            id = "ShortAlarm",
            briefDescription = "Short or Frequent Alarm",
            explanation = """
            Frequent alarms are bad for battery life. As of API 22, the `AlarmManager` will override \
            near-future and high-frequency alarm requests, delaying the alarm at least 5 seconds into the \
            future and ensuring that the repeat interval is at least 60 seconds.

            If you really need to do work sooner than 5 seconds, post a delayed message or runnable to a \
            Handler.""",
            category = Category.CORRECTNESS,
            priority = 6,
            severity = Severity.WARNING,
            implementation = IMPLEMENTATION
        )
    }
    other code ...
}

after assemble lint.jar and place it to "<HOME>/.android/lint"

it run fine in my project with android gradle plugin:3.1.1

but when i use the lint tools in android sdk (sdk/tools/bin/lint) with command: lint --show

I got error
Could not load custom lint check jar file /Users/gaojin/.android/lint/lint.jar
java.lang.NoSuchFieldError: Companion
at com.bytedance.ci.rules.lint.rules.ByteBlockDestroyCheck.<clinit>(ByteBlockDestroyCheck.kt:20)
at com.bytedance.ci.rules.lint.IesLintRegistry.<init>(IesLintRegistry.kt:17)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at com.android.tools.lint.client.api.JarFileIssueRegistry$Factory.loadIssueRegistry(JarFileIssueRegistry.kt:210)
at com.android.tools.lint.client.api.JarFileIssueRegistry$Factory.get(JarFileIssueRegistry.kt:174)
at com.android.tools.lint.client.api.JarFileIssueRegistry$Factory.get(JarFileIssueRegistry.kt:143)
at com.android.tools.lint.client.api.LintClient.addCustomLintRules(LintClient.kt:1232)
at com.android.tools.lint.Main.getGlobalRegistry(Main.java:883)
at com.android.tools.lint.Main.run(Main.java:399)
at com.android.tools.lint.Main.main(Main.java:131)

i found the latest sdk tools is 26.1.1 https://developer.android.com/studio/releases/sdk-tools

which the version of lint&lint-api&lint-check is 26.0.0-dev

lint.png



ps:i got the same error when i change the version of android gradle plugin to 3.0.0

I would love to hear from you all how your teams/companies do it! Thanks for any advice and insight.








Reply all
Reply to author
Forward
0 new messages