1

Yesterday I was able to run a plain Java class in Android Studio without any problems. I did this by creating an Application configuration:

enter image description here

... for running the main() method of some class I defined in the default app module, e.g.:

package com.example.myappname;

public class Api {

    public static void main(String[] arguments) {
        System.out.println("API test");
    }
}

If I then pressed run:

enter image description here

... the output (which I can't show you now, because it doesn't work anymore) of main() would appear nicely in the Run Tool Window:

enter image description here

As far as I was aware, no gradle task or build process was invoked by running this type of configuration. But now, all of a sudden, it appears as though gradle is being executed before running, because it complains that task [:app:Api.main()] failed with message:

A problem occurred configuring project ':app'.
  > Could not create task ':app:Api.main()'.
    > SourceSet with name 'main' not found.

... as seen here:

enter image description here

I tried recreating the configuration and for the first run it worked as before again, but on rerunning the same gradle build error appears again.

Do you have any idea why this is happening and how I can solve this? Am I correct in thinking gradle should be omitted with these kinds of Application configurations?

When it worked, the output in the Run Tool Window would appear almost instantaneously and no elaborate build process appeared in the Build Tool Window, leading me to believe no gradle build process was being executed.

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106

1 Answers1

0

I got it to work again:

enter image description here

... but don't ask me how exactly. It appears to be very buggy.

After deleting all sorts of cache directories and files and searching all kinds of directories for the location of the configuration files I was able to find that the configurations are stored in .idea/workspace.xml of the project.

And one thing I appeared to notice is that the following "hidden" option is needed as a minimum (which I believe was not there, when it didn't work):

    <method v="2">
      <option name="Make" enabled="true" />
    </method>

Here's a full example configuration:

  <configuration name="API" type="Application" factoryName="Application" nameIsGenerated="true">
    <option name="MAIN_CLASS_NAME" value="com.example.myappname.Api" />
    <module name="MyAppName.app.main" />
    <method v="2">
      <option name="Make" enabled="true" />
    </method>
  </configuration>

But sometimes it works and sometimes it doesn't. It appears to be the most reliable if I signify the classpath (-cp option in the Android Studio dialog) to be MyAppName.app instead of MyAppName.app.main.

Ironically, it started to work again when the configuration dialog claimed it couldn't find the class I wanted to run and a red X appeared in the top right corner of the configuration icon:

enter image description here

So, it's all a bit of a mystery to me, as to why it sometimes works and other times it does not.


PS: I was wrong about gradle not building in this type of configuration; it does.

And after a few more tests, it appears one simply shouldn't select the ".main" part when choosing a module for the classpath argument, as that appears to be some specific gradle configuration/task I don't understand well enough.

In spite of Android Studio claiming it can't find the class then (and the red X appearing), it works.

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106