Wednesday 22 June 2016

Adding a Custom Library in Android Studio

There are different ways of adding a custom library to your project in android studio:
* I'm going to use Glide library for this demonstration
* Installation will require the internet, if it's the first time your adding them. But after your configuration of one project with the library, you won't need the internet anymore if you import the same library to other projects.
a) Add a library using gradle
b) You can also add a library from the project structure
c) You can also add a library a jar file
d) You can also add a library a compiled module or project


1. Adding a library using gradle:

 - Since you may not know all the library compile command,
i)  Go to maven repository , this is a repository for most libraries,

ii) On the search, enter your library key word, e.g glide



iii) Select the first option, most likely, the library you seek for will be listed the first one, or atleast among the first results.
- The results also come with a short description of what the library does; thus gives you a head start of the library your looking for.





iv) The library opens fully on a new page with all the versions of the library. just click on the latest one, in my case as per now, latest glide version is 3.7.0 



v) The library opens up on a new page again with different tabs at the bottom e.g maven, gradle, sbt, ivy e.t.c
- Just select gradle for our case,
- You'll see the compile command of the library, e.g in my case it will be :
    compile group: 'com.github.bumptech.glide', name: 'glide', version: '3.7.0'




- Just copy the code, and go to the build.gradle (Module app) in android studio and paste the code.



- After pasting, the build.gradle should be:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "24.0.0"
    defaultConfig {
        applicationId "com.androidmastermind.glide"        
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'    compile 'com.android.support:appcompat-v7:23.4.0'
     
    //Glide library
    compile group: 'com.github.bumptech.glide', name: 'glide', version: '3.7.0'
}

* Android studio will prompt you to sync the project, click on the Sync Now option and wait for some few seconds, then you'll be good to go.



2. Add a library from the project structure.

i) On the toolbar in android studio, click on the project structure 

ii) After the project structure window has poped up, click on the app, at the modules, then click on the dependencies tab. At this tab, you'll see all the libraries your app is using.



iii) On the right side of the ide, click on the   then choose library dependency, from the popup option window that will appear.

iv) Another window will appear with a list of dependencies and will provide search functionality; At this point, just type the library keyword,e.g glide and press enter.



v) A list of libraries associated with that keyword will be listed, just select the dependency you need and press ok,



vi) Android studio will take you back to the previous popped up window and added your library at the bottom of all the dependencies available in your app. 

.
vi) Press ok, gradle will sync your project for some few seconds then you'll be good to go.

3. Add a jar file as a library 

- There are many sources of jar files, you can also get them from the maven repository after searching for it, ...
i) Pretty straight forward, just copy the downloaded jar file to android studio under the libs directory in your Project



ii) Right click on the library downloaded (glide for my case) and select the option 'Add As Library'



iii) Android studio will prompt you to select the module to add the library to, just leave it to default and press ok.


iv) Gradle will sync for some few seconds and you'll be good to go,



Also see:
* How to load an image from the internet using glide
* Android login, using volley



0 comments:

Post a Comment