Saturday, April 30, 2016

[Android] Chapter 3: Package Structure

Hello! Today we take a look at project and package structure in our project.

Project structure:

The basic project structure is the following:

  • app
    • src
      • main
        • java - Contains java classes.
        • res - Contains resources (images, layouts, dimensions, strings, etc.)
        • AndroidManifest.xml - File defining application components.
      • build.gradle - Build configuration file.

the Java folder contains our project package (like com.example.myapp). We already chosen this name when we created our project. This project package will contain all our Java classes.

Java package structure:

Let's say our project package looks like this:


In this project root package we will create another packages for easier orientation. We should use simple names, which can tell us exactly what the package contains. As an example, I will show you my favorite project structure I personally use:

  • app
    • src
      • main
        • java
          • cz.helu.stocks
            • activity
            • adapter
            • database
            • dialog
            • entity
            • enum
            • fragment
            • service
            • rest
            • utility

Many of those are pretty simple and self explanatory. Like activity, adapter, database, fragment, service, etc.
Some people suggest putting entity and enums into the same package. I personally prefer to split them in two separate packages. Also, I like to put DialogFragments into dialog package, instead of fragment package.
In the rest package I usually store everything that has something to do with REST API calls, like whole retrofit configuration and classes.
More complicated is the utility package, where I personally store classes that can't be placed anywhere else. For example DateConverterUtility.

In the project root folder I also like to create the Application class and also Configuration class, in which I hold many useful information that I use in other places of the app, and want them in one place (for example REST API URL, logs, and services switchers).

Resource package structure:

Here we store our layouts, images, icons, and other resources for our application. Here is the structure we should use.
  • app
    • src
      • main
        • res
          • drawable
          • drawable-mdpi
          • drawable-hdpi
          • drawable-xhdpi
          • drawable-xxhdpi
          • drawable
          • layout
          • mipmap-mdpi
          • mipmap-hdpi
          • mipmap-xhdpi
          • mipmap-xxhdpi
          • values
            • colors.xml
            • dimens.xml
            • strings.xml
            • styles.xml
            • themes.xml

In drawable folder we store images. In mipmap folder we store application icon. In layout folder we store all the layouts we use in the app and in the values folder everything else.
You surely noticed that there is a lot of drawable and mipmap folders. There is a reason for it. As a developer you should support multiple screen sizes and this it the way for doing it. When you create some image you want to use in your app, you should create it in more sizes, and put it in all those folders. You can learn more about it here:

Now for the values xml files, they all have the following structure:
<?xml version="1.0" encoding="utf-8"?>
    <!-- Some items here -->

And here are some examples for the xml files you should use. All are pretty self explanatory.

The colors.xml file:
<?xml version="1.0" encoding="utf-8"?>
    <color name="global_color_primary">#03a9f4</color>
    <color name="global_color_primary_light">#b3e5fc</color>
    <color name="global_color_primary_dark">#0288d1</color>

The dimens.xml file:
<?xml version="1.0" encoding="utf-8"?>
    <dimen name="global_spacing_8">8dp</dimen>
    <dimen name="global_spacing_16">16dp</dimen>
    <dimen name="global_spacing_24">24dp</dimen>

The strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
    <string name="global_network_offline">No Internet connection available</string>
    <string name="global_network_fail">Communication with server failed</string>

You can than access all this defined values in the code using getResources() method:
In the next lesson we will take a look at how we should correctly name these classes and files in your package.

No comments :

Post a Comment