Wednesday, 6 April 2022

Cannot access 'java.lang.Object' which is a supertype of... Check your module classpath for missing or conflicting dependencies

I thought it would be fun and informative to learn more about static code analysis by implementing my own custom linter. I've been trying to declare the below dependencies:

dependencies {
    compileOnly "com.android.tools.lint:lint-api:30.1.2"
    compileOnly "com.android.tools.lint:lint-checks:30.1.2"
}

But Gradle is giving an error that these dependencies cannot be resolved. After digging some, I found that MavenCentral and Google have seemingly different versioning for these libraries, with Google being the version described in the code snippet above and MavenCentral having version 25.3.0 as the latest version. If I swap out the version numbers for those described on MavenCentral, the dependencies can be resolved by Gradle but my custom linter code is completely highlighted in red and gives an error

Cannot access 'java.lang.Object' which is a supertype of my_custom_linter. Check your module classpath for missing or conflicting dependencies

There are many SO posts regarding this error, and the few that have been resolved were resolved by using the most recent version of an available dependency, which in my case circles back to the first error described in this post.


Project level build.gradle:

buildscript {
    repositories {
        google()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.4"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
    }
}

plugins {
    id 'com.android.application' version '7.1.2' apply false
    id 'com.android.library' version '7.1.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
    id 'org.jetbrains.kotlin.jvm' version '1.6.10' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Lint module build.gradle:

plugins {
    id 'java-library'
    id 'org.jetbrains.kotlin.jvm'
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7
}

dependencies {
    compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10"
    compileOnly "com.android.tools.lint:lint-api:25.3.0"
    compileOnly "com.android.tools.lint:lint-checks:25.3.0"
}

jar {
    manifest {
        attributes("Lint-Registry": "com.example.lint_checks.LintRegistry")
    }
}

App module build.gradle:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        ...
    }

    buildTypes {
        ...
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    kotlinOptions {
        jvmTarget = '1.7'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {
    lintChecks project(path: ":lint-checks")
    ...
}

InputTypeDetector.kt (custom lint class):

/* Entire class highlighted by IDE with error message: 
Cannot access 'java.lang.Object' which is a supertype of 'com.example.lint_checks.InputTypeDetector'. Check your module classpath for missing or conflicting dependencies */ 

class InputTypeDetector: LayoutDetector() {
    companion object {
        @JvmStatic
        internal val ISSUE_MISSING_INPUT_TYPE = Issue.create(
            id = "MissingInputType",
            briefDescription = "Specify inputType attribute to get proper keyboard shown by system.",
            explanation = "You should specify an inputType for each EditText so that you can get the proper keyboard to be shown by system.",
            category = Category.USABILITY,
            priority = 8,
            severity = Severity.ERROR,
            implementation = Implementation(
                InputTypeDetector::class.java,
                Scope.ALL_RESOURCES_SCOPE
            )
        ).addMoreInfo("https://developer.android.com/training/keyboard-input/style")
    }

    override fun getApplicableElements(): Collection<String>? {
        return listOf(
            SdkConstants.EDIT_TEXT,
            "androidx.appcompat.widget.AppCompatEditText",
            "android.support.v7.widget.AppCompatEditText"
        )
    }

    override fun visitElement(context: XmlContext, element: CoroutineContext.Element) {
        if (!element.hasAttribute(SdkConstants.ATTR_INPUT_TYPE)) { // Check if the element has the `android:inputType` attribute
            context.report(
                issue = ISSUE_MISSING_INPUT_TYPE, // The issue that we defined above
                location = context.getLocation(element),
                message = ISSUE_MISSING_INPUT_TYPE.getExplanation(TextFormat.TEXT)
            )
        }
    }
}

UPDATE: I've verified the lint-api and lint-checks jar files are in the external libraries directory of my project. Gradle has resolved and downloaded these dependencies when requesting version 25.3.0. Why am I getting the error about accessing java.lang.Object and checking my classpath?

I have been following this tutorial



from Cannot access 'java.lang.Object' which is a supertype of... Check your module classpath for missing or conflicting dependencies

No comments:

Post a Comment