问题
Repro steps:
- Start a new project in Android Studio (with the latest update);
- Make a new class and add main() as usual;
- Right-click class to run main() as a test.
package test;
public class Test {
public static void main(String[] args) {
}
}
Usually I expect I can just System.out.printLn("Hello World")
but this time, no matter if it's a new project, I get the following error:
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()'.
I am fairly new to Java after years of C# in Unity and have no idea what all this stuff is trying to tell me. I have googled but the closest I found was a way to HIDE this error (assuming code was still compiling). I need this code to at least compile.
回答1:
The app module is an android library and expects android lifecycle methods such as onCreate() etc for successful execution. If you want to execute plain java code one option is to add a java library to the project using File -> New Module -> Java Library and add the main method there:
package com.example.lib;
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello");
}
}
This would work as you expect it to.
回答2:
Quick Fix : You can run using Run with Coverage.. See Image Below.
Permanent Solution: Add
<option name="delegatedBuild" value="false" />
inside File gradle.xml
under path E:\Project\.idea\gradle.xml
. See Image below.
回答3:
Try to run it with "coverage". I don't have any explanation, but it works!
回答4:
I had this problem momentarily as well
I found when I changed Gradle to offline and then back online again then updated Gradle and made sure the connection and proxy settings were correct, things started working normally
回答5:
open "gradle.xml" in path ,“.idea”
.idea/gradle.xml
add the following within </GradleProjectSettings/>
<option name="delegatedBuild" value="false" />
then , it worked for me.
回答6:
Avoid naming the class name as Test, Android studio somehow will get screwed when you name class as Test.
回答7:
I'm currently using Android Studio Version 4.0.1
after creating the project, you should go to tab File ==> New ==> New Module...
on "Create New Module/ Selecte Module Type":
Select Java or Kotlin Library
After it finish, go to javalib folder/ module that you have created.
Create Java file on there.
try to run it
回答8:
If the module is from an android application or library it is not allowed to run the main; because what is expected is navigate transitions between stages of the activity lifecycle.
In the following cases of modules with these plugins applied, the main can NOT be run.
build.gradle.kts (:appAndroid)
plugins {
id("com.android.application")
id("kotlin-android")
}
build.gradle.kts (:libraryAndroid)
plugins {
id("com.android.library")
id("kotlin-android")
}
build.gradle.kts (:kmmShared)
plugins {
kotlin("multiplatform")
id("com.android.library")
}
If the purpose of running the main was to test things, what you really have to do is use the sourceSets of test
.
If the module is from a Java or Kotlin library, running the main is allowed so the error would not exist.
The following applied plugins describe a JVM library:
build.gradle.kts (:jvmLibrary)
plugins {
id("java-library")
id("kotlin")
}
Finally, if using the test
sourceSets or creating a JVM module was not enough for you, there is a 'hack' which is weak because it depends on the IDE (It does not work in KMM modules).
.idea/gradle.xml
<project version="4">
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="delegatedBuild" value="false" />
</GradleProjectSettings>
</option>
</component>
</project>
GL
来源:https://stackoverflow.com/questions/57734823/android-studio-refuses-to-run-main