Android Studio 拒绝运行 main()

2022-09-01 11:08:18

重现步骤:

  1. 在Android Studio中启动一个新项目(使用最新更新);
  2. 创建一个新类并像往常一样添加main();
  3. 右键单击类以运行 main() 作为测试。
package test;

public class Test {
    public static void main(String[] args) {
    }
}

通常我希望我能,但这次,无论它是一个新项目,我都会得到以下错误:System.out.printLn("Hello World")

2:34:23 PM: Executing task 'Test.main()'...

Executing tasks: [Test.main()] in project C:\Users\regan\Desktop\events\MyApplication


FAILURE: Build failed with an exception.

* Where:
Initialization script 'C:\Users\regan\AppData\Local\Temp\Test_main__2.gradle' line: 20

* What went wrong:
A problem occurred configuring project ':app'.
> Could not create task ':app:Test.main()'.
   > SourceSet with name 'test' not found.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.4.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 0s
2:34:24 PM: Task execution finished 'Test.main()'.

在Unity中学习了多年的C#之后,我对Java相当陌生,并且不知道所有这些东西都试图告诉我什么。我已经谷歌了,但我发现最接近的是一种隐藏此错误的方法(假设代码仍在编译中)。我需要这个代码至少编译。


答案 1

快速修复 :您可以使用“带覆盖率运行”来运行。见下图。

J

永久解决方案:添加和同步项目。<option name="delegatedBuild" value="false" />

在路径 下的文件 内。见下图。gradle.xmlE:\Project\.idea\gradle.xml

enter image description here


答案 2

在此路径中打开“gradle.xml”文件:

.idea/gradle.xml

</GradleProjectSettings/>之前添加以下行

<option name="delegatedBuild" value="false" />

就是这样,它对我有用。


推荐