Showing posts with label brief. Show all posts
Showing posts with label brief. Show all posts

Friday, 5 June 2015

Adding gradle dependencies when library package name is not known

Im an android developer who recently moved to Android Studio. I had this problem of finding a library dependency name that I needed to declare in the gradle. The problem started when I was trying to add the Volley library to my project, I searched all over to find the relevant package of volley which needs to be declared in the dependency set and when I tried to use the
https://android.googlesource.com/platform/frameworks/volley
The gradle build failed, and I couldnt find a proper package name. So, I thought to share with you how to add a dependency whose package name you don't know in the android studio gradle. Please follow these steps:

  1. Right click your project name (say app)
  2. Open Module Settings
  3. Select Dependencies tab
  4. Click on the + sign in the top right
  5. Choose Library Dependency
  6. In the window that opens type the library file's name(say volley) 
  7. Double click on any library file and click Ok

You will see the dependency added in your gradle and the gradle auto sync has been initiated.

Tuesday, 26 May 2015

Android Environment Setup and basic code

Android Studio is a slightly heavy app, even in a machine with reasonable specs it sometimes hangs, yet that would do the job, just don’t run too many applications while you are working with studio. Moving on, I will first talk about the system requirements which would be any modern system with at least 2.5+GHz processor , a 4 Gb or above RAM(this is the minimum, please don’t  go below this it will kill your system).
However, for Linux users, it is recommended to install Studio in 64 bit systems. But to install Studio which is a 32 bit application in a 64 bit machine you need to have the 32-bit support library package ia32-libs.
For Ubuntu users, you can execute the below command:
sudo apt-get install ia32-libs
1)      Install Java Development Kit(JDK)
Download  and install the required JDK package from the below link which is apt for your system OS.
For windows , the JAVA_HOME environment variable. The steps of which are:
  1. Open My Computer.
  2. Go to System Properties.
  3. Open Advance system settings.
  4. Goto Advanced Tab and Open Environmental Variables.
  5. Then Click on New and Set Variable Name : Path and Set Variable Value : C:\Program Files (x86)\Java\jdk1.7.0_25\bin (The Directory In which JDK is installed.)
  6. Click OK
Verify the JDK installation by typing
java -version
from the command prompt, it should output
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) Client VM (build 24.71-b01, mixed mode, sharing)
If it works properly, else it will show command not found error
Install Android Studio
Install Android  Studio from the below link
Go through the license agreement and download the file as exe or zip. Install the exe file or unzip the zip file.
Run Android Studio, for the very first run you will get a dialog prompting  to import settings from an existing installation, followed by the setup guide.  Go through the guide and in the end press the finish button.
On starting Studio, you will be greeted with a welcome screen as follows

If you have already created projects it will be listed in the Recent Projects tab.
Now starting fresh,  click on the Start a new Android Studio project  and go through the guide. 

You can give your project a name say “HelloWorld” in camel case in the Application name field, also give a proper package name say ”com.example.firstproject.helloworld” in lower case, click next.

Select your desired platform, say you are developing a mobile app, select phone and tablet and also choose the minimum sdk version as to which version of android release can support your app(its advised to choose the lowest version, so that you app may run in most devices), click next.



Select a template, say Blank Activity. An activity is a code to support the UI. It will help you to design your application to behave as you wish in each lifecycle of the application like when the app is in foreground or background etc. The next page allows you to name your project components such as your activity, layout (a layout is view design on how you want to see each activity in your device), click Finish.

The android studio opens up with a layout displayed in front of you. You can see it contains a view with a label “Hello World”. In android all activity layouts by default comes up this way, so for now just click the play button on top if you have a device plugged in.
To run in emulator
·         Select Tools>Android>AVD Manager or click the icon https://developer.android.com/images/tools/avd-manager-studio.png in the toolbar.
·         Click Create Virtual Device
·         Select a device configuration say Nexus 6 and click next.
·         Select the version of the AVD
·         Click Finish
Once the emulator is launched and the home screen appears (it may take a while, have patience) click the run button from the toolbar and you can see the Hello World in your screen.






Android Studio and Gradle in brief

The features of the Android Studio consist of the following:
  1. Intellij or the intelligent code editor -  provides powerful code editing, code completion, refactoring etc. so that developers can save a lot of time. This feature was available with the ADT bundle as well which was used for android development earlier.
  2.  UI - The new Studio is shaped for better developer interactions. Easily accessible tools and settings and all other development related buttons are their within reach and there is no need to fish around much. Also the Layout Editors are better designed and layout previews are more real looking.
  3.  Rich AVD - The AVD manager has a rich set of devices in all shapes and sizes. This is one great boon.
  4.  Built in google cloud support.
  5.  Build tools - This is one of the most confusing point for developers who switched from eclipse. I would be going to this in detail hence.
    • Android Studio makes use of the Gradle build tools which is an automated build toolkit, basically a plugin. It consists of a set of files to configure and manage the build process. The scripts are based on the Groovy programming language.  
    • Compared to Eclipse IDE, where the developers were unaware about the backend apk building process, Android studio’s Gradle allows a seamless way for developers to manage their builds. With this plugin you will be able to build different versions of the same project like paid, unpaid or device specific apks etc. Developers can also trigger the build from command line as well.
    • The gradle allows including dependencies from within your local repos as well as remote repos. The gradle build normally takes place by assuming a default project convention or structure. It also gives you the freedom to alter your project structure; only, you have to make sure to configure the build files accordingly.

There are two levels of build files: module build file and project build file.  The project level build file is the topmost and it will contain basic information. An example project gradle file is as follows:
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
         // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
 allprojects {
    repositories {
        jcenter()
    }
}

This file simply states that the build is based on the Android plugin and all the remote libraries will be obtained through the jcenter repository.
The Module build file contains the module wise build configurations. It also allows                 you override the main manifest file setting and configure custom packaging/compiling              option etc.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.demo.sample"
        minSdkVersion 19
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
debug {
            debuggable true
        }

    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:+'
}

Thursday, 21 May 2015

Android SDK

It consists of the following:



  • Android API: The API libraries provides the developer an interface to the devices in-built functions. Its the same libraries google uses to develop their native apps. As each api update release happens for your android handset, the SDK manager also receives updates, so that the developer can use the latest apis to built more featured applications.
  • Development tools: These are tools to compile and build executable applications out of the written source codes.
  • Android Emulator: In order to run and test your applications during the development phase, the SDK provides you a variety of emulator types. Using this you can get a visual experience of how your app runs in different devices. 
  • Sample codes: The SDK manager also has a huge collection of sample codes which describes on how to use different apis for different development needs.