Android Studio Gradle Build

Ongoing work with Android Studio. I had a library project + 1 pro project + 1 free project and it was very cumbersome in Eclipse. With gradle it becomes 1 project with two flavors. The main concepts are appended. This is a developing post and this is my fourth edit. I will refine it further as I get more insights.

Project Structure

D:\
repository/                       //Custom Maven repository
MyExtLib/                         //External Lib Project
MyProject/                        //Project Root

   MyModule/
      libs/
         myJar.jar               //Lib as a jar in libs folder
      src/
         main/
            java/                //Common Classes
            res/                 //Common res
            AndroidManifest.xml
         proflavor/
            java/
            res/
            AndroidManifest.xml
         freeflavor/
            java/
            res/
            AndroidManifest.xml
      proguard-project.txt
      xxyy.keystore
      build.gradle               //Module level gradle build file

   MyLib/
      src/
         main/
            java/
            res/
            AndroidManifest.xml
      build.gradle             //Lib Module gradle build file

   settings.gradle
   build.gradle                //Top level gradle build file

Top level settings.gradle file

include ':MyModule'

Common module level build.gradle file

apply plugin: 'com.android.application'
android {
    compileSdkVersion 22
    buildToolsVersion '22.0.1'

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1625
        versionName '9.2.5'
    }

    signingConfigs {
        myConfig {
            storeFile file('../xxyy.keystore')
            storePassword "*********"
            keyAlias "*******"
            keyPassword "********"
        }
    }

    buildTypes {
        debug {
        }
        release {
            zipAlignEnabled true
            signingConfig signingConfigs.myConfig
            minifyEnabled true
            proguardFile getDefaultProguardFile('proguard-android.txt')
        }
    }

    productFlavors {
        freeflavor {
            applicationId 'com.cls.xxx'
            proguardFile('freeflavorproguard.txt')
        }
        proflavor {
            applicationId 'com.cls.xxxpro'
            proguardFile('proflavorproguard.txt')
        }
    }
}

dependencies {
    compile 'com.android.support:support-v13:21.0.0'
    compile 'com.android.support:appcompat-v7:22.1.1'
}

You can have four approaches to libraries:-
1. Local Library Module
2. Library jar in /libs folder
3. External Library Project
4. Library jar in custom local maven repository

Approach 1 – Local Library Module

#Insert this line into the top level settings file

include ':MyLib'

#Insert this line into the module level gradle file

dependencies {
   compile project(':MyLib')
}

Approach 2 – Library jar in /libs folder

#Insert this line into the module level gradle file
dependencies {
   compile files('libs/myJar.jar')
}

Approach 3 – External library project

#Insert this line into the top level settings file

include ':MyExtLib'
project(':MyExtLib').projectDir = new File('D:/MyExtLib')

#Insert this line into the module level gradle file

dependencies {
   compile project(':ExtMyLib')
}

Approach 4 – Custom Local Maven Repository

Step 1 upload jars into maven

Install maven from maven.org
Issue maven command for uploading your jars which are not available in mavenCental to a custom local maven repository d:\repository

D:\store\Applications\apache-maven-3.1.0\bin>mvn install:install-file
-Dfile=d:\My\ Project/My\ Module/libs/myJar.jar
-DgroupId=com.cls.mycustomjar
-DartifactId=mycustomjar
-Dversion=1.0.0
-Dpackaging=jar
-DgeneratePom=true
-DlocalRepositoryPath=d:\repository

Step 2 upload library projects as aar into maven

//build.gradle for Mylib
//uploadArchives task
apply plugin: 'com.android.library'
...
apply plugin: 'maven'
group = 'com.cls.mylibrary'
version = '1.0.0'
uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: uri("d:/repository"))
        }
    }
}

Execute gradle tasks to upload library projects as aar into the custom rep

//In mylibrary project execute task
gradlew uploadArchives

Step 3

OK Now your jars and libraries are ready to be pulled from your local custom repository.

//build.gradle for module
//Insert repository code into the module level build.gradle file

repositories {
    maven { url uri("d:/repository") }
}

dependencies {
    compile 'com.cls.mycustomjar:mycustomjar:1.0.0'
    compile 'com.cls.mylibrary:mylibrary:1.0.0'
}

Author: admin

Indie Android dev

Leave a Reply

Your email address will not be published. Required fields are marked *