Tuesday, 30 May 2017

Build Gradle Android App from the Command Line

Hello Guys!!! Hope You all are doing well.
Today I am going to discuss “How to build android app from command line”.

You people can ask this question what is the need?
We have Android Studio IDE , Eclipse. Yaa Guys!!! your question is right .
Let me explain :-
  • As you guys know that Android Studio IDE internally used Gradle build System and Eclipse use ANT build System.
  • As a Android App developer we focus on our App business logic and ui, less bother for build system internally used by IDE.
  • But sometime for large android app(containing several module, lib, platform dependency), It is better to use command line for building the app(whole Eco system).
  •   Second thing, It is better to use command line  for your day to day development work, It will give you better understanding and power as a Developer.
Here I am going to discuss Android Gradle build via command line. I will discuss android ANT based build System(Eclipse) later . In Gradle system, We have two option to build android app.
  1. Build with Gradle (install Gradle and environment path) 
  2. Build with Gradle Wrapper (no need to install, created by Android Studio project)
Building with gradle wrapper (option 2) is easy. There is no need to install gradle build in your machine.  Just you have to go in your project directory (created by Android Studio ) and have to run some build command with gradlew .   
In window machine
gradlew task-name
in Mac/Linux
./gradlew task-name
you can see a list of all available build tasks for your project 
gradlew tasks
By running above command you find a list of task that you can run using gradlew for building your app. I am summarizing few of them in below table 
Command
Description
./gradlew build
build project, runs both the assemble and check task
./gradlew clean build
build project complete from scratch
./gradlew clean build
build project complete from scratch
./gradlew test
Run the tests
./gradlew connectedAndroidTest
Run the instrumesntation tests


Now Build with gradle (Option 1)
For building android app with gradle, we have  two option  Install gradle  or download gradle zip and use it. Gradle runs on all major platform (Linux, Mac window) and required only Java JDK or JRE version 7 or higher.  
Installing gradle on Linux(Ubuntu) , I will suggest use SDKMAN tools to install gradle on your machine.  After successfully installation on sdkman in your Linux machine run following command 
sdk install gradle 4.2.1
For more detail of gradle installation you can visit here.  Now open your terminal and run gradle,  output of this command is shown in below image 
Now in terminal go to your project (either created by Android Studio or  Eclipse, but make sure it contain build.gradle file)  root directory and run gradle tasks to see all available task you can perform by using gradle. 
Above image is showing gradle tasks command output for a empty project root directory. If you run this same command in a Android Studio project or an Android app project that contain build.gradle file you find a lot more option for building your project via gradle command. The most simple Android project has the following build.gradle file:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.3'
    }
}
apply plugin: 'com.android.application'
android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1" } 
gradle build command compiles, tests, and packages the code into an APK file. You can see results of the build process in the build folder.  For more detail on gradle build system you can visit Android Developer website

Thanks
Saurabh
Happy Coding!!!!

1 comment:

  1. hello,
    i just want to say that you have clearly mention the process very clearly and i also found it is very much easy to understand. keep sharing.

    android app development company in chennai

    ReplyDelete

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...