Friday, July 10, 2015

Android Studio: Android application with Java library

This is a short guide how to set up Android Studio for developing Android Applications that depend on the Java library.

Add Java library to the Android application

 Add new module using File -> New -> New module





Add dependencies to the app/build.gradle. In my case I have demolib library at the same level as app (if it is in the /lib folder, you should add ‘:lib:demolib’). 

Execute Java application 

You can add Main class to the Java library and execute it as Java application. 
Open Edit configurations


    Add Application configuration, select Main class and module
          Results are in “Run” tab:
     
     

Add Unit tests to the Java library

 Useful links:

Add corresponding directory structure for tests
Open Java class you want to test. Right click in editor and select Go To->Test
follow Create New Test assistance
Add necessary dependencies to the demolib/build.gradle
The same action can be done by the following way:
Open File-> Project Structure
Add Library dependency to demolib
Type "junit" and press search button
change Scope to Test compile
Right click in the test class editor and select Run as JUnit (not gradle)
New JUnit configuration would be created:
Results:




No comments:

Post a Comment