Is it possible in Android Studio to launch the main() method of a simple java-class that contains only references to the standard java classes without compiling the entire Android project.

  • Right-click on the class - "Run ClassName.main ()" will not work? - post_zeew
  • Please clarify the essence of the question. You just need to execute the main method as mentioned above and execute it on the fly in the Android application itself? - Streletz
  • @post_zeew The whole project is compiled. - iramm
  • @Streletz I want to quickly test a java-class independent of Android. - iramm
  • In this case, Android Studio, IMHO, is clearly not the best solution. Still, this IDE is designed for a specific narrow range of tasks. Better take some lightweight general purpose IDE. - Streletz

2 answers 2

Yes you can. To do this, you need to specify in the IDE, but rather to give new settings for your debugger, so that it understands which part to compile. In fact, everything is much simpler.

Create a java class:

enter image description here

We write the main method

As usual, the IDE immediately recognizes the starting point.

 public class HelloFromMainJava { public static void main(String... args){ System.out.println("HelloFromAndroidStudio ☺!"); } } 

You can run from the panel, or right-click the mouse, or just a combination of keys as in the screenshot. enter image description here

Total:

enter image description here Do not forget on the panel, above, switch back to your project. Good luck.

  • one
    Thank. Strangely, yesterday I did the same thing, but System.out.println did not give out anything and compiled the whole project. Now everything worked with your light hand)) - iramm
  • one
    Isn't the whole project compiled in this case? - post_zeew
  • In this case, gradle will rebuild everything according to the standard, if you want to completely ignore other classes, then you need to reconfigure the build gradle, as I remember, you can specify the exact path in the srcSet. In fact, there are many options here, will depend on your desire. From my own experience, I open IntelliJ IDEA separately, so for simpler, faster and more convenient, and all the additional utilities that I might not even use in the project, I write and test there. You can make a separate module by the way, if you need exactly Android classes, also an interesting option. - Shwarz Andrei

To edit and compile standard Java classes, download a development environment, such as NetBeans . Convenient and fast.

  • I already started a class that interests me in Intellij Idea. The problem was in the toString () method. Inside the Android project when using this class, the Cyrillic alphabet looked unrecognizable. When launched into Idea, Cyrillic characters looked correct. The reasons for this problem have not yet been clarified. - iramm
  • @iramm, I saw your other topic, about the SDK update and related problems with the Cyrillic alphabet. Try to do as said here and here . And, of course, it is better not to use Cyrillic in the code, but to put it out in string.xml, later on, replace / fix it faster and do the localization. - Pollux
  • I would not mind including everything in string.xml, but is it possible for messages like "Tomorrow, October 2, 2016 ..." What you advise, I will try in the near future. - iramm
  • @iramm Do you need to insert a variable into a string resource? Something like this - pavlofff
  • @pavlofff Thanks for the link. Looks like I'm wrong. And in this case you can also do with string resources. - iramm