Some project-structure advices(Android Studio)

Hello!

Here are some brief advices concerning project structure(gradle based) in Android Studio

The structure is pretty simple, it's gonna save you a lot of time, and will make development process comfortable.

Add following lines to your build.gradle file in app module

buildTypes {
        debug{
            debuggable true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    productFlavors {
        mock {
            applicationId = "com.your.application.id.mock"
            resValue "string", "app_name", "Your_app_name(D)"
        }
        production {
            applicationId = "com.your.application.id.production"
            resValue "string", "app_name", "Your_app_name"
        }
    }

Store your files in java folder following way:

smth went wrong

So it will give you different configuration of the project. You can switch them in Build Variants windows on Android Studio.

So you can have different implementation for certain classes in your project. For example you can create mock server implementation to test the project even if the server is down or not ready at the moment

Note that if you want some classes to have different implementation in different build variants, just add this class to different configurations, but exclude this class from main. You can do it also with everything: resources,assets and so on

Also some staff about signing your apk-file. A lot of people use the wrong approach and store singing config information to their build.gradle file this way:

signingConfigs {
        debug {
            storeFile file(/somepath/...)
            storePassword 'android'
            keyAlias 'android'
            keyPassword 'android'
        }
            storeFile file(/somepath/...)
            storePassword 'android'
            keyAlias 'android'
            keyPassword 'android'
        }
    }

This approach is wrong because when you push your project to some public repo, the singing config data would be seen by all who have access to this public repo

There is a way how to resolve this problem:

In your root path of the project create new text-file "keystore.properties" with following content

storePassword=android
keyPassword=android
keyAlias=android
storeFile=../distribution/keystore

Then modify your build.gradle file

add it after apply plugin 'com.android.application'

def keystorePropertiesFile = rootProject.file("keystore.properties");
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

Then:

signingConfigs {
        debug {
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
        }
        release {
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
        }
    }

    buildTypes {
        debug{
            debuggable true
            signingConfig signingConfigs.debug
        }
        release {
            signingConfig signingConfigs.release
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

Here I showed some tips concerning project-structure. Next time i want to post something about Dagger 2

Good luck!