Introduction
Building apps with source code can be done by both the terminal and using the graphical interface of Android studio. For the creation of a new project inside Android studio, the below files are available,
- build.grade: This is a top-level project related build file. It contains the declaration of some repositories and dependencies to all modules.
- gradle.properties: It contains technical settings related to Gradlebuilds. There is no need to edit the file.
- gradlew and gralew.bat: There are wrapper scripts to run builds using a terminal instead of Android studio Integrated development Environment.
- local.properties: This holds generated technical properties related to Android studio installation.
- settings.gradle: Android stuio handles the files during the addition of new modules.
- app/build.gradle: It is a module related build file. It creates a first module application including build file and additional modules have various names, and it has its own build files.
Module configuration
Build gradle is each module of a project which contains its own build file. It creates an initial build file and a basic build file for a module with Kotlin.
- apply plugin: "com.android.application"
- apply plugin: "kotlin-android"
- apply plugin: "kotlin-android-extensions"
- android {
- compileSdkVersion 26
- defaultConfig {
- applicationId " de.pspaeth.xyz"
- minSdkVersion 16
- targetSdkVersion 26
- versionCode 1
- versionName " 1.0 "
- testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
- }
- buildTypes {
- release {
- minifyEnabled false
- prouardFiles
- getDefaultProguardFile(" proguard-android.txt "), "proguard-rules.pro"
- }
- }
- }
- dependencies {
- implementation fileTree(dir: 'libs', include: ['*.jar'
- '])
- implementation " org.jetbrains.kotlin:kotlin-stdlib-jre7"
- $kotlin_version "
- implementation " com.android.support:apcompat-v7:26.1.0"
- implemetation "com.android.support.constraint : constraint-layout : 1.0.2"
- test Implementation "junit : junit : 4.12"
- androidTestImplementation " com.android.support.test : runner : 1.0.1"
- androidTestImplementation " com.android.support.test.espresso : espresso - core : 3.0.1"
- }
The apply plugin line load and apply gradle plugins are necessary for Android and Kotlin development. The android { } element specifies settings for the android plugin. The dependencies { } element specifies dependencies of module. The implementation keyword specifies that the dependency is needed for both compiling and running the module. Identifiers refer to build type or source set xyz. The build type and product flavor are declared inside the configurations { } element.
- configurations {
-
- freeDebugCompile {}
- }
Module common configuration
The element defaultConfig { } specifies configuration settings for a build independent of variant selection. The possible settings can be looked up in the Android Gradle DSL but see below:
- defaultConfig {
- applicationId 'com.example.myapp'
- minSdkVersion 24
- targetSdkVersion 26
- versionCode 42
- versionName "2.5"
- }
Build Types
Application development is subjected to different stages and it is referred to as build Types. The development and release are the two build types setup by Android studio during the creation of a new project. Opening the module build.gradle file, it consists of the below code,
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles
- getDefaultProguardFile(' proguard-android.txt'), ' proguard-rules.pro'
- }
- }
The below code defines a new build type called integration that inherits from debug by virtue of initWith. It adds a custom app file suffix and provides a place holder to use in manifest file. The settings specified are rather numerous. Identifier is used for filtering files to be included in the application.
- buildTypes {
- release {}
- debug {}
- integration {
- initWith debug manifestPlaceholders = [hostName: " internal.mycompany.com "]
- applicationIdSuffix ".integration"
- }
- }
Running Build from console
When bootstrapping an application project using android studio, the building of the application is made easy with the terminal. To build an application for APK files build typedebug or release the following line : ./gradlew assemleDebug, ./gradlew assembleRelease and to build the debug APK and install it on a connected device or using the emulator use the line : ./gradlew installDebug.
Signing
Applications must be signed before running on the device. FDEbug build type normal signing configuration is used and in Android studio build it generates a signed APK menu item, and uses the appropriate key. Release build type, refers to signing config inside the listing, creating a keystore using the dialog and selecting the generated signed APK in the menu.