Android-Developer-Tools

Projects and modules build settings

A project in Android Studio represents the top-level Android development structure. Android Studio projects contain project files and one or more application modules. A module is a component of your app that you can build, test, or debug independently. Modules contain the source code and resources for your apps. Android Studio projects can contain several kinds of modules:

  • Android application modules contain application (mobile, TV, Wear, Glass) code and may depend on library modules, although many Android apps consists of only one application module. The build system generates APK packages for application modules.

  • Android library modules contain reusable Android-specific code and resources. The build system generates an AAR (Android ARchive) package for library modules. App Engine modules contain code and resources for App Engine integration.

  • Java library modules contain reusable code. The build system generates a JAR package for Java library modules.

Android Studio projects contain a top-level project Gradle build file that allows you to add the configuration options common to all application modules in the project. Each application module also has its own build.gradle file for build settings specific to that module.

Project Build File

By default, the project-level Gradle file uses buildscript to define the Gradle repositories and dependencies. This allows different projects to use different Gradle versions. Supported repositories include JCenter, Maven Central, or Ivy. This example declares that the build script uses the JCenter repository and a classpath dependency artifact that contains the Android plugin for Gradle version 1.0.1.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.1'

        // NOTE: Do not place your application dependencies here: they belong
        // in the individual module build.gradle files
    }
}

allprojects {
   repositories {
       jcenter()
   }
}

Note: The SDK location for the Android Studio project is defined in the local.properties file in the sdk.dir setting or through an ANDROID_HOME environment variable.

Module Build File

The application module Gradle build file allows you to configure module build settings, including overriding the src/main manifest settings and setting custom packaging options.

  • android settings

    • compileSdkVersion
    • buildToolsVersion
  • defaultConfig and productFlavors

    • manifest properties such as applicationId, minSdkVersion, targetSdkVersion, and test information
  • buildTypes

    • build properties such as debuggable, ProGuard enabling, debug signing, version name suffix and testinformation
  • dependencies

This example applies the Android plugin, uses the default configuration to override several manifest properties, creates two build types: release and debug, and declares several dependencies.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.mycompany.myapplication"
        minSdkVersion 13
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:20.0.0'
    compile project(path: ':app2, configuration: 'android-endpoints')
}

Note: You can inject custom build logic for property values defined by a function that gets called by the property, for example:

def computeVersionName() {
  ...
}

android {
    defaultConfig {
        versionName computeVersionName()
        ...
    }
}