Slide 1

Slide 1 text

KeepTruckin Subhrajyoti Sen Droidcon APAC 2020 Building a Better Codebase with Lint

Slide 2

Slide 2 text

What is Lint? < / >

Slide 3

Slide 3 text

What is Lint? • Static analysis tool

Slide 4

Slide 4 text

What is Lint? • Static analysis tool • Open Source

Slide 5

Slide 5 text

What is Lint? • Static analysis tool • Open Source • Works on .java, .kt, .gradle and a lot more

Slide 6

Slide 6 text

What is Lint? • Static analysis tool • Open Source • Works on .java, .kt, .gradle and a lot more • Can be used outside Android as well

Slide 7

Slide 7 text

What is Lint? • Static analysis tool • Open Source • Works on .java, .kt, .gradle and a lot more • Can be used outside Android as well • Can be extended to write custom rules

Slide 8

Slide 8 text

What is Lint? • Static analysis tool • Open Source • Works on .java, .kt, .gradle and a lot more • Can be used outside Android as well • Can be extended to write custom rules • Can be run from both Android Studio and CLI

Slide 9

Slide 9 text

What does Lint look like? < / >

Slide 10

Slide 10 text

Internationalization

Slide 11

Slide 11 text

Security

Slide 12

Slide 12 text

Performance

Slide 13

Slide 13 text

Accessibility

Slide 14

Slide 14 text

Why consider Lint? < / >

Slide 15

Slide 15 text

Why consider Lint? • Opinionated codebase

Slide 16

Slide 16 text

Why consider Lint? • Opinionated codebase • Simpler code reviews

Slide 17

Slide 17 text

Why consider Lint? • Opinionated codebase • Simpler code reviews • Adherence to style guide

Slide 18

Slide 18 text

Setup < / >

Slide 19

Slide 19 text

Create a new module

Slide 20

Slide 20 text

Dependencies dependencies { compileOnly "org.jetbrains.kotlin:kotlin stdlib:$kotlin_version" compileOnly 'com.android.tools.lint:lint api:27.1.1' compileOnly 'com.android.tools.lint:lint checks:27.1.1' testImplementation "com.android.tools.lint:lint tests:27.1.1" } lint/build.gradle

Slide 21

Slide 21 text

Issue Registry class Registry : IssueRegistry() { override val api: Int = CURRENT_API @get:NotNull override val issues: List get() = listOf() }

Slide 22

Slide 22 text

Register Issue Registry jar { manifest { attributes 'Lint-Registry-V2': 'me.subhrajyoti.lint.Registry' } } lint/build.gradle

Slide 23

Slide 23 text

Add dependency on lint module dependencies { lintChecks project(':lint') } app/build.gradle

Slide 24

Slide 24 text

The Basic Elements Scope The kind of files the lint rule applies to • JAVA_FILE_SCOPE • GRADLE_SCOPE • MANIFEST_SCOPE • PROGUARD_SCOPE • RESOURCE_FILE_SCOPE

Slide 25

Slide 25 text

The Basic Elements Severity How severe is the issue • INFORMATIONAL • WARNING • ERROR • FATAL

Slide 26

Slide 26 text

The Basic Elements Category The category the issue falls in, from a pre-defined list of categories • SECURITY • PERFORMANCE • L10N • A11Y

Slide 27

Slide 27 text

Use case - Style Migration V1.TextView.Header1 V2.TextView.Header1 →

Slide 28

Slide 28 text

Custom Rule Structure val ISSUE: Issue = Issue.create( "V1StyleUsageDetector", "Replace V1 styles with V2", "V1 styles have been deprecated and " + "you should use V2 styles for all TextView going forward", Category.CORRECTNESS, 5, Severity.ERROR, Implementation( V1StyleUsageDetector::class.java, Scope.RESOURCE_FILE_SCOPE ) )

Slide 29

Slide 29 text

Custom Rule Structure class V1StyleUsageDetector : ResourceXmlDetector() { }

Slide 30

Slide 30 text

Custom Rule Structure class V1StyleUsageDetector : ResourceXmlDetector() { override fun getApplicableElements(): Collection { return listOf("TextView") } }

Slide 31

Slide 31 text

Custom Rule Structure class V1StyleUsageDetector : ResourceXmlDetector() { override fun getApplicableElements(): Collection { return listOf(SdkConstants.TEXT_VIEW) } }

Slide 32

Slide 32 text

Custom Rule Structure class V1StyleUsageDetector : ResourceXmlDetector() { //.. override fun appliesTo(folderType: ResourceFolderType): Boolean { return folderType == ResourceFolderType.LAYOUT } }

Slide 33

Slide 33 text

Custom Rule Structure class V1StyleUsageDetector : ResourceXmlDetector() { //.. override fun visitElement(context: XmlContext, element: Element) { if (element.hasAttribute(SdkConstants.ATTR_STYLE)) { val attributeValue = element.getAttribute(SdkConstants.ATTR_STYLE) if (attributeValue.startsWith("@style/V1")) { context.report( ISSUE, element, context.getLocation(element), "Replace V1 style with V2") } } }

Slide 34

Slide 34 text

Issue Registry class Registry : IssueRegistry() { override val api: Int = CURRENT_API @get:NotNull override val issues: List get() = listOf(V1StyleUsageDetector.ISSUE) }

Slide 35

Slide 35 text

Lint x val newValue = element.getAttribute(SdkConstants.ATTR_STYLE).replace("V1", "V2") val compositeFix = LintFix.create() .name("Replace V1 with V2") .composite( LintFix.create() .unset(null, SdkConstants.ATTR_STYLE) .build(), LintFix.create() .set(null, SdkConstants.ATTR_STYLE, newValue) .build(), )

Slide 36

Slide 36 text

Lint x context.report( ISSUE, element, context.getLocation(element), "Replace V1 style with V2", compositeFix )

Slide 37

Slide 37 text

Auto x val newValue = element.getAttribute(SdkConstants.ATTR_STYLE).replace("V1", "V2") val compositeFix = LintFix.create() .name("Replace V1 with V2") .composite( LintFix.create() .unset(null, SdkConstants.ATTR_STYLE) .build(), LintFix.create() .set(null, SdkConstants.ATTR_STYLE, newValue) .build(), ) .autofix()

Slide 38

Slide 38 text

Lint x ./gradlew app:lintFix

Slide 39

Slide 39 text

Testing

Slide 40

Slide 40 text

Testing @RunWith(JUnit4 class) class V1StyleUsageDetectorTest : LintDetectorTest() { override fun getIssues(): MutableList = mutableListOf(V1StyleUsageDetector.ISSUE) override fun getDetector(): Detector = V1StyleUsageDetector() }

Slide 41

Slide 41 text

@RunWith(JUnit4 class) class V1StyleUsageDetectorTest : LintDetectorTest() { @Test fun `check no lint error`() { lint() .files( xml( "res/layout/layout.xml", """

Slide 42

Slide 42 text

@Test fun `check exactly 1 lint error`() { lint() .files( xml( "res/layout/layout.xml", """

Slide 43

Slide 43 text

@Test fun `check exactly 1 lint error`() { lint() .files( xml( ).indented() ) .run() .expectErrorCount(1) .expect( """ res/layout/layout.xml:1 Error: Replace V1 style with V2 [V1StyleUsageDetector]

Slide 44

Slide 44 text

Integration

Slide 45

Slide 45 text

Integration • Incremental fixes

Slide 46

Slide 46 text

Integration • Incremental fixes • Using a baseline

Slide 47

Slide 47 text

Baseline android { lintOptions { baseline file("lint baseline.xml") } }

Slide 48

Slide 48 text

Baseline - Pass 1

Slide 49

Slide 49 text

Baseline - Pass 2

Slide 50

Slide 50 text

lint-baseline.xml

Slide 51

Slide 51 text

Performance

Slide 52

Slide 52 text

Performance • Single pass • Missing attribute • Fast • Enable in IDE

Slide 53

Slide 53 text

Performance • Single pass • Missing attribute • Fast • Enable in IDE • Multiple pass • Unused resources • Slow • Disable in IDE

Slide 54

Slide 54 text

Kotlin Considerations?

Slide 55

Slide 55 text

Kotlin Considerations? • No explicit return statements

Slide 56

Slide 56 text

Kotlin Considerations? • No explicit return statements • No explicit type declarations

Slide 57

Slide 57 text

Kotlin Considerations? • No explicit return statements • No explicit type declarations • Write one test case for Kotlin usage

Slide 58

Slide 58 text

References • https://groups.google.com/g/lint-dev • https://github.com/SubhrajyotiSen/LintTalk • https://github.com/vanniktech/lint-rules • https://www.youtube.com/watch?v=p8yX5-lPS6o • https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio- master-dev:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/

Slide 59

Slide 59 text

@iamsubhrajyoti