Android App Anatomy with Gradle Introduction

Our Android Project contains : 


  1. Kotlin files for he core logic of the app 
  2. A resources folder for static content such as images and strings 
  3. An Android Manifest file that defines essential app details so the OS can launch your app. 
  4. Gradle scripts, for building and running your app. 

Layout Inflation process makes a bunch of kotlin view objects from this xml. Our inflated views are organised in what's known as View hierarchy tree.

activity_mail.xml     ---> LAYOUT INFLATION --->MainActivity.kt 


Gradle Introduction

Gradle is the build tool of choice for Android, it controls a number of different things including but not limited to following things:

  • What devices run your app 
  • Compile to executable 
  • Dependency management 
  • App signing for Google Play 
  • Run Automated Tests 


When we click the run button the series of Gradle commands compiles your source code through dependencies and any associated resources into a android application package or APK.

Gradle is a generic build tool. It doesn't automatically knows how to build Android Project or Kotlin files. 
so this two dependencies in project level are very important. This is where we download all the Gradle scripts for actually building Kotlin Android Apps. This scripts are part of Gradle Plugins. And these dependencies say which version of plugins to actually download. 

dependencies {
classpath "com.android.tools.build:gradle:4.1.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

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

APK : Android Application Package is the executable format for distributing Android Applications.
It installs that apk to emulator or physical device to run it.

build.gradle(Project)
build.gradle(Modlule:app)  : there is a specific gradle file for each module of your project modules.


plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 30
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.niranjan.coolapp"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

Module

A folder with source files and resources for a discrete piece of functionality in your app.

By default we only have only one module called :app but if we consider making larger applications and we want to split the part of functionality or write your own libraries or support things like android wear we will end up making more modules which has it's own build.gradle files.

  • build.gradle (Module: diceroller)
  • build.gradle (Module:randomizationlibrary)
  • build.gradle (Module: watchface)

Repository

In gradle repository are remote server where external code is downloaded from. Ex. Android libraries


2011 - First supported library released. Google owned library for backward compatiblity.
2018 - Android jetpack released
AndroidX - is the namespace for android jetpack


import androidx.appcompat.app.AppCompatActivity





Comments

Popular Posts