Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions android-studio-4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Sample Lint Checks

This project shows how Android Studio 4 handles packaging of lint rules.

## Lint Check Jar Library

First, there's the lint check implementation itself. That's done in the
"checks" project, which just applies the Gradle "java" plugin, and
that project produces a jar. Note that the dependencies for the lint
check project (other than its testing dependencies) must all be "compileOnly":

dependencies {
compileOnly "com.android.tools.lint:lint-api:$lintVersion"
compileOnly "com.android.tools.lint:lint-checks:$lintVersion"
...

## Lint Check AAR Library

Next, there's a separate Android library project, called "library". This
library doesn't have any code on its own (though it could). However,
in its build.gradle, it specifies this:

dependencies {
lintPublish project(':checks')
}

This tells the Gradle plugin to take the output from the "checks" project
and package that as a "lint.jar" payload inside this library's AAR file.
When that's done, any other projects that depends on this library will
automatically be using the lint checks.

## App Modules

Note that you don't have to go through the extra "library indirection"
if you have a lint check that you only want to apply to one or more
app modules. You can simply include the `lintChecks` dependency as shown
above there as well, and then lint will include these rules when analyzing
the project.

## Lint Version

The lint version of the libraries (specified in this project as the
`lintVersion` variable in build.gradle) should be the same version
that is used by the Gradle plugin.

If the Gradle plugin version is *X*.*Y*.*Z*, then the Lint library
version is *X+23*.*Y*.*Z*.
27 changes: 27 additions & 0 deletions android-studio-4/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
buildscript {
ext {
gradlePluginVersion = '4.0.0-alpha08'
lintVersion = '27.0.0-alpha08'
kotlinVersion = '1.3.61'
}

repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:$gradlePluginVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}

allprojects {
repositories {
google()
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
3 changes: 3 additions & 0 deletions android-studio-4/checks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/build
lint-report.html
lint-results.txt
25 changes: 25 additions & 0 deletions android-studio-4/checks/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apply plugin: 'java-library'
apply plugin: 'kotlin'
apply plugin: 'com.android.lint'

lintOptions {
htmlReport true
htmlOutput file("lint-report.html")
textReport true
absolutePaths false
ignoreTestSources true
}

dependencies {
// For a description of the below dependencies, see the main project README
compileOnly "com.android.tools.lint:lint-api:$lintVersion"
compileOnly "com.android.tools.lint:lint-checks:$lintVersion"
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
testImplementation "junit:junit:4.13"
testImplementation "com.android.tools.lint:lint:$lintVersion"
testImplementation "com.android.tools.lint:lint-tests:$lintVersion"
testImplementation "com.android.tools:testutils:$lintVersion"
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.lint.checks

import com.android.tools.lint.client.api.UElementHandler
import com.android.tools.lint.detector.api.*
import com.android.tools.lint.detector.api.Detector.UastScanner
import org.jetbrains.uast.UElement
import org.jetbrains.uast.ULiteralExpression
import org.jetbrains.uast.evaluateString

/**
* Sample detector showing how to analyze Kotlin/Java code.
* This example flags all string literals in the code that contain
* the word "lint".
*/
@Suppress("UnstableApiUsage")
class SampleCodeDetector : Detector(), UastScanner {
override fun getApplicableUastTypes(): List<Class<out UElement?>>? {
return listOf(ULiteralExpression::class.java)
}

override fun createUastHandler(context: JavaContext): UElementHandler? {
// Note: Visiting UAST nodes is a pretty general purpose mechanism;
// Lint has specialized support to do common things like "visit every class
// that extends a given super class or implements a given interface", and
// "visit every call site that calls a method by a given name" etc.
// Take a careful look at UastScanner and the various existing lint check
// implementations before doing things the "hard way".
// Also be aware of context.getJavaEvaluator() which provides a lot of
// utility functionality.
return object : UElementHandler() {
override fun visitLiteralExpression(node: ULiteralExpression) {
val string = node.evaluateString() ?: return
if (string.contains("lint") && string.matches(Regex(".*\\blint\\b.*"))) {
context.report(ISSUE, node, context.getLocation(node),
"This code mentions `lint`: **Congratulations**")
}
}
}
}

companion object {
/** Issue describing the problem and pointing to the detector implementation */
@JvmField
val ISSUE: Issue = Issue.create(
// ID: used in @SuppressLint warnings etc
id = "ShortUniqueId",
// Title -- shown in the IDE's preference dialog, as category headers in the
// Analysis results window, etc
briefDescription = "Lint Mentions",
// Full explanation of the issue; you can use some markdown markup such as
// `monospace`, *italic*, and **bold**.
explanation = """
This check highlights string literals in code which mentions the word `lint`. \
Blah blah blah.

Another paragraph here.
""", // no need to .trimIndent(), lint does that automatically
category = Category.CORRECTNESS,
priority = 6,
severity = Severity.WARNING,
implementation = Implementation(
SampleCodeDetector::class.java,
Scope.JAVA_FILE_SCOPE))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.lint.checks

import com.android.tools.lint.client.api.IssueRegistry
import com.android.tools.lint.detector.api.CURRENT_API

/*
* The list of issues that will be checked when running <code>lint</code>.
*/
@Suppress("UnstableApiUsage")
class SampleIssueRegistry : IssueRegistry() {
override val issues = listOf(SampleCodeDetector.ISSUE)

override val api: Int
get() = CURRENT_API
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.example.lint.checks.SampleIssueRegistry
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.lint.checks

import com.android.tools.lint.checks.infrastructure.LintDetectorTest
import com.android.tools.lint.detector.api.Detector
import com.android.tools.lint.detector.api.Issue

@Suppress("UnstableApiUsage")
class SampleCodeDetectorTest : LintDetectorTest() {
fun testBasic() {
lint().files(
java("""
package test.pkg;
public class TestClass1 {
// In a comment, mentioning "lint" has no effect
private static String s1 = "Ignore non-word usages: linting";
private static String s2 = "Let's say it: lint";
}
"""
).indented())
.run()
.expect("""
src/test/pkg/TestClass1.java:5: Warning: This code mentions lint: Congratulations [ShortUniqueId]
private static String s2 = "Let's say it: lint";
~~~~~~~~~~~~~~~~~~~~
0 errors, 1 warnings
"""
)
}

override fun getDetector(): Detector {
return SampleCodeDetector()
}

override fun getIssues(): List<Issue> {
return listOf(SampleCodeDetector.ISSUE)
}
}
17 changes: 17 additions & 0 deletions android-studio-4/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
Binary file not shown.
6 changes: 6 additions & 0 deletions android-studio-4/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Fri Sep 01 06:52:38 PDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1-rc-1-all.zip
Loading