Now you can Subscribe using RSS

Submit your Email


Working with android studio gradle and offline mode

Vishal Shrestha
I was finally comfortable developing for android with eclipse but then Google rolled out the stable version of Android studio and ended support for eclipse so I had to switch to android studio and I was introduced to the world of Gradle. Yes, it was and still is frustrating for me to use gradle but it does have it's fair share of advantages that you will definitely understand after finishing you first project with android studio. Here are few pointers about gradle and how you can use the offline mode, so no need to bang you head on the wall,  you will start understanding it gradually by using it, not just by studying the documentation.

When you create a new project, go to file->settings->build, execution, deployment->build tools->gradle. Make sure you keep the use default gradle wrapper selected unless you have very specific and have requirements to use your own Gradle build. You can select offline mode too, i will talk about it in the end of this post. There is a default Gradle distribution that is already shipped and linked up in your project, so that you do not need to separately go ahead and download Gradle and set it up.

First let's take an overview of what's in the android gradle file. Go to  /project/app/build.gradle.
You will see something like this-

android {
    compileSdkVersion 21    buildToolsVersion "21.1.1"
    defaultConfig {
        applicationId "com.vishals.bblhel"        minSdkVersion 14        targetSdkVersion 21        versionCode 1        versionName "1.0"    }
    buildTypes {
        release {
            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''        }

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.squareup.picasso:picasso:2.3.4'    compile ''}
From top to bottom lets see what each property should be-
  • compileSdkVersion
This should not be higher than the highest SDK version you have installed via SDK Manager.  You can check the SDK Manager, look for platform entries and check the API and the Status.

  • buildToolsVersion
This should be set to the latest Android SDK build-tools version you have installed, not a revision/version that matches your target SDK.  Remember, the build tools version is not related to your SDK version. Mostly, you should install and use the latest build tools version.

  • targetSdkVersion
This should be the same as your compileSdkVersion. Just like in eclipse.

  • dependencies
All the support libraries are identified / referenced here.

compile ''
A version suffix is used to identify the support library, which comes after the colon (21.0.0 in above example). Note that, this specifies the version of the support library, not the SDK version. Generally you should use the latest unless you specifically need to use previous versions of methods/functions in older support library revisions.
In your SDK Manager, jmake sure you have Support Repository installed.

When importing someone else's project, there will mostly be sync problems, Corret the version numbers and other properties in build.gradle and Android Studio will request you Sync again. And hopefully your errors will be fixed.

Now let' talk about offline mode, if you want to work offline, you can check the offline mode in the settings. If you have checked this option, Gradle will use the cache for dependency resolution. If the files are not there in the cache, then it will result in a build error. You might get some errors, if you add some dependency, Gradle will not be able to resolve it since it's going to look into the cache itself.You might  see a failed to sync error. In case you receive this error. Just uncheck the offline mode and sync again and then check the offline mode again. Completely offline work is very difficult with the gradle system if you have to keep adding dependencies.

Vishal Shrestha / Author & Founder

A developer by profession, a born Adventurer. I mainly do Android but like to get my hands dirty with web development and a little bit of Python. I would't rather go on a Trek than a party and you can find me having a few rounds with the heavy bag to let out the steam ;)

For Business info : My Portfolio Site.


Post a Comment

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha