Thursday, 30 October 2014

Video search in Android using Youtube Data api

This post is about how to use Youtube's data api to search youtube videos in android. Unlike all the other youtube api's like Player api there is not an android client to leverage those api's into the app.

But there is a Java client provided access to data api which we can use to search videos in our case.
Following post will allow you to make you better use of the Java client to get the required result.

Since in this post we will be presenting to you how to search youtube videos by keywords but using the java client to search videos gets a little confusing as Java client contains a number of jars required for various functionalities ranging from posting a channel bulletin, uploading a video, updating an video, searching videos etc.

A lot of other dependencies (Google OAuth client etc.) are also added in the client library which simply of no use in a case like current where we only want to search videos for a particular keyword.

Assumptions :
1. Registered your application in Google API console to be able to use Google Services like Youtube Data API etc.
Please follow the link

https://developers.google.com/youtube/v3/getting-started#before-you-start

2. Create a browser key under your project's OAuth and Credentials menu.

The API supports two types of credentials. Create whichever credentials are appropriate for your project:
-> OAUTH
-> API KEY

Since we are presenting a demo for the videos search, so we won't need any user level authentication hence avoiding OAUTH, instead we will be using API KEY to authenticate app to use Youtube data api.

YOUTUBE Data Api :
Best part of the youtube data api is that it provides an efficient way to fetch the partial resources with employed filtering of unneeded data. Hence makes better use of network, CPU and memory resources.

The filtering as cited in the above sentence is implemented by using two parameters to each request we make to fetch the youtube reosurce. It simply means that a particular youtube resource which is completely defined by a large number of properties can be filtered down based upon the properties required by a user.


1. part : this parameter ensures that a required group of properties is returned by the user. It is the one of the important top-level non-nested properties that should be added into the api response . For example : snippet, contentDetails, statistics etc.

2. fields : this filters the API response to return only specific properties within the requested resource parts.
example for representing filtering of nested properties is as follows :
-> fields=items/id,playlistItems/snippet/title,playlistItems/snippet/position {identify a nested property}
-> fields=items(id,snippet/title,snippet/position)
-> fields=items(id,snippet(title,position)) {group of nested properties }



NOTE : Alternative for accessing Youtube data is to fetch the data using REST API url with defined parameters
https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=YOUR_API_KEY&fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics


Gradle dependencies required are : 


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

 // Library for using Youtube data api
 compile 'com.google.apis:google-api-services-youtube:v3-rev120-1.19.0'
 compile 'com.google.http-client:google-http-client-android:+'
 compile 'com.google.api-client:google-api-client-android:+'
 compile 'com.google.api-client:google-api-client-gson:+'

 // Library for using CardView
 compile 'com.android.support:cardview-v7:21.0.+'

 // Image loading library
 compile 'com.squareup.picasso:picasso:2.3.+'
}


Issue :
We will create a browser key instead of android key with allowed HTTP referrers field set empty. With Android key it was giving service config errors (Error code : 403)

References :
https://developers.google.com/youtube/v3/getting-started
https://developers.google.com/youtube/v3/code_samples/java#search_by_keyword



Please find Source Code here.

Monday, 27 October 2014

Gradle Configuration for an Android Studio project with Android 5.0 or L or Lollipop

This post is only about configuring an android project in Android Studio with compile and target versions set as  android L or 5.0 or Lollipop. 

    I am writing this post with the sample gradle as I faced a lot of problems while configuring Lollipop's preview version.
    I will write the sample gradle as well as the screen shot of android sdk manager to represent most basic requirements required for a project setup with target version as Lollipop.
   
    So to be able to use any feature of the Android 5.0, here is the sample gradle with support library for Android L features :



apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.0.1"

    defaultConfig {
        applicationId "your app's packagename"
        minSdkVersion 10
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.+'
 // Not compulsory just added 
    compile 'com.google.android.gms:play-services:+'
 // Support for L features for devices with lower OS versions
    compile 'com.android.support:cardview-v7:21.0.+'
    compile 'com.android.support:recyclerview-v7:21.0.+'
}

Also see the following attached screenshots to get the required SDK, various support dependencies, platforms etc to be downloaded :

Android Tools :


Android SDK 5.0(L or Lollipop)

 Minimum Android SDK (as per above gradle 2.3.3 or API level 10)

Support Libraries for L features for older devices

Please provide your comment in case of any issues.

Monday, 20 October 2014

Git Submodules

This blog post is a very simple description on how to use a very important feature of GIT i.e submodules.

Requirement:
How to use a third party library into your app without keeping the source code for library project inside your repository ?There are multiple ways to include a third party code into your app. Either copy the entire source code into your app or get it installed using some tool like Ruby gem or CPAN install.

Or else you could import the remote third party repository into your code without getting its actual code by using git submodule feature. This is also used to include a third party repository without making your own repository getting heavier in size.
This approach also allows you to contribute back to the third party repository.


Submodules:
Submodules allow you to keep a Git repository as a sub-directory of another Git repository. This lets you clone another repository into your project and keep your commits separate.

 What you need to get the library :
Add the external repository under your project in a sub-directory. You can do this by the calling following git command from the project's repository


        git submodule add <remote url> <subdirectory name>  
        example :
        git submodule add https://github.com/couchbase/couchbase-lite-android.git  mylibs

Now you will have the library project under subdirectory folder. and will see that you have a new file in your project directory
        new file:   .gitmodules
        new file:  <subdirectory name> (in example mylibs which will contain the external lib project)
 

   
.gitmodules is a configuration file that contains the mapping between the library project's url and the subdirectory to which it is pulled into. 



What git thinks of and does with submodules :
Although <subdirectory name> is a subdirectory in your working directory, Git sees it as a submodule and doesn’t track its contents when you’re not in that directory. Instead, Git records it as a particular commit from that repository.
When you make changes and commit in that subdirectory, the superproject notices that the HEAD there has changed and records the exact commit you’re currently working off of; that way, when others clone this project, they can re-create the environment exactly.

All the Git commands work independently in the two directories.



What other users need to do to get your repository along with submodules : 
1. Clone the repository.
       $ git clone <repository_url>
2. Navigate to root of the project directory

       $ cd <repo_name>
2. call following command from project's root

       $ git submodule init && git submodule update
      
You must run above two commands: git submodule init to initialize your local configuration file, and git submodule update to fetch all the data from that project.


Now you are ready to Clean, Build and Run.