Regan Music
Regan Music

Reputation: 511

Android Studio refuses to run main()

Repro steps:

  1. Start a new project in Android Studio (with the latest update);
  2. Make a new class and add main() as usual;
  3. 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.

Upvotes: 39

Views: 31223

Answers (11)

Roshan
Roshan

Reputation: 893

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.

Upvotes: 23

حسن زال
حسن زال

Reputation: 11

i had the same problem, but this way the error went away. In the priject field right click on app, select New module, then chosse java or kotlin Library. After that open your brand new module. Inside public class write main and hit enter. Now inside your main class you are able to write your code.as you wished.

Upvotes: 1

Sanjay Sharma
Sanjay Sharma

Reputation: 51

I also got stuck in this problem. I am using Android Studio Arctic Fox in Macbook Pro M1. I have also tried to add New Module and Select Java and Kotlin Library and edited the configuration but it did not work for me. But it did work on my Windows System.

In macbook, I simply downgraded the dependancy in my App level build.gradle file from

implementation 'androidx.appcompat:appcompat:1.5.0'

to

implementation 'androidx.appcompat:appcompat:1.2.0'

and run my Java Class as Run as Coverage and it worked.

Upvotes: 5

user1052478
user1052478

Reputation: 149

I have not much idea about it as I am very new in Android development and Android Studio. But after reading some solution over the internet and watchin some Youtube Videos, I have fixed it.

In app/build.gradle under dependencies for me implementation 'androidx.appcompat:appcompat:1.4.1' - this was there

I changed it to implementation 'androidx.appcompat:appcompat:1.2.0'

rebuilt the project and run, it worked for me..

I guess there must be some android studio update (in the latest version) was causing the problem.

Upvotes: 3

Sukhbir
Sukhbir

Reputation: 1608

Quick Fix : You can run using Run with Coverage.. See Image Below.

J

Permanent Solution: Add <option name="delegatedBuild" value="false" /> and sync project.

inside File gradle.xml under path E:\Project\.idea\gradle.xml. See Image below.

enter image description here

Upvotes: 69

Jian Chen
Jian Chen

Reputation: 467

Open "gradle.xml" File in this path :

.idea/gradle.xml

Add the following line before </GradleProjectSettings/>

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

That is it , it worked for me.

Upvotes: 33

Braian Coronel
Braian Coronel

Reputation: 22907

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

Upvotes: 2

Gideon Tobing
Gideon Tobing

Reputation: 409

I'm currently using Android Studio Version 4.0.1

  1. after creating the project, you should go to tab File ==> New ==> New Module... image one

  2. on "Create New Module/ Selecte Module Type":

  3. Select Java or Kotlin Library image 2 image 3

  4. After it finish, go to javalib folder/ module that you have created.

  5. Create Java file on there. image 4

  6. try to run it

Upvotes: 7

Frank
Frank

Reputation: 740

Try to run it with "coverage". I don't have any explanation, but it works!

Upvotes: 18

ferraro
ferraro

Reputation: 342

Avoid naming the class name as Test, Android studio somehow will get screwed when you name class as Test.

Upvotes: 3

Arthur
Arthur

Reputation: 41

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

Upvotes: 4

Related Questions