您好,登錄后才能下訂單哦!
小編給大家分享一下Android Studio無(wú)法執(zhí)行Java類的main方法怎么辦,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
Android Studio升級(jí)到哦最新版3.6.1后,新建了個(gè)項(xiàng)目,發(fā)現(xiàn)無(wú)法執(zhí)行Java類的main方法。試了網(wǎng)上的各種方法,比如切換gradle離線模式、gradle.properties中添加android.enableAapt2=false等,我還嘗試了重新情況Gradle緩存、重新下載gradle等方式,都沒(méi)用。
環(huán)境
android studio版本:3.6.1
gradle版本:5.6.4
gradle插件版本:3.6.1
錯(cuò)誤提示
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_warningsBUILD FAILED in 207ms
11:41:35 PM: Task execution finished 'TxtUtil.main()'.
解決方案:
方案一:暫時(shí)的替代方案
Run "XXX.main()" with Coverage
方案二:修改.idea/gradle.xml文件
Android Studio使用gradle作為構(gòu)建工具,所以歸根到底還是gradle配置的問(wèn)題。我先找一個(gè)沒(méi)問(wèn)題的項(xiàng)目,再打開(kāi)新創(chuàng)建的項(xiàng)目,對(duì)比著看他們的gradle配置。經(jīng)過(guò)對(duì)比和嘗試,發(fā)現(xiàn).idea/gradle.xml文件內(nèi)部不同,并且參照老項(xiàng)目進(jìn)行修改是有效的。
老項(xiàng)目的.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>
新項(xiàng)目的.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>
經(jīng)過(guò)對(duì)比發(fā)現(xiàn),新項(xiàng)目的<GradleProjectSettings>
標(biāo)簽下少了<option name="delegatedBuild" value="false" />
這行配置,然后給新項(xiàng)目添加這行配置,運(yùn)行代碼發(fā)現(xiàn)成功了。
根據(jù)配置的字面意思理解,這個(gè)設(shè)置的意思是不委托g(shù)radle進(jìn)行構(gòu)建?,F(xiàn)實(shí)效果也是加了這行配置之后就是jvm執(zhí)行Java類的main方法了,而不是gradle當(dāng)做task來(lái)執(zhí)行了。
知識(shí)點(diǎn)補(bǔ)充,參考方法
Android Studio 3.0 無(wú)法運(yùn)行Java代碼main() 方法的解決方案
Android Studio 升級(jí)到3.0 以后發(fā)現(xiàn) 運(yùn)行Java 代碼中的main()方法的時(shí)候grandle 就會(huì)報(bào)錯(cuò)的問(wèn)題
解決方法 :
打開(kāi)工程目錄下的下圖標(biāo)注的文件:
打開(kāi)文件后 在文件中添加
android.enableAapt2=false
最后同步一下 OK!
看完了這篇文章,相信你對(duì)“Android Studio無(wú)法執(zhí)行Java類的main方法怎么辦”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。