diff --git a/android-studio-4/README.md b/android-studio-4/README.md new file mode 100644 index 00000000..e025c26a --- /dev/null +++ b/android-studio-4/README.md @@ -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*. diff --git a/android-studio-4/build.gradle b/android-studio-4/build.gradle new file mode 100644 index 00000000..12258f64 --- /dev/null +++ b/android-studio-4/build.gradle @@ -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 +} diff --git a/android-studio-4/checks/.gitignore b/android-studio-4/checks/.gitignore new file mode 100644 index 00000000..6ccc6caa --- /dev/null +++ b/android-studio-4/checks/.gitignore @@ -0,0 +1,3 @@ +/build +lint-report.html +lint-results.txt diff --git a/android-studio-4/checks/build.gradle b/android-studio-4/checks/build.gradle new file mode 100644 index 00000000..592fe5bd --- /dev/null +++ b/android-studio-4/checks/build.gradle @@ -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" diff --git a/android-studio-4/checks/src/main/java/com/example/lint/checks/SampleCodeDetector.kt b/android-studio-4/checks/src/main/java/com/example/lint/checks/SampleCodeDetector.kt new file mode 100644 index 00000000..14603725 --- /dev/null +++ b/android-studio-4/checks/src/main/java/com/example/lint/checks/SampleCodeDetector.kt @@ -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>? { + 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)) + } +} diff --git a/android-studio-4/checks/src/main/java/com/example/lint/checks/SampleIssueRegistry.kt b/android-studio-4/checks/src/main/java/com/example/lint/checks/SampleIssueRegistry.kt new file mode 100644 index 00000000..f4c4f723 --- /dev/null +++ b/android-studio-4/checks/src/main/java/com/example/lint/checks/SampleIssueRegistry.kt @@ -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 lint. + */ +@Suppress("UnstableApiUsage") +class SampleIssueRegistry : IssueRegistry() { + override val issues = listOf(SampleCodeDetector.ISSUE) + + override val api: Int + get() = CURRENT_API +} \ No newline at end of file diff --git a/android-studio-4/checks/src/main/resources/META-INF/services/com.android.tools.lint.client.api.IssueRegistry b/android-studio-4/checks/src/main/resources/META-INF/services/com.android.tools.lint.client.api.IssueRegistry new file mode 100644 index 00000000..fa8542a3 --- /dev/null +++ b/android-studio-4/checks/src/main/resources/META-INF/services/com.android.tools.lint.client.api.IssueRegistry @@ -0,0 +1 @@ +com.example.lint.checks.SampleIssueRegistry diff --git a/android-studio-4/checks/src/test/java/com/example/lint/checks/SampleCodeDetectorTest.kt b/android-studio-4/checks/src/test/java/com/example/lint/checks/SampleCodeDetectorTest.kt new file mode 100644 index 00000000..d5427cb7 --- /dev/null +++ b/android-studio-4/checks/src/test/java/com/example/lint/checks/SampleCodeDetectorTest.kt @@ -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 { + return listOf(SampleCodeDetector.ISSUE) + } +} \ No newline at end of file diff --git a/android-studio-4/gradle.properties b/android-studio-4/gradle.properties new file mode 100644 index 00000000..aac7c9b4 --- /dev/null +++ b/android-studio-4/gradle.properties @@ -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 diff --git a/android-studio-4/gradle/wrapper/gradle-wrapper.jar b/android-studio-4/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..13372aef Binary files /dev/null and b/android-studio-4/gradle/wrapper/gradle-wrapper.jar differ diff --git a/android-studio-4/gradle/wrapper/gradle-wrapper.properties b/android-studio-4/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..0755eca4 --- /dev/null +++ b/android-studio-4/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/android-studio-4/gradlew b/android-studio-4/gradlew new file mode 100755 index 00000000..9d82f789 --- /dev/null +++ b/android-studio-4/gradlew @@ -0,0 +1,160 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/android-studio-4/gradlew.bat b/android-studio-4/gradlew.bat new file mode 100644 index 00000000..8a0b282a --- /dev/null +++ b/android-studio-4/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/android-studio-4/library/.gitignore b/android-studio-4/library/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/android-studio-4/library/.gitignore @@ -0,0 +1 @@ +/build diff --git a/android-studio-4/library/build.gradle b/android-studio-4/library/build.gradle new file mode 100644 index 00000000..2261372a --- /dev/null +++ b/android-studio-4/library/build.gradle @@ -0,0 +1,18 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 29 + defaultConfig { + minSdkVersion 15 + targetSdkVersion 29 + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} + +/** Package the given lint checks library into this AAR */ +dependencies { + lintPublish project(':checks') +} diff --git a/android-studio-4/library/src/main/AndroidManifest.xml b/android-studio-4/library/src/main/AndroidManifest.xml new file mode 100644 index 00000000..843049f7 --- /dev/null +++ b/android-studio-4/library/src/main/AndroidManifest.xml @@ -0,0 +1,3 @@ + + + diff --git a/android-studio-4/settings.gradle b/android-studio-4/settings.gradle new file mode 100644 index 00000000..b613efff --- /dev/null +++ b/android-studio-4/settings.gradle @@ -0,0 +1 @@ +include ':checks', ':library'