Building Apps In Kotlin

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.
  1. apply plugin: "com.android.application"  
  2. apply plugin: "kotlin-android"  
  3. apply plugin: "kotlin-android-extensions"  
  4. android {  
  5.     compileSdkVersion 26  
  6.     defaultConfig {  
  7.         applicationId " de.pspaeth.xyz"  
  8.         minSdkVersion 16  

  9.         targetSdkVersion 26  
  10.         versionCode 1  
  11.         versionName " 1.0 "  
  12.         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"  
  13.     }  
  14.     buildTypes {  
  15.         release {  
  16.             minifyEnabled false  
  17.             prouardFiles  
  18.             getDefaultProguardFile(" proguard-android.txt "), "proguard-rules.pro"  
  19.         }  
  20.     }  
  21. }  
  22. dependencies {  
  23.     implementation fileTree(dir: 'libs', include: ['*.jar'  
  24.                 '])    
  25.                 implementation " org.jetbrains.kotlin:kotlin-stdlib-jre7"  
  26.                 $kotlin_version "    
  27.                 implementation " com.android.support:apcompat-v7:26.1.0"  
  28.                 implemetation "com.android.support.constraint : constraint-layout : 1.0.2"  
  29.                 test Implementation "junit  : junit : 4.12"  
  30.                 androidTestImplementation " com.android.support.test : runner : 1.0.1"  
  31.                 androidTestImplementation " com.android.support.test.espresso : espresso - core : 3.0.1"  
  32.             }  
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.
  1. configurations {  
  2.     // flavor = "free", type = "debug"    
  3.     freeDebugCompile {}  
  4. }  

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:
  1. defaultConfig {  
  2.     applicationId 'com.example.myapp'  
  3.     minSdkVersion 24  
  4.     targetSdkVersion 26  
  5.     versionCode 42  
  6.     versionName "2.5"  
  7. }  

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,
  1. buildTypes {  
  2.     release {  
  3.         minifyEnabled false  
  4.         proguardFiles  
  5.         getDefaultProguardFile(' proguard-android.txt'), ' proguard-rules.pro'  
  6.     }  
  7. }  
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. 
  1. buildTypes {  
  2.     release {}  
  3.     debug {}  
  4.     integration {  
  5.         initWith debug manifestPlaceholders = [hostName: " internal.mycompany.com "]  
  6.         applicationIdSuffix ".integration"  
  7.     }  
  8. }  

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.


Similar Articles