Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: MainActivity must extend android.app.Activity [Instantiatable]

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.

like image 237
Michael Osofsky Avatar asked Aug 23 '21 22:08

Michael Osofsky


1 Answers

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"
    }
}
like image 158
CommonsWare Avatar answered Oct 17 '22 12:10

CommonsWare