0. Preface
Recently, because of the development of a bottom-level Java application for the project, I stumbled across a problem where Eclipse had Java project and now does not directly build a Java application project with Android studio on hand. So after collecting and integrating some information on the Internet, it is found that Android studio can also perfectly support the development of Java application projects, so write a blog to share the coder with the same needs.
1, the establishment of the Java EE Project
There is no way to build a Java project directly in Android Studio, so it's easier to just build an Android project and then add a Java Library module to project so that you have a Java You can find tasks such as build, jar in the Gradle window of Android Studio, run these tasks to build and package, but note that it is not possible to run the Java module directly because there is no running script for this module.
2. Create a run script for the Java library
If you want to convert the Java Library module to a application module that can be run directly, it is very simple, just click on Edit Configurations in the Run menu, then on the left you can see a number of running scripts, click "+" in the upper left corner, Then add a application configuration and check the new configuration, you can see that there is a main class (Specify your main method Class), VM Option (can set the heap size, and so on), program Arguments (you can set the running parameters), Use Classpath of module (sets the Classpath of which Module is associated), and whether to open the form, whether a custom JRE is used, and so on.
That's what we need to do, first of all, to set the main class as the class with the main method you want to run in your Java library module, and then set the use Classpath of module to the Java library you built. Module, which basically completes the configuration of an execution script.
Then one more thing to do is to set the main Class in Gradle, open the Build.gradle file in the Java Library module and change it to the following:
1 Apply plugin: ' java '2 Apply plugin: ' Application '34 mainclassname = ' Org.hanos.java.MainApplication '56Dependencies {7 compile Filetree (dir: ' Libs ', include: [' *.jar '])8 }
Be aware of the application plugin and Mainclassname, after completing this step, try running the new script.
3. Play the executable jar package for Java library
This is actually very simple, just need to modify Build.gradle can:
1Apply plugin: ' java '2Apply plugin: ' Application '3 4Mainclassname = ' org.hanos.java.MainApplication '5 6 Jar {7 Manifest {8Attributes ' Main-class ': Mainclassname9 }TenBaseName = ' Hframejava ' OneVersion = ' 0.1.0 ' A } - - Dependencies { theCompile Filetree (dir: ' Libs ', include: [' *.jar ']) -}
Where manifest indicates what attributes need to be added to the manifest in the jar that needs to be hit, the executable jar package needs to add Main-class to indicate the Main method class that is running, basename represents the name of the jar package, and version represents the build number, After completing this build.gradle, Sync project, then execute Jar task to find the jar package in <module>/build/outputs/libs
OK, basically Android Studio development Java application need content is here, if there is a new discovery, welcome to add a message, thank you ~
Developing J2SE project methods using Android Studio