Android Studio升级到最新版3.6.1后,新建了个项目,发现无法执行Java类的main方法,很蛋疼。试了网上的各种方法,比如切换gradle离线模式、gradle.properties中添加android.enableAapt2=false等,我还尝试了重新情况Gradle缓存、重新下载gradle等方式,都没用。
环境
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
1
方案二:修改.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来执行了。
个人能力有限,如有错误尽请指正。
————————————————
版权声明:本文为CSDN博主「tinyvampirepudge」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_26287435/article/details/104749689