### Lint Warning Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/LocalContextConfigurationRead.md.html
Shows examples of lint warnings for reading configuration using LocalContext.current.resources.configuration.
```text
src/test/test.kt:11:Error: Reading Configuration using LocalContext.current.resources.configuration [LocalContextConfigurationRead]
LocalContext.current.resources.configuration
--------------------------------------------
src/test/test.kt:12:Error: Reading Configuration using LocalContext.current.resources.configuration [LocalContextConfigurationRead]
LocalContext.current.getResources().getConfiguration()
------------------------------------------------------
```
--------------------------------
### Basic Lint Rule Implementation
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/IdleBatteryChargingConstraints.md.html
Example of a simple custom lint rule that checks for a specific pattern. Requires setup in a separate module.
```java
package com.example.lint;
import com.android.tools.lint.client.api.IssueRegistry;
import com.android.tools.lint.detector.api.Issue;
import com.android.tools.lint.detector.api.Severity;
public class MyIssueRegistry extends IssueRegistry {
@Override
public Issue[] getIssues() {
return new Issue[] {
MyCustomIssue.ISSUE
};
}
}
class MyCustomIssue {
static final Issue ISSUE = Issue.create(
"MyCustomRule", // ID
"Avoid using hardcoded strings", // Title
"Hardcoding strings is discouraged. Use string resources instead.", // Message
Category.I18N, // Category
6, // Priority
Severity.WARNING, // Severity
new Implementation(MyDetector.class, Scope.RESOURCE_FILE_SCOPE));
}
import com.android.tools.lint.detector.api.Detector;
import com.android.tools.lint.detector.api.JavaContext;
import com.android.tools.lint.detector.api.XmlContext;
import com.android.tools.lint.detector.api.TextRange;
import com.android.tools.lint.detector.api.Location;
import com.android.resources.ResourceFolderType;
import org.jetbrains.uast.UElement;
import org.jetbrains.uast.UStringLiteral;
public class MyDetector extends Detector implements Detector.UastScanner {
@Override
public void visitLiteralExpression(JavaContext context, UStringLiteral literal) {
if (literal.getValue().startsWith("Hardcoded")) {
context.report(MyCustomIssue.ISSUE, literal, context.getLocation(literal), "This is a hardcoded string.");
}
}
}
```
--------------------------------
### Kotlin Lint Detector Test Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/LintImplUnexpectedDomain.md.html
Demonstrates how to write a unit test for a custom Kotlin lint detector using LintDetectorTest. Requires setup with lint().files().run().expect().
```kotlin
package test.pkg
import com.android.tools.lint.checks.infrastructure.LintDetectorTest
import com.android.tools.lint.detector.api.Detector
class MyKotlinLintDetectorTest : LintDetectorTest() {
override fun getDetector(): Detector {
return MyKotlinLintDetector()
}
fun testBasic() {
val expected = """
src/test/pkg/AlarmTest.java:9: Warning: Value will be forced up to 5000 as of Android 5.1; don't rely on this to be exact [ShortAlarm] alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 50, 10, null); // ERROR ~~ 0 errors, 1 warnings """.trimIndent()
lint().files(
kotlin(
"""
fun test() {
println("Value=${'$'}")
}
""".trimIndent()
),
java(
"src/test/pkg/AlarmTest.java",
"""
package test.pkg;
import android.app.AlarmManager;
@SuppressWarnings("ClassNameDiffersFromFileName")
public class AlarmTest {
public void test(AlarmManager alarmManager) {
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 5000, 60000, null); // OK
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 6000, 70000, null); // OK
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 50, 10, null); // ERROR
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, 5000, // ERROR
OtherClass.MY_INTERVAL, null); // ERROR
}
private static class OtherClass {
public static final long MY_INTERVAL = 1000L;
}
}
"""
)
).run().expect(expected)
}
}
```
--------------------------------
### Kotlin AST Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/api-guide.html
A simple Kotlin program equivalent to the Java example, used to illustrate AST representation.
```kotlin
// MyTest.kt
package test.pkg
class MyTest {
val s: String = "hello"
}
```
--------------------------------
### Example of ComposeNamingLowercase lint warning
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/ComposeNamingLowercase.md.html
This example shows a typical lint warning generated when a composable function that returns a value starts with an uppercase letter.
```text
src/test.kt:4:Error: Composable functions that return a value should start with a lowercase letter.While useful and accepted outside of @Composable functions, this factory function convention has drawbacks that set inappropriate expectations for callers when used with @Composable functions.See https://slackhq.github.io/compose-lints/rules/#naming-composable-functions-properly for more information. [ComposeNamingLowercase] fun MyComposable(): Something { }
```
```kotlin
import androidx.compose.runtime.Composable
@Composable
fun MyComposable(): Something {
}
```
--------------------------------
### Gradle Build File Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/GradleDynamicVersion.md.html
This is a sample Gradle build file demonstrating the usage of dependencies, including a dynamic version for 'appcompat-v7'.
```groovy
apply plugin: 'android'
android {
compileSdkVersion buildToolsVersion
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 17
versionCode 1
versionName "1.0"
}
productFlavors {
free {
}
pro {
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
freeCompile 'com.google.guava:guava:11.0.2'
compile 'com.android.support:appcompat-v7:13.0.0'
compile 'com.google.android.support:wearable:1.2.0'
compile 'com.android.support:multidex:1.0.0'
androidTestCompile 'com.android.support.test:runner:0.3'
}
```
--------------------------------
### Example Lint Warnings for Uppercase Naming
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/ComposeNamingUppercase.md.html
These examples show the errors generated by the ComposeNamingUppercase lint rule when composable functions do not start with an uppercase letter.
```text
src/test.kt:4:Error: Composable functions that return Unit should start with an uppercase letter.They are considered declarative entities that can be either present or absent in a composition and therefore follow the naming rules for classes.See https://slackhq.github.io/compose-lints/rules/#naming-composable-functions-properly for more information. [ComposeNamingUppercase]
fun myComposable() { }
------------
src/test.kt:7:Error: Composable functions that return Unit should start with an uppercase letter.They are considered declarative entities that can be either present or absent in a composition and therefore follow the naming rules for classes.See https://slackhq.github.io/compose-lints/rules/#naming-composable-functions-properly for more information. [ComposeNamingUppercase]
fun myComposable(): Unit { }
------------
```
--------------------------------
### Add Startup Runtime Dependency with Version Catalogs (build.gradle.kts)
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/androidx_startup_startup-runtime.md.html
Integrate the startup-runtime library using Gradle version catalogs for better dependency management. Define the version and library in your libs.versions.toml file.
```kotlin
implementation(libs.startup.runtime)
```
--------------------------------
### Base String Resource Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/StringFormatCount.md.html
This is the base string resource file defining the 'hello' string with two formatting arguments. Ensure all translated strings match this argument count.
```xml
Hello %1$s, %2$s?
```
--------------------------------
### Example of Wrong Layout Name Warning
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/WrongLayoutName.md.html
This example shows the output of the WrongLayoutName lint check, indicating a warning for a layout file that does not start with a required prefix.
```text
res/layout/random.xml:Warning: Layout does not start with one of the following prefixes: activity_, view_, fragment_, dialog_, bottom_sheet_, adapter_item_, divider_, space_, popup_window_ [WrongLayoutName]
0 errors, 1 warnings
```
--------------------------------
### AppCompat Method Lint Warnings Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/AppCompatMethod.md.html
Examples of warnings generated by the AppCompatMethod lint check for incorrect method calls.
```text
src/test/pkg/AppCompatTest.java:5:Warning: Should use getSupportActionBar instead of getActionBar name [AppCompatMethod]
getActionBar(); // ERROR
--------------
```
```text
src/test/pkg/AppCompatTest.java:8:Warning: Should use startSupportActionMode instead of startActionMode name [AppCompatMethod]
startActionMode(null); // ERROR
```
```text
src/test/pkg/AppCompatTest.java:11:Warning: Should use supportRequestWindowFeature instead of requestWindowFeature name [AppCompatMethod]
requestWindowFeature(0); // ERROR
```
```text
src/test/pkg/AppCompatTest.java:14:Warning: Should use setSupportProgressBarVisibility instead of setProgressBarVisibility name [AppCompatMethod]
setProgressBarVisibility(true); // ERROR
```
```text
src/test/pkg/AppCompatTest.java:15:Warning: Should use setSupportProgressBarIndeterminate instead of setProgressBarIndeterminate name [AppCompatMethod]
setProgressBarIndeterminate(true); // ERROR
```
```text
src/test/pkg/AppCompatTest.java:16:Warning: Should use setSupportProgressBarIndeterminateVisibility instead of setProgressBarIndeterminateVisibility name [AppCompatMethod]
setProgressBarIndeterminateVisibility(true); // ERROR
```
--------------------------------
### Example Layout File
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/MissingDefaultResource.md.html
An example of a land-specific layout file using the merge tag. This file is part of the lint rule tests.
```xml
```
--------------------------------
### Source File with Instant App Call
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/InstantAppCall.md.html
The source file `src/com/example/app/MyApp.kt` demonstrating the usage of `InstantApps.showInstallPrompt`, which triggers the InstantAppCall lint warning.
```kotlin
package com.example.app
import android.app.Activity
import com.google.android.gms.instantapps.InstantApps
class MyApp {
fun go(activity: Activity) {
InstantApps.showInstallPrompt(activity, null, 0, null)
}
}
```
--------------------------------
### Example of SuperfluousMarginDeclaration Warning
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/SuperfluousMarginDeclaration.md.html
This example shows how the lint warning appears in the IDE, indicating that `layout_margin` should be used instead of individual start, end, top, and bottom margins.
```text
res/layout/ids.xml:1:Warning: Should be using layout_margin instead. [SuperfluousMarginDeclaration]
```
--------------------------------
### XML Resource Violation Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/ResourceName.md.html
This example shows a layout XML file where the resource name 'activity_main' does not start with the configured project resource prefix 'unit_test_prefix_'. The lint check flags this as an error.
```xml
```
--------------------------------
### Base Resource Definitions (strings.xml)
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/DuplicateDefinition.md.html
This XML file contains the initial definitions for various strings, including 'wallpaper_instructions'. Ensure resources are not duplicated within the same folder.
```xml
Home SampleAllWallpaperSearchSettingsIgnore MeTap picture to set portrait wallpaper
```
--------------------------------
### Alternatives Quick Fix Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/api-guide.html
Create an alternatives fix to offer users multiple distinct options for resolving an issue. This example provides choices for setting a content description attribute.
```kotlin
val fix = fix().alternatives(
fix().set().todo(ANDROID_URI, "text").build(),
fix().set().todo(ANDROID_URI, "contentDescription")
.build())
```
--------------------------------
### Example of Invalid Navigation XML
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/InvalidNavigation.md.html
This XML snippet demonstrates a navigation graph with an invalid start destination, triggering the InvalidNavigation lint warning. Ensure the start destination is a direct child of the navigation element.
```xml
```
--------------------------------
### Example of UnlocalizedSms Lint Warning
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/UnlocalizedSms.md.html
This example shows a lint warning generated when an SMS destination number does not start with a country code. It suggests prefixing with '+' and a country code or restricting usage to a specific country.
```text
src/test/pkg/NonInternationalizedSmsDetectorTest.java:18:Warning: To make sure the SMS can be sent by all users, please start the SMS number with a + and a country code or restrict the code invocation to people in the country you are targeting [UnlocalizedSms]
sms.sendMultipartTextMessage("001234567890", null, null, null, null);
--------------
```
--------------------------------
### Create Quickfix with Alternatives
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/api-guide/quickfixes.md.html
Use 'alternatives' to offer the user multiple distinct quickfix options. This is useful when a problem can be solved in several ways.
```kotlin
val fix = fix().alternatives( fix().set().todo(ANDROID_URI, "text").build(), fix().set().todo(ANDROID_URI, "contentDescription") \
.build())
```
--------------------------------
### Example build.gradle File with Deprecated Configurations
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/GradleDeprecatedConfiguration.md.html
A sample build.gradle file demonstrating the use of deprecated configurations 'compile' and 'debugCompile'.
```groovy
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
}
apply plugin: 'com.android.library'
dependencies {
compile 'androidx.appcompat:appcompat:1.0.0'
debugCompile 'androidx.appcompat:appcompat:1.0.0'
}
```
--------------------------------
### Example of CompositionLocal getters flagged as errors
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/ComposeCompositionLocalGetter.md.html
This example demonstrates how the lint check identifies CompositionLocals defined with getters. It shows two cases: one using `get()` and another using a block body for the getter, both of which are flagged.
```text
src/test.kt:2:Error: `CompositionLocal`s should be singletons and not use getters. Otherwise a new instance will be returned every call. [ComposeCompositionLocalGetter]
val LocalBanana get() = compositionLocalOf { "Prune" }
src/test.kt:3:Error: `CompositionLocal`s should be singletons and not use getters. Otherwise a new instance will be returned every call. [ComposeCompositionLocalGetter]
val LocalPotato get() {
```
--------------------------------
### Gradle Build File Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/UseTomlInstead.md.html
This Gradle build file demonstrates the correct way to declare dependencies using the TOML version catalog.
```groovy
dependencies {
implementation(libs.androidx.appCompat) // OK
implementation 'androidx.appcompat:appcompat:1.5.1'
}
```
--------------------------------
### ModifierFactoryUnreferencedReceiver Lint Error Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/ModifierFactoryUnreferencedReceiver.md.html
This example shows the lint warning produced when a Modifier factory function does not use the receiver Modifier instance. Ensure the returned chain includes 'this' or starts with an implicit call to another factory function.
```kotlin
src/androidx/compose/ui/foo/TestModifier.kt:8:Error: Modifier factory functions must use the receiver Modifier instance [ModifierFactoryUnreferencedReceiver]
fun Modifier.fooModifier(): Modifier.Element {
-----------
```
--------------------------------
### Groovy Build Script Example for TargetSdkVersion
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/ExpiringTargetSdkVersion.md.html
An example of a `build.gradle` file demonstrating how `targetSdkVersion` is set. It includes comments indicating compliance with current and future requirements.
```groovy
apply plugin: 'com.android.application'
android {
defaultConfig {
// Already meeting last year's requirement but not this year's requirement
targetSdkVersion 31
targetSdkVersion 2023 // OK
targetSdkVersion 2024 // OK
}
}
```
--------------------------------
### LintFix for Creating and Deleting Files
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/api-guide.html
Quickfixes can now create and delete new files using `LintFix#newFile` and `LintFix#deleteFile`.
```java
LintFix.newFile(filePath, content).build()
LintFix.deleteFile(filePath).build()
```
--------------------------------
### Lint Error Example: Use Require Instead of Get
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/UseRequireInsteadOfGet.md.html
Illustrates common lint warnings for using 'checkNotNull(fragment.get ())' instead of the preferred 'fragment.require ()' methods.
```java
checkNotNull(fragment.getArguments());
```
```java
checkNotNull(fragment.getFragmentManager());
```
```java
checkNotNull(fragment.getContext());
```
```java
checkNotNull(fragment.getActivity());
```
```java
checkNotNull(fragment.getHost());
```
```java
checkNotNull(fragment.getParentFragment());
```
```java
checkNotNull(fragment.getView());
```
--------------------------------
### Correct Java Bean property accessors
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/KotlinPropertyAccess.md.html
Examples of correctly named Java Bean accessor methods that can be accessed as properties in Kotlin. Includes 'get' and 'is' prefixes.
```java
public void setOk1(String s) {
}
public String getOk1() {
return "";
}
```
```java
public void setOk2(String s) {
}
public String isOk2() {
return "";
}
public String getOk2() {
return "";
}
```
--------------------------------
### Full Source File Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/RetainLeaksContext.md.html
This is the complete source file demonstrating the usage of `retain` with a `Context`, which triggers the `RetainLeaksContext` lint warning.
```kotlin
package androidx.compose.runtime.foo
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.view.View
import android.view.TextView
import androidx.compose.runtime.Composable
import androidx.compose.runtime.retain.retain
@Composable
fun Test(context: Context) {
val foo = retain { context }
}
```
--------------------------------
### Read Option Value from Context
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/api-guide/options.md.html
Retrieve the configured value of an option within the lint analysis context. This example shows how to get the integer value for the `MAX_COUNT` option.
```kotlin
val maxCount = MAX_COUNT.getValue(context)
```
--------------------------------
### Version Catalog TOML File Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/SimilarGradleDependency.md.html
An example of a Gradle version catalog file (`libs.versions.toml`) that demonstrates the declaration of multiple versions for the 'joda-time:joda-time' dependency.
```toml
[versions]
jodaVersion = "2.1"
dagger="1.2.0"
[libraries]
joda_library = { module = "joda-time:joda-time", version.ref = "jodaVersion"}
joda_library2 = { module = "joda-time:joda-time", version = "2.0" }
dagger-lib = { group = "com.squareup.dagger", name ="dagger", version.ref = "dagger" }
```
--------------------------------
### build.gradle.kts Configuration Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/MinSdkTooLow.md.html
This snippet shows a typical build.gradle.kts file configuration where the minSdkVersion is set using version catalog references.
```kotlin
android {
compileSdk = libs.versions.compile.sdk.version.get().toInt() // ERROR 12
compileSdk = libs.versions.keys.csv.get().toInt() // ERROR 13
defaultConfig {
minSdk = libs.versions.keys.msv.get().toInt() // ERROR 14
targetSdk = libs.versions.keys.tsv.get().toInt() // ERROR 15
}
}
```
--------------------------------
### Example of Navigation XML with Included Graph
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/InvalidNavigation.md.html
This XML snippet shows an included navigation graph. The lint check analyzes these structures to ensure valid start destinations are defined.
```xml
```
--------------------------------
### Layout with RTL Attributes Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/RtlEnabled.md.html
This layout file demonstrates the use of RTL attributes like `layout_gravity="left"` and `gravity="right"` which trigger the lint warning if RTL support is not enabled in the manifest. It also shows examples of correct usage with `start` and `end` attributes, and how to suppress the warning.
```xml
```
--------------------------------
### Example build.gradle with library compliance checks
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/PlaySdkIndexNonCompliant.md.html
This snippet demonstrates various library declarations in a build.gradle file, highlighting different compliance statuses (OK, Outdated, Critical, Policy issues, Vulnerabilities) and their blocking or non-blocking nature.
```groovy
compile 'com.example.ads.third.party:example:7.1.9' // Policy (multiple types), no severity
```
```groovy
compile 'log4j:log4j:1.2.18' // OK, latest
```
```groovy
compile 'log4j:log4j:1.2.17' // OK
```
```groovy
compile 'log4j:log4j:1.2.16' // Critical NON_BLOCKING
```
```groovy
compile 'log4j:log4j:1.2.15' // Outdated NON_BLOCKING
```
```groovy
compile 'log4j:log4j:1.2.14' // Non compliant
```
```groovy
compile 'log4j:log4j:1.2.13' // Critical BLOCKING
```
```groovy
compile 'log4j:log4j:1.2.12' // OUTDATED BLOCKING
```
```groovy
compile 'log4j:log4j:1.2.11' // Ok (not in Index)
```
```groovy
compile 'com.example.ads.third.party:example:8.0.0' // OK
```
```groovy
compile 'com.example.ads.third.party:example:7.2.2' // OK
```
```groovy
compile 'com.example.ads.third.party:example:7.2.1' // OK
```
```groovy
compile 'com.example.ads.third.party:example:7.2.0' // Outdated + Critical + Policy (multiple issues), no severity
```
```groovy
compile 'com.example.ads.third.party:example:7.1.0' // Policy (Ads), non-blocking
```
```groovy
compile 'com.example.ads.third.party:example:7.1.1' // Policy (Device and Network Abuse), blocking
```
```groovy
compile 'com.example.ads.third.party:example:7.1.2' // Policy (Deceptive Behavior), no severity
```
```groovy
compile 'com.example.ads.third.party:example:7.1.3' // Policy (User Data), non-blocking
```
```groovy
compile 'com.example.ads.third.party:example:7.1.4' // Policy (Permissions), blocking
```
```groovy
compile 'com.example.ads.third.party:example:7.1.5' // Policy (Mobile Unwanted Software), no-severity
```
```groovy
compile 'com.example.ads.third.party:example:7.1.6' // Policy (Malware), non-blocking
```
```groovy
compile 'com.example.ads.third.party:example:7.1.7' // Policy (multiple types), non-blocking
```
```groovy
compile 'com.example.ads.third.party:example:7.1.8' // Policy (multiple types), blocking
```
```groovy
compile 'com.example.ads.third.party:example:7.1.10' // Vulnerability (UNSAFE_HOSTNAME_VERIFIER, non-blocking)
```
```groovy
compile 'com.example.ads.third.party:example:7.1.11' // Vulnerability multiple (UNSAFE_SSL_ERROR_HANDLER, ZIP_PATH_TRAVERSAL, UNSAFE_WEBVIEW_OAUTH, blocking)
```
```groovy
compile 'com.example.ads.third.party:example:7.1.12' // Vulnerability multiple (non-blocking)
```
```groovy
compile 'com.example.issues:issues-on-latest:1.8.0' // Outdated blocking
```
```groovy
compile 'com.example.issues:latest-is-preview:1.0.0' // Outdated non-blocking
```
```groovy
compile 'com.example.issues:deprecated:2.0.0' // Deprecated
```
```groovy
compile 'log4j:log4j:latest.release' // OK
```
```groovy
compile 'log4j:log4j' // OK
```
```groovy
compile 'log4j:log4j:_'
```
```groovy
compile 'com.another.example:example' // Ok (not in Index)
```
--------------------------------
### Creating API Stubs for Binary Analysis
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/api-guide/unit-testing.md.html
Demonstrates how to create API stubs for libraries using `mavenLibrary` and `java` to provide necessary definitions for lint analysis. This example includes a stub for `DiffUtil`.
```kotlin
fun testIdentityEqualsOkay() {
lint().files(
kotlin("/*test contents here */using* some recycler view APIs").indented(),
mavenLibrary("androidx.recyclerview:recyclerview:1.0.0",
java(
"""
package androidx.recyclerview.widget;
public class DiffUtil {
public abstract static class ItemCallback {
public abstract boolean areItemsTheSame(T oldItem, T newItem);
public abstract boolean areContentsTheSame(T oldItem, T newItem);
}
}
""" ).indented()
)
).run().expect(
```
--------------------------------
### Library Consumer Rules with Global Option
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/GlobalOptionInConsumerRules.md.html
This is an example of a library's consumer rules file (proguard.pro) that contains a global option (-dontoptimize) which is not supported in library builds starting with Android Gradle Plugin 9.0.
```text
-dontoptimize
```
--------------------------------
### libs.versions.toml for Startup Runtime
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/androidx_startup_startup-runtime.md.html
Define the startup-runtime version and library alias in your TOML file for use with version catalogs. Ensure the module and version reference are on a single line.
```toml
[versions]
startup-runtime = "1.2.0"
[libraries]
# For clarity and text wrapping purposes the following declaration is
# shown split up across lines, but in TOML it needs to be on a single
# line (see https://github.com/toml-lang/toml/issues/516) so adjust
# when pasting into libs.versions.toml:
startup-runtime = { module = "androidx.startup:startup-runtime", version.ref = "startup-runtime" }
```
--------------------------------
### Example Lint Warning for DeepLinkInActivityDestination
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/DeepLinkInActivityDestination.md.html
This warning indicates that a deep link is incorrectly attached to an activity destination. Attach the deep link directly to the second activity or the start destination of a nav host in the second activity instead.
```xml
```
--------------------------------
### Sample lint.xml Configuration
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/usage/lintxml.md.html
This XML file demonstrates various features of lint configuration, including ignoring specific issues, using path and regular expression matching, and specifying tool-specific rules. It covers severities, ignore paths, and options for detectors.
```xml
```
--------------------------------
### Correct StartDestination Usage in NavController
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/WrongStartDestinationType-2.md.html
Demonstrates correct usage of `startDestination` in `navController.createGraph`. For classes with arguments, pass an instance. For classes without arguments, use a KClass reference.
```kotlin
navController.createGraph(startDestination = TestClass) {}
```
```kotlin
navController.createGraph(startDestination = TestClassWithArg) {}
```
```kotlin
navController.createGraph(startDestination = Outer.InnerClass) {}
```
```kotlin
navController.createGraph(startDestination = InterfaceChildClass) {}
```
```kotlin
navController.createGraph(startDestination = AbstractChildClass) {}
```
```kotlin
navController.createGraph(startDestination = TestInterface) {}
```
```kotlin
navController.createGraph(startDestination = TestAbstract) {}
```
```kotlin
navController.createGraph(startDestination = TestClassComp) {}
```
--------------------------------
### XML Layout with Hardcoded Left/Right Attributes
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/RtlHardcoded.md.html
This XML snippet demonstrates the incorrect usage of 'left' and 'right' for layout_gravity and gravity attributes, which will trigger the RtlHardcoded lint warning. It also shows examples of correct 'start' and 'end' usage.
```xml
```
--------------------------------
### XML Navigation File Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/DeepLinkInActivityDestination.md.html
This XML snippet shows a navigation graph where a deep link is incorrectly attached to an activity destination. The correct approach is to attach deep links directly to the activity or the start destination of a nav host within that activity.
```xml
```
--------------------------------
### Add AppCompat Dependency with Version Catalogs
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/UseCompatLoadingForDrawables.md.html
This example shows how to add the AppCompat dependency using version catalogs in build.gradle.kts. Remember to define the version and library in your libs.versions.toml file.
```gradle
implementation(libs.appcompat) # libs.versions.toml
[versions]
appcompat = "1.7.1"
[libraries]
# For clarity and text wrapping purposes the following declaration is
# shown split up across lines, but in TOML it needs to be on a single
# line (see https://github.com/toml-lang/toml/issues/516) so adjust
# when pasting into libs.versions.toml:
appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
```
--------------------------------
### Get PsiClass from PsiType and vice versa
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/api-guide.html
Use `evaluator.getTypeClass` to get the PsiClass from a PsiType, and `getClassType` to get the PsiClassType from a PsiClass. Ensure JavaEvaluator is obtained first.
```java
abstract fun getClassType(psiClass: PsiClass?): PsiClassType?
abstract fun getTypeClass(psiType: PsiType?): PsiClass?
```
--------------------------------
### Gradle Build Script Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/GradlePath.md.html
An example of a Gradle build script demonstrating the incorrect usage of file paths that would trigger the GradlePath lint check.
```groovy
apply plugin: 'com.android.application'
dependencies {
compile files('my\\libs\\http.jar')
compile files('/libs/android-support-v4.jar')
}
```
--------------------------------
### Source File Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/ComposeUnstableReceiver.md.html
This is the source Kotlin file that demonstrates the usage of composable functions with potentially unstable receivers, triggering the lint warnings.
```kotlin
import androidx.compose.runtime.Composable
interface ExampleInterface {
@Composable
fun Content()
}
class Example {
@Composable
fun Content() {}
}
@Composable
fun Example.OtherContent() {}
// Supertypes
interface Presenter {
@Composable
fun present()
}
class HomePresenter : Presenter {
@Composable
override fun present() {
println("hi")
}
}
```
--------------------------------
### Moshi Collection Type Lint Warning Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/MoshiUsageNonMoshiClassCollection.md.html
This example demonstrates lint warnings for concrete collection types not natively supported by Moshi. It highlights 'ArrayList' and 'HashSet' as examples.
```text
src/slack/model/Example.kt:25:Hint: Concrete Collection type 'ArrayList' is not natively supported by Moshi. [MoshiUsageNonMoshiClassCollection]
val concreteList: ArrayList,
--------------
src/slack/model/Example.kt:26:Hint: Concrete Collection type 'HashSet' is not natively supported by Moshi. [MoshiUsageNonMoshiClassCollection]
val concreteSet: HashSet,
------------
```
--------------------------------
### Example of a valid Gradle settings file
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/ExpiredTargetSdkVersion.md.html
A minimal Gradle settings file configuration. This file is relevant in the context of lint checks related to build configurations.
```gradle
android {
}
```
--------------------------------
### Lint Warning for Missing Documentation Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/LintDocExample.md.html
This is an example of a lint warning indicating that a documentation example test is missing. It specifies the file, line number, and the specific lint check that was violated.
```text
src/test/pkg/MyKotlinLintDetectorTest.kt:10:Warning: Expected to also find a documentation example test (testDocumentationExample) which shows a simple, typical scenario which triggers the test, and which will be extracted into lint's per-issue documentation pages [LintDocExample] fun testBasic() {
^
```
--------------------------------
### libs.versions.toml Configuration Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/MinSdkTooLow.md.html
This TOML file defines version catalog entries, including minSdkVersion, which can be flagged by the lint check.
```toml
[versions]
compile_sdk_version = "34" # ERROR 1
min_sdk_version = "15" # ERROR 2
target_sdk_version = "34" # ERROR 3
compileSdkVersion = "34" # ERROR 4
minSdkVersion = "15" # ERROR 5
targetSdkVersion = "34" # ERROR 6
compileSdk = "34" # ERROR 7
minSdk = "15" # ERROR 8
targetSdk = "34" # ERROR 9
# https://github.com/Kotlin/multiplatform-library-template/blob/main/gradle/libs.versions.toml
android-minSdk = "15" # ERROR 10
android-compileSdk = "34" # ERROR 11
# Unusual keys, referenced via KTS
keys-csv = "34" # ERROR 12
keys-msv = "15" # ERROR 13
keys-tsv = "34" # ERROR 14
javaCompileSdk = "17" # OK 1
other-compileSdk = "15" # OK 2
```
--------------------------------
### Example Lint Warning
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/SpecifyJobSchedulerIdRange.md.html
This is an example of the warning produced by the SpecifyJobSchedulerIdRange lint check.
```text
com/example/TestJobService.kt:5:Warning: Specify a valid range of job id's for WorkManager to use. [SpecifyJobSchedulerIdRange]
class TestJobService: JobService()
--------------
```
--------------------------------
### Example Lint Warning
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/MergeRootFrame.md.html
This is an example of the warning produced by the MergeRootFrameLayoutDetector lint check.
```text
res/layout/simple.xml:1:Warning: This can be replaced with a tag [MergeRootFrame]
----------------------------------------------------
```
--------------------------------
### Create a new file with LintFix
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/api-guide/changes.md.html
Use `LintFix.newFile()` to create new files as part of a quickfix.
```java
LintFix.newFile()
```
--------------------------------
### Example Lint Violation
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/AlertDialogUsage.md.html
This example shows how the AlertDialogUsage lint warning appears in the IDE.
```text
src/Test.java:4:Warning: Should not be using android.app.AlertDialog [AlertDialogUsage]
public Test(AlertDialog dialog) { }
------------------
```
--------------------------------
### EdgeContentLayout Initialization in Kotlin
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/ProtoLayoutEdgeContentLayoutResponsive.md.html
Shows how to initialize EdgeContentLayout with and without responsive content enabled in Kotlin. The `null` argument for DeviceParameters is a placeholder.
```kotlin
package foo
import androidx.wear.protolayout.material.layouts.EdgeContentLayout
val layout = EdgeContentLayout.Builder(null)
.setResponsiveContentInsetEnabled(false)
.build()
```
--------------------------------
### TOML Version Catalog Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/UseTomlInstead.md.html
This TOML file defines versions and libraries, which should be referenced in the Gradle build files.
```toml
[versions]
appCompat = "1.5.1"
androidxTest = "1.5.0"
[libraries]
androidx-appCompat = { module = "androidx.appcompat:appcompat", version.ref = "appCompat" }
androidx-test-core = { module = "androidx.test:core", version.ref = "androidxTest" }
```
--------------------------------
### Example Incident Report
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/api-guide/terminology.md.html
An example of a specific occurrence of a lint issue at a particular location.
```text
Warning: In file IoUtils.kt, line 140, the field download folder is "/sdcard/downloads"; do not hardcode the path to `/sdcard`.
```
--------------------------------
### Gradle build file example with KTX suggestion
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/KtxExtensionAvailable.md.html
This example shows a lint warning for a Gradle dependency that could be replaced with its KTX equivalent. It highlights the need to add the '-ktx' suffix to library names for Kotlin extensions.
```text
build.gradle:7:Hint: Add suffix -ktx to enable the Kotlin extensions for this library [KtxExtensionAvailable] implementation "androidx.core:core:1.2.0"
```
--------------------------------
### Example Lint Warning
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/ConstraintLayoutToolsEditorAttribute.md.html
This is an example of the warning produced by the lint check when `tools:layout_editor_absoluteX` is used.
```xml
res/layout/layout.xml:3:Warning: Don't use tools:layout_editor_absoluteX [ConstraintLayoutToolsEditorAttribute]
tools:layout_editor_absoluteX="4dp"/>
```
--------------------------------
### Kotlin File Example
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/InvalidFragmentVersionForActivityResult.md.html
This Kotlin file demonstrates the usage of ActivityResultCaller and ActivityResultContract, which requires Fragment version 1.3.0 or higher.
```kotlin
package com.example
import androidx.activity.result.ActivityResultCaller
import androidx.activity.result.contract.ActivityResultContract
val launcher = ActivityResultCaller().registerForActivityResult(ActivityResultContract())
```
--------------------------------
### Example Lint Warning
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/UseCompatLoadingForColorStateLists.md.html
This is an example of the warning produced by the UseCompatLoadingForColorStateLists lint check when Resources.getColorStateList() is called directly.
```text
com/example/CustomActivity.kt:8:Warning: Use ContextCompat.getColorStateList() [UseCompatLoadingForColorStateLists]
getResources().getColorStateList(R.color.color_state_list)
----------------------------------------------------------
```
--------------------------------
### Including Fragment Testing Library with Version Catalogs (build.gradle.kts)
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/FragmentGradleConfiguration.md.html
Configure the fragment-testing library using version catalogs in `build.gradle.kts`. This requires defining the version and library in `libs.versions.toml`.
```kotlin
implementation(libs.fragment.testing)
```
--------------------------------
### Example Lint Warning
Source: https://github.com/googlesamples/android-custom-lint-rules/blob/main/docs/checks/StringNotCapitalized.md.html
This is an example of a lint warning produced by the StringNotCapitalized check, indicating a string that is not capitalized.
```text
res/values/strings.xml:2:Warning: String is not capitalized [StringNotCapitalized]
my string
```