“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”
How does AndroidStudio run the Java main method
Sometimes we test the output directly in the main method to make sure that the Java method works as expected. Although it can be output in an activity, it is very heavy and slow to use. You need to compile, install, and run the command to see the effect. Therefore, if it is a pure Java method, it is easier to use the main method directly.
Although Java main method is very convenient, as android developers do not often use eclipse, sometimes I suddenly thought of an idea, but also have to link to the phone to run the program, so the speed is slow, the operation is very troublesome, so how to convenient output in AndroidStudio?
Since the Android developer is updated frequently, the Settings for each developer version will be slightly different. This article starts with the Snow Fox version and adds some additions to the Settings for other developer versions.
In the premise, write the main method in a Java file, enter PSVM, and then the development tool will automatically prompt according to the input, press Enter to quickly generate a main method.
Methods a
On the main method class, right-click -> Run xx.main()
The following error message is displayed:
* What went wrong:
A problem occurred configuring project ':app'.
> Could not create task ':app:Test.main()'.
> SourceSet with name 'main' not found.
Copy the code
Add the following attributes under the tag in the.idea/gradle. XML file at the root of the project to run. Do not let Gradle take over the build task and prevent it from being executed as gradle’s task.
<option name="delegatedBuild" value="false" />
Copy the code
Method 2
Using a shortcut key to type PSVM prompts us to use the main method. For the main class, right-click ->More Run/Debug -> Run xx with Coverage. After executing, you can see the output. For the second time, you can click the button to the right of the Debug icon to Run directly.
Method 3 (Standby)
The above two methods are already fully operational on the Snow Fox version. For other versions of AndroidStudio, there is another approach.
- Create a New Java library
- After successful creation, modify the compile (Gradle) file
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'java.MainRun'
Copy the code
In addition to modifying compiled files, you can also perform interface operations. For example, select run Edit Configurations to modify. Once the compilation is complete, you can right click to run it.