26

I tried upgrading Android Gradle Plugin from 4.2.2 to 7.0.1 using the upgrade assistant which is available in Android Studio at Tools > AGP Upgrade Assistant. The only change it made was to my project-level build.gradle file:

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.1' // changed from 4.2.2 to 7.0.1
        // ...
    }
}

However, now when I run ./gradlew assemble assembleAndroidTest I get the following error:

/builds/locuslabs/android-team/locuslabs-android-sdk/app/src/main/AndroidManifest.xml:21: Error: MainActivity must extend android.app.Activity [Instantiatable]
            android:name="com.locuslabs.appsdk.MainActivity"
                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   Explanation for issues of type "Instantiatable":
   Activities, services, broadcast receivers etc. registered in the manifest
   file (or for custom views, in a layout file) must be "instantiatable" by
   the system, which means that the class must be public, it must have an
   empty public constructor, and if it's an inner class, it must be a static
   inner class.
1 errors, 0 warnings
Lint found fatal errors while assembling a release target.
To proceed, either fix the issues identified by lint, or modify your build script as follows:
...
android {
    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }
}

My project is multi-module, but I don't suspect that as the problem since it's complaining about the application module, not a library module.

I believe my <activity> tag is well formed in my AndroidManifest.xml for my application module:

        <activity
            android:name="com.locuslabs.appsdk.MainActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustNothing">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Furthermore, I don't think there is anything wrong with extending AppCompatActivity instead of android.app.Activity as I'm doing in my MainActivity.kt:

import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    // ...
}

I'm concerned that Android Gradle Plugin 7.0.1 is not really ready for prime-time because the Android Gradle Plugin documentation still says classpath 'com.android.tools.build:gradle:4.2.0' instead of 7.0.1.

I saw that the Android Gradle Plugin 7.0.1 release notes mentioned some changes to linting but none of those changes seemed relevant to me.

I also skimmed through the Android Gradle Plugin source code to see if I could find the linting stage any identify any changes but it looked like a lot of work to find that code and do that analysis.

I searched for answers but all I could find were these two stackoverflow entries where the error was legitimate and the programmer just needed to change their code to ensure they were referencing an actual Activity:

  1. Android Studio Error: Activity must extend android.app.activity
  2. MainActivity cannot be cast to android.app.Activity

I also tried Android Gradle Plugin 7.0.0 but got the same error. Only Android Gradle Plugin 4.2.2 prevents the error.

Is this a bug in Android Gradle Plugin 7.0.1?

Update: could not disable Instantiatable

I tried to disable the Instantiatable lint error the following ways but none of them prevented the error.

First, I tried adding disable "Instantiatable" to my application-level build.gradle file:

android {
    lintOptions {
        disable "Instantiatable"
    }
}

Second, I tried prepending @SdkSuppress("Instantiatable") to the class:

@SdkSuppress("Instantiatable")
class MainActivity : AppCompatActivity() {
   // ...
}

Similarly, I tried @SuppressLint("Instantiatable") but that didn't work either.

Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113
  • What about the "answers" mentioned in this [post](https://stackoverflow.com/questions/22821420/this-class-should-be-public-android-support-v7-internal-widget-actionbarview-ho) that are more about AS settings itself, and also seem like a workaround. In Artic Fox: Settings -> Editor -> Inspections – javdromero Aug 23 '21 at 22:36
  • 3
    There is an opened issue for this problem. Disabling "Instantiatable" is only a bypass. There could be other concerns. see https://issuetracker.google.com/issues/196406778 – Java42 Oct 13 '21 at 18:08

2 Answers2

24

the Android Gradle Plugin documentation still says classpath 'com.android.tools.build:gradle:4.2.0' instead of 7.0.1.

You need to read further down the page, to this and this. That table is only relevant for pre-7.0.0 versions.

Is this a bug in Android Gradle Plugin 7.0.1?

Quite possibly. Or, perhaps beyond, as the Instantiatable Lint check has a history of problems.

If your scenario does not match one of those three August 2021 bugs, and you are in position to provide a reproducible test case, file a fresh issue! Beyond that, if a clean-and-rebuild is not clearing up your problem, you might need to simply disable the Instantiatable Lint check for the time being by adding the following to all of your build.gradle files at the application or library level (i.e. all except your project-level build.gradle):

android {
    lintOptions {
        disable "Instantiatable"
    }
}
Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you @CommonsWare. I don't think those three August 2021 Android Gradle Plugin issues referenced at https://androidstudio.googleblog.com/2021/08/android-studio-arctic-fox-202031-patch.html are the issue. – Michael Osofsky Aug 23 '21 at 23:35
  • when you say "file a fresh issue" are you referring to a fresh issue on stackoverflow or somewhere else (where?) – Michael Osofsky Aug 23 '21 at 23:36
  • I tried two methods for disabling `Instantiatable` which I've just described as an update to my original post (see "Update: could not disable `Instantiatable`"). Do you know of any other way to disable it? – Michael Osofsky Aug 23 '21 at 23:42
  • 1
    @MichaelOsofsky: "are you referring to a fresh issue on stackoverflow or somewhere else (where?)" -- I was referring to [the issue tracker](https://issuetracker.google.com/issues). – CommonsWare Aug 24 '21 at 10:38
  • 1
    @MichaelOsofsky: "I tried two methods for disabling Instantiatable" -- I am very surprised that the first one did not work. You mentioned that you have multiple modules. Have you disabled `Instantiatable` in all of them? Are you still getting the error from a command-line build, or only from within Studio? I see `Instantiatable` in Settings > Editor > Inspections, and you could change the severity there, but AFAIK that's for IDE inspections, and your error report sure seems like a Lint check, which is related but separate. – CommonsWare Aug 24 '21 at 10:39
  • 1
    you were right! After I added disable "Instantiatable" to *all* of my `android { lintOptions {}}` blocks then my `./gradlew assemble assembleAndroidTest` command succeeded. I edited your answer by adding this key detail and will mark your answer as accepted. – Michael Osofsky Aug 24 '21 at 16:37
  • 1
    As for filing a fresh issue in [the issue tracker](https://issuetracker.google.com/issues), the problem is similar to [AGP 4.0 false positive Instantiatable Lint check if Application extends java class](https://issuetracker.google.com/issues/158128960) except it applies to Android Gradle Plugin 7.0.0 and 7.0.1 not 4.0 and it applies to the Activity, not to the Application. Considering that issue was marked resolved, I tried to reproduce on a new project but couldn't so I didn't file it. I guess it has something to do with some config option in my project. – Michael Osofsky Aug 24 '21 at 17:06
  • When I add that `lintOptions` line I get `...failed: ... .gradle/caches/jars-9/o_ad21f02729bd9fcad5f9c9e683636547/gradle-tooling-extension-impl.jar` without further information. Why? – rwst Apr 08 '22 at 15:26
  • In case you want to know where to add this code, search for `compileSdkVersion` and add this block blow it `lintOptions { disable "Instantiatable" }` – Shady Mohamed Sherif Apr 25 '22 at 06:42
0

Remember you can use the lint config xml file and add this "Instantiatable" rule as "ignored":

<lint>
    [...]
    <issue id="Instantiatable" severity="ignore" />
</lint>

In addition, you should configure lint plugin in your build.gradle to use the lint.xml file:

android {
[...]
    lint {
        lintConfig = file("$rootDir/config/lint.xml")
    }
}
Rubén
  • 103
  • 1
  • 7