Home >Java >javaTutorial >How to solve the problem that Android Studio cannot execute the main method of Java class

How to solve the problem that Android Studio cannot execute the main method of Java class

PHPz
PHPzforward
2023-05-17 16:43:062478browse

环境

android studio版本:3.6.1
gradle版本:5.6.4
gradle插件版本:3.6.1

错误提示

11:41:35 PM: Executing task 'TxtUtil.main()'...

Executing tasks: [TxtUtil.main()] in project /Users/tinytongtong/Documents/workspace/as/self/BlogDemo


FAILURE: Build failed with an exception.

* Where:
Initialization script '/private/var/folders/qw/8_p1t59142d0xntgn799rt1c0000gn/T/TxtUtil_main__.gradle' line: 20

* What went wrong:
A problem occurred configuring project ':app'.
> Could not create task ':app:TxtUtil.main()'.
   > SourceSet with name 'main' 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.6.4/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 207ms
11:41:35 PM: Task execution finished 'TxtUtil.main()'.

解决方案:

方案一:暂时的替代方案

Run "XXX.main()" with Coverage

方案二:修改.idea/gradle.xml文件

Android Studio使用gradle作为构建工具,所以归根到底还是gradle配置的问题。我先找一个没问题的项目,再打开新创建的项目,对比着看他们的gradle配置。经过对比和尝试,发现.idea/gradle.xml文件内部不同,并且参照老项目进行修改是有效的。

老项目的.idea/gradle.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
 <component name="GradleMigrationSettings" migrationVersion="1" />
 <component name="GradleSettings">
 <option name="linkedExternalProjectsSettings">
 <GradleProjectSettings>
 <option name="delegatedBuild" value="false" />
 <option name="testRunner" value="PLATFORM" />
 <option name="distributionType" value="DEFAULT_WRAPPED" />
 <option name="externalProjectPath" value="$PROJECT_DIR$" />
 <option name="modules">
  <set>
  <option value="$PROJECT_DIR$" />
  <option value="$PROJECT_DIR$/app" />
  ...
  </set>
 </option>
 <option name="resolveModulePerSourceSet" value="false" />
 </GradleProjectSettings>
 </option>
 </component>
</project>

新项目的.idea/gradle.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
 <component name="GradleMigrationSettings" migrationVersion="1" />
 <component name="GradleSettings">
 <option name="linkedExternalProjectsSettings">
 <GradleProjectSettings>
 <option name="testRunner" value="PLATFORM" />
 <option name="distributionType" value="DEFAULT_WRAPPED" />
 <option name="externalProjectPath" value="$PROJECT_DIR$" />
 <option name="modules">
  <set>
  <option value="$PROJECT_DIR$" />
  <option value="$PROJECT_DIR$/app" />
  </set>
 </option>
 <option name="resolveModulePerSourceSet" value="false" />
 </GradleProjectSettings>
 </option>
 </component>
</project>

经过对比发现,新项目的<GradleProjectSettings>标签下少了<option name="delegatedBuild" value="false" />这行配置,然后给新项目添加这行配置,运行代码发现成功了。

根据配置的字面意思理解,这个设置的意思是不委托gradle进行构建。现实效果也是加了这行配置之后就是jvm执行Java类的main方法了,而不是gradle当做task来执行了。

知识点补充,参考方法

Android Studio 3.0 无法运行Java代码main() 方法的解决方案

Android Studio 升级到3.0 以后发现 运行Java 代码中的main()方法的时候grandle 就会报错的问题

解决方法 :

打开工程目录下的下图标注的文件:

Android Studio无法执行Java类的main方法怎么解决

打开文件后 在文件中添加

android.enableAapt2=false<br>

最后同步一下 OK!

The above is the detailed content of How to solve the problem that Android Studio cannot execute the main method of Java class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete