【发布时间】:2021-08-23 22:21:10
【问题描述】:
我尝试使用 upgrade assistant 将 Android Gradle 插件从 4.2.2 升级到 7.0.1,它在 Android Studio 的工具 > AGP 升级助手中可用。它所做的唯一更改是对我的项目级 build.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 插件 7.0.1 还没有真正准备好迎接黄金时段,因为 Android Gradle Plugin documentation 仍然显示 classpath 'com.android.tools.build:gradle:4.2.0' 而不是 7.0.1。
我看到Android Gradle Plugin 7.0.1 release notes 提到了对 linting 的一些更改,但这些更改似乎与我无关。
我还浏览了Android Gradle Plugin source code,看看是否可以找到 linting 阶段以识别任何更改,但找到该代码并进行分析似乎需要做很多工作。
我搜索了答案,但我只能找到这两个 stackoverflow 条目,其中错误是合法的,程序员只需要更改他们的代码以确保他们引用的是实际的 Activity:
- Android Studio Error: Activity must extend android.app.activity
- MainActivity cannot be cast to android.app.Activity
我也尝试了 Android Gradle Plugin 7.0.0,但遇到了同样的错误。只有 Android Gradle Plugin 4.2.2 可以防止该错误。
这是 Android Gradle Plugin 7.0.1 中的错误吗?
更新:无法禁用Instantiatable
我尝试通过以下方式禁用Instantiatable lint 错误,但都没有阻止错误。
首先,我尝试将disable "Instantiatable" 添加到我的应用程序级 build.gradle 文件中:
android {
lintOptions {
disable "Instantiatable"
}
}
其次,我尝试在课程前添加@SdkSuppress("Instantiatable"):
@SdkSuppress("Instantiatable")
class MainActivity : AppCompatActivity() {
// ...
}
同样,我尝试了@SuppressLint("Instantiatable"),但也没有用。
【问题讨论】:
-
post 中提到的更多关于 AS 设置本身的“答案”怎么样,而且似乎也是一种解决方法。在 Artic Fox 中:设置 -> 编辑器 -> 检查
-
这个问题有一个未解决的问题。禁用“Instantiatable”只是一种绕过方式。可能还有其他担忧。见issuetracker.google.com/issues/196406778
标签: android android-studio gradle android-gradle-plugin