Thursday, September 4, 2014

Missing Android Private Libraries in Build Path

I recently came across the following error while creating a new android application project in eclipse.
My project has dependencies to appcompat_v7 project.


Found 2 versions of android-support-v4.jar in the dependency list,
but not all the versions are identical (check is based on SHA-1 only at this time).
All versions of the libraries must be the same at this time.
Versions found are:
Path: D:\rohit\eclipse_workspace\AndroidNotificationProgress\libs\android-support-v4.jar
Length: 758727
SHA-1: efec67655f6db90757faa37201efcee2a9ec3507
Path: D:\rohit\eclipse_workspace\appcompat_v7\libs\android-support-v4.jar
Length: 648327
SHA-1: ded9acc6a9792b8f1afc470f0c9cd36d178914cd
Jar mismatch! Fix your dependencies

Fortunately i found the solution. If you have recently updated your android SDK then that could be the cause of this error.

If you also got this type of error, then follow below steps to fix it.

First of all, if you have any android-support-xx.jar file in your project’s libs directory then delete it.

1) Check your Project Properties → Java Build Path. It will have Android Private Libraries missing as shown in below screenshot.

missing private libraries.png

2) Go to Project Properties → Android . Copy the SDK path configured in this as below.

android sdk path.png

3) Go to the directory : <android-sdk-home>\extras\android\support\v7\appcompat\libs You will find below 2 files there. android-support-v4.jar android-support-v7-appcompat.jar Copy these 2 files. and paste it to your appcompat_v7 project’s lib directory in your workspace. <your-workspace-dir>\appcompat_v7\libs 4) Clean and build both the project. appcompat_v7 and the one on which you got the error.

Tuesday, September 2, 2014

Downloading file from android webview over https (ssl)

If you are using android version lower than 4.1.2 ( API16), then downloading a file from android webview over https (ssl) might not work properly.

Please go through this post first : Downloading file from android webview , for instructions on how to download it over http.
Same instructions will be applicable for https including the additional instructions described in  below steps.

In android version lower than 4.1.2 , download manager doesn't handle https protocol. so you have to copy that class to your project and have to use it instead of that from sdk.

You should not use that custom download manager for all android targets. Because for newer versions that class already handles https. and some additional features might have been added to it.
So you can conditionally check for versions and use either of download manager.

if(android.os.Build.VERSION.SDK_INT<16) //for android 4.1.2 or lower
{
 com.test.android.app.DownloadManager downloadManager = new com.test.android.app.DownloadManager(getContentResolver(), "com.test"); // you have to provide your application package here
 com.test.android.app.DownloadManager.Request request = new com.test.android.app.DownloadManager.Request(Uri.parse(url));
 request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "app_download_directory");
 downloadManager.enqueue(request);
}
else //for newer version, use download manager from sdk
{
 Request request = new Request(Uri.parse(url));
 request.allowScanningByMediaScanner();
 request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "app_download_directory");
 DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
 dm.enqueue(request);
}

You have to add 2 SDK classes in your project : android.app.DownloadManager , android.provider.Downloads

You can download it from here : Download source files

Copy these 2 classes in your project and refer those in above code snippet. (replace com.test with your project’s package)



Downloading file from android webview

Here we will see how to download a file from android webview over http.
If you are downloading over https (ssl) then refer to this tutorial : Downloading file from android webview over https (ssl)

You might have used webview in your android application. but if your webview’s html has download link then it might not work directly.

If you are downloading over http protocol then continue reading this post. But if you are downloading over https (ssl) then you may continue reading this but you will also need some additional code to be added for that. Please refer this post for more information after reading current post : Downloading file from android webview over https (ssl).

There are 2 ways to download the file from within android webview.

1) Downloading a file by directly using android download manager.
2) Downloading a file by using a web browser opened from android webview. Browser will take care of downloading a file. It will internally use download manager.

When you download a file using a download manager, a notification will appear in the notification bar and you can check the progress there as below.

downloading-file-from-webview.png

We will be creating a simple android app with only one activity. This will have only webview inside it.  
Webview will load html which will have download link. and on click of which we will download a target file using download manager and web browser as well.

First of all following properties will be needed in android manifest file.

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

WRITE_EXTERNAL_STORAGE is required if you have to download the file to sdcard.
If you are going to test the sample app using android emulator, then you have to enable sdcard on it, as shown in below screenshot of virtual device properties from AVD manager.

android-emulator-sdcard-setting.png

Below is the activity’s layout xml.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <WebView        
        android:id="@+id/webView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

1) Downloading a file directly using android download manager

Following code will set the download listener for webview and will directly use download manager to download a file.


webView.setDownloadListener(new DownloadListener()
  {
   public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)
   {
    //for downloading directly through download manager
    Request request = new Request(Uri.parse(url));
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    dm.enqueue(request);
   }
  });

In the above code, if you don’t have sdcard on emulator/device, then you can use internal storage to store a file, by commenting this line.

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");

2) Downloading a file using a web browser opened from android webview

This will send an intent to web browser and web browser will take care of downloading the file. It will internally use download manager to download a file.

webView.setDownloadListener(new DownloadListener()
  {
   public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)
   {    
    //download file using web browser
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
   }
  });

If you are downloading over https (ssl) then refer to this tutorial : Downloading file from android webview over https (ssl)



Creating and Deploying Java Web Application on AWS using Elastic Beanstalk

This tutorial is for creating simple java web application using eclipse and then deploying it on AWS cloud. Video tutorial for creating/de...