错误:MainActivity必须扩展android.app.Activity [Instantiatable].<



我尝试使用Android Studio中的Tools &gt中的升级助手将Android Gradle Plugin从4.2.2升级到7.0.1;AGP升级助手。它所做的唯一更改是对我的项目级构建。gradle文件:

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

然而,现在当我运行./gradlew assemble assembleAndroidTest时,我得到以下错误:

/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
}
}

我的项目是多模块的,但我不怀疑这是问题所在,因为它抱怨的是应用程序模块,而不是库模块。

我相信我的<activity>标签在我的AndroidManifest.xml中为我的应用程序模块格式良好:

<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>

此外,我不认为扩展AppCompatActivity而不是android.app.Activity有什么问题,因为我在MainActivity.kt:

import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
// ...
}

我担心Android Gradle Plugin 7.0.1还没有为黄金时间做好准备,因为Android Gradle Plugin文档仍然写着classpath 'com.android.tools.build:gradle:4.2.0'而不是7.0.1。

我看到Android Gradle Plugin 7.0.1的发布说明中提到了一些linting的变化,但是这些变化对我来说似乎都不相关。

我还浏览了Android Gradle Plugin的源代码,看看我是否能找到linting阶段或识别任何更改,但看起来要找到这些代码并进行分析需要做很多工作。

我搜索了答案,但我能找到的都是这两个stackoverflow条目,其中错误是合法的,程序员只需要改变他们的代码,以确保他们引用了一个实际的Activity:

  1. Android Studio错误:Activity必须扩展Android .app. Activity
  2. MainActivity不能强制转换为android.app.Activity

我也尝试了Android Gradle Plugin 7.0.0,但得到了同样的错误。只有Android Gradle Plugin 4.2.2可以避免这个错误。

这是Android Gradle Plugin 7.0.1的bug吗?

更新:could not disableInstantiatable

我试图禁用Instantiatablelint错误,但没有一种方法可以阻止错误。

首先,我尝试将disable "Instantiatable"添加到应用程序级构建中。gradle文件:

android {
lintOptions {
disable "Instantiatable"
}
}

第二,我尝试将@SdkSuppress("Instantiatable")添加到类:

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

同样,我尝试了@SuppressLint("Instantiatable"),但也不起作用。

Android Gradle Plugin文档仍然显示classpath 'com.android.tools.build: Gradle:4.2.0'而不是7.0.1.

你需要往下读,读到这个和这个。这个表只适用于7.0.0之前的版本。

这是Android Gradle Plugin 7.0.1中的一个bug吗?

很有可能。或者,可能超出,因为InstantiatableLint检查有问题的历史记录。

如果您的场景与2021年8月的三个bug之一不匹配,并且您可以提供可重复的测试用例,请提交一个新的问题!除此之外,如果清理和重建不能解决您的问题,您可能需要通过在构建的all中添加以下内容来暂时禁用InstantiatableLint检查。应用程序级或库级的Gradle文件(即除了项目级的build.gradle之外的所有文件):

android {
lintOptions {
disable "Instantiatable"
}
}

记住你可以使用lint configxml文件并添加这个"Instantiatable"规则为"ignored":

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

另外,你应该在build.gradle中配置lint插件来使用lint.xml文件:

android {
[...]
lint {
lintConfig = file("$rootDir/config/lint.xml")
}
}

最新更新