SVN ignores file extension .so by default – which corrupted my build

I was just working on my current OSGI project which is build with Eclipse PDE headless for multiple platforms.
The Eclipse Deltapack contains a launcher bundle (directory) calledorg.eclipse.equinox.Lauscher.gtk.linux.x86_1.0.200.v20090520 which contains a file called eclipse_1206.so.
This launcher bundle I also committed to my SVN repository for my headless build from the build machine.

I was wondering why my build was always broken and corrupt and I found out that in my SVN repository the eclipse_1206.so file was missing…and indeed it was NOTcommitted.
I first thought I made a mistake, so I removed and recommitted everything. And again, the eclipse_1206.so wasn’t there.

So I started googling and I found this which directed me to this. Apparently *.so files are igored by SVN by default even though there is no svn:ignore property set.

Solution:
In order to solve this I did the following:

  1. Open the file ~/.subversion/config
  2. Remove the comment from the line which starts with # global-ignores
  3. The line looks like this now: global-ignores = *.o *.lo *.la #*# .*.rej *.rej .*~ *~ .#* .DS_Store

As you can see the .so file extension is not defined anymore and suddenly .so files also show up in my SVN Commit Dialog (I am using svnX on MaxOSX).

This also explains a bit my most recent issue I was having with a broken Eclipse build.

I am not too sure if this is a good default by SVN to ignore .so files as those are shared objects in Unix Environments which are important for Multi Platform builds in my case. Anyway I found this out and solved the problem.

 

Simple way :

Right click the file, choose Team -> Add to Version Control

Then the file will be able to be commited.

Advertisements

Converting existing sqlite to encrypt SQLCipher Android

If we have an existing application which is running successfully with sqlite database package. Now we need to convert the sqlite to encrypted SQL Cipher. Simple steps to achieve that.

Step 1:                  

Add the code to your UI to prompt the user to enter a passphrase.

Step 2:

Download the SQLCipher for Android ZIP file.

Step 3:

UnZIP the ZIP file and navigate to the directory that has an assets/ and a libs/ folder.

Step 4:

Copy the contents of the assets/ directory into your project’s assets/ directory.

Step 5:

Copy the contents of the libs/ directory into your project’s libs/ directory. Gradle/Android Studio users will also need to add a line to the top-level dependencies closure loading up the contents of libs/, if you do not have one already.

Step 6:

 Replace all relevant android.database.* and android.database.sqlite.* imports with their SQLCipher for Android equivalents. If you are using an IDE that can help you resolve missing imports (e.g., Ctrl-Shift-O in Eclipse), the easiest thing to do is to get rid of all existing android.database.* and android.database.sqlite.* imports and let the IDE help resolve them. Choose the net.sqlcipher imports when given the choice.

Step 7: You will now have compiler errors on a few methods where you open a database (e.g., getReadableDatabase() on SQLiteOpenHelper), where you now need to pass in the passphrase you collected from the user in Step #0.

This will work for new apps starting up with new databases. There is additional work involved to upgrade an existing app with existing users, if you want to allow those users to switch to an encrypted database.

Upgrade an existing app

To update existing sqlite database Use the following methd:

public static void encrypt(Context ctxt, String dbName, String passphrase)

                     throws IOException {

              // Old sqlite file

              File originalFile = ctxt.getDatabasePath(dbName);

              // Check sqlite file existing or not

              if (originalFile.exists()) {

                     // Create neww file path

                     File newFile = File.createTempFile(“sqlcipherutils”, “tmp”,getCacheDir());

                     // Open existing database using SQLCipher

                     SQLiteDatabase db = SQLiteDatabase.openDatabase(getAbsolutePath(), “”, null,SQLiteDatabase.OPEN_READWRITE);

                     // Encrypt existing database

rawExecSQL(String.format(

                                  “ATTACH DATABASE ‘%s’ AS encrypted KEY ‘%s’;”,getAbsolutePath(), passphrase));

rawExecSQL(“SELECT sqlcipher_export(‘encrypted’)”);

rawExecSQL(“DETACH DATABASE encrypted;”);

                     // Get database version

                     int version = db.getVersion();

                    // close the database

close();

                     // Open SQLCipher

                     db = SQLiteDatabase.openDatabase(newFile.getAbsolutePath(),

                                  passphrase, null, SQLiteDatabase.OPEN_READWRITE);

                     // Set version to database

setVersion(version);

close();

                     // Delete origional file

delete();

                     // Rename file to rigional file

renameTo(originalFile);

              }

       }

int vs Integer

Newbies are often confused by the difference between int and Integerboolean vs Boolean,char vs Charactershort vs Shortlong vs Longfloat vs Float or double vsDouble.

Definitions

An int is a primitive. It is not an Object. An int is a high performance, streamlined beast for calculating numbers in the range -2,147,483,648 [-231] aka Integer.MIN_VALUE to +2,147,483,647 [2 31-1] aka Integer.MAX_VALUE. An int is a bare bones 32-bit chunk of information. int variables are mutable. Unless you mark them final, you can change their value at any time.

An Integer, is a Object that contains a single int field. An Integer is much bulkier than anint. It is sort like a Fedex box to contain the intIntegers are immutable. If you want to affect the value of a Integer variable, the only way is to create a new Integer object and discard the old one.

Converting

Fortunately it is easy to convert back and forth between int and Integer.

// to int i from Integer ii
int i = ii.intValue();

// to Integer ii from int i
Integer ii = new Integer( i );

 

Why Both?

Why are there both int and Integer? For speed. ints, without any Object packaging are compact and fast. Would it not have been easier if there were only one of sort of creature that could do everything and have the compiler automatically figure out when the packaging was needed and when not? The Eiffel language designers thought so, and the Java designers are gradually coming around to the same conclusion.

Hiding Files inside an Image file Secretly

Before we move further with the steps.There are something that we need in order to perform this trick. We will need WinRAR Installed on your PC. So if you already have WinRAR archiver in your PC, which most of us do have, you can proceed with below steps

Step 1: 

Create a folder with files that you would like to Hide in an Image file, Lets say the folder name is “Files”. Now Right Click on the folder and select add to archive. Select RAR as Archive format and Click OK.

Step 2: 

You will now see a rar file with name files, Now say you have an image with name “image.jpg” in which we will hide these files. Copy both Files.rar and image.jpg and paste them in ” C” or “D” Drive.

Step 3: 

Next Open CMD, Search for CMD and open it. If you files are in “D” drive then Type D: and press Enter. Now Type in this Code as it is copy /b image.jpg + files.rar new.jpg and press Enter

Step 4: 

If you have done everything right till now, then you will see something Similar like below Picture. I had the file in E: so i initially typed E: to change the location to E drive.

Step 5: 

Now Navigate to the location where you saved the file and you will see one for file added with name New.jpg.

Ok So now we have a new file but what next ? If you notice you will se the size of the new file has increased and if you open the new.jpg file it will show you just the picture, So you will as where the other files gone which we tried to hide inside this image ?

Here is how to Access the files

We know that the file is in jpg format so we will first Open WinRAR archiver, Click on File and Select Open archive.

Now Navigate to the Path where you have saved new.jpg file which will be Drive “D” Now make sure to select All Files from the drop down, so that it displays the new.jpg file too.

Select it and Click on Open, There you will see a folder with name files, Open it and there are your Hidden files.

Out of Memory (OOM) Error

Out of Memory (OOM) Error

Out of memory error is very common error when you are developing for a application that deals with multiple images sets or large bitmaps or some Animation stuff. In this case we have to be very careful and efficient while handling the images or object allocation and deallocation. OOM error comes when the allocation crosses the heap limit or your process demand a amount of memory that crosses the heap limit.

In Android, every application runs in a Linux Process. Each Linux Process has a Virtual Machine (Dalvik Virtual Machine) running inside it. There is a limit on the memory a process can demand and it is different for different devices and also differs for phones and tablets. When some process demands a higher memory than its limit it causes a error i.e Out of memory error.

Possible Reasons:

There are number of reasons why we get a Out of memory errors. Some of those are:

1. You are doing some operation that continuously demands a lot of memory and at some point it goes beyond the max heap memory limit of a process.

2. You are leaking some memory i.e you didn’t make the previous objects you allocated eligible for Garbage Collection (GC). This is called Memory leak.

3. You are dealing with large bitmaps and loading all of them at run time. You have to deal very carefully with large bitmaps by loading the size that you need not the whole bitmap at once and then do scaling.

The biggest reason for Out of memory is a Memory leak. First lets see a memory leak examples:

Examples:

1. We make an Activity and create a TextView with Activity context and set a sample text in to it.

@Override
 protected void onCreate(Bundle state) {
   super.onCreate(state);
   TextView label = new TextView(this);
   label.setText("Leaks are bad");
   setContentView(label);
 }

This means that views have a reference to the entire Activity and therefore to anything your activity is holding onto; usually the entire View hierarchy and all its resources. Therefore, if you leak the Context (“leak” meaning you keep a reference to it thus preventing the GC from collecting it), you leak a lot of memory. Leaking an entire activity can be really easy if you are not careful.

2. When the screen orientation changes the system will, by default, destroy the current activity and create a new one while preserving its state. In doing so, Android will reload the Application’s UI from the resources. Now imagine you wrote an application with a large bitmap that you don’t want to load on every rotation. The easiest way to keep it around and not having to reload it on every rotation is to keep in a static field:

private static Drawable sBackground;
@Override
protected void onCreate(Bundle state) {
 super.onCreate(state);
 ImageView label = new ImageView(this);
 if (sBackground == null) {
   sBackground = getDrawable(R.drawable.large_bitmap);
 }

 label.setBackgroundDrawable(sBackground);
 setContentView(label);
}

This code is very fast and also very wrong; it leaks the first activity created upon the first screen orientation change. When a Drawable is attached to a view, the view is set as acallback on the drawable. In the code snippet above, this means the drawable has a reference to the ImageView which itself has a reference to the activity (the Context) which in turns has references to pretty much anything (depending on your code.).

Now if the orientation changes the Drawable will not come under GC because it is static and due to this the ImageView will also not go under GC operation because the Drawable is linked to the ImageView by the Drawable callback and same for Activity context becauseImageView was created with Activity context. So this lead to a memory leak and every time you change the orientation this leak will grow in the heap and at some point it will increase the heap limit and android will kill the application and throw Out of memory error.

Tools to Diagnose OOM

In Android there are some tools that can be used to find the memory leaks and one of them is MAT (Memory Analyzer Tool). This can be used as a plugin to Eclipse or as a stand alone tool.

Lets start with installing MAT and using it with Eclipse with a step by step guide:

1. To start up with this tool you have to download this tool from following link:http://www.eclipse.org/mat/downloads.php

2. Start Eclipse and run your application.

3. Go to DDMS and select your application and click the Dump HPROF file button and it will ask you to save the hprof file. Save it on your hard disk.

Heap Dump Profiler

4. Run the MemoryAnalyzer.exe file situated in the MAT package downloaded.

5. This MAT tool will not understand the hprof file generated by Eclipse so you have to convert it using the hprof-conv tool in the tools folder of android-sdk using the following command:

hprof-conv com.example.android.hcgallery.hprof mat.hprof

6. Now this file is ready to use with MAT.

How to analyse the Heap dump in MAT ?

Case Study

While doing Android app development, I got Out of memory error. In the following example I will share my problem statement and the way i found the leak and how i resolve it.

Problem Statement:
I was dealing with multiple Drawables that are used in AnimationFragment class to do a frame animation and it was working fine as long as I didn’t check the heap while rotating the device. Every time I was rotating the device the Activity restarts as to load a new layout for landscape mode and than heap grows and this keep on going and then I fall in to OOM error.

Reason:
Actually there was a context leak in my Fragment and every time I rotate the device the previous Context is not destroyed because the Drawable used in the AnimaitonDrawable class were still holding a callback to the AnimationDrawable that in turn stops the Activity to get destroyed or to get collected by the GC.

How I found the reason?
1. I took a dump for my application and convert it using hprof-conv and then open the file “mat.hprof” in MAT.

2. Go to overview option and check the Top consumer option and it will look like following:

Heap Dump Chart

This diagram will tell you about the allocation and the space occupied by every object (widget, drawables, bitmaps etc). Using this you can check which objects are taking more memory than you can look at the code and attack them first. In my case it was AnimatioinDrawable.

Heap Dump Big Objects

There are two heap shown in the diagram lets see them first:

Shallow heap: This is the size in bytes that is actually taken by the variable.
Retained heap: This is the size of the variables with whom this variable is maintaining reference.

To get it better see the following image

heap_explamation

For A the shallow heap is 100 but the Retained heap is 300 because it is holding 300 bytes of memory to be collected by GC.

In the 2nd image we can clearly see that the AnimationDrawable objects are the biggest objects in the heap. So we should target AnimationDrawable first. To see the actual object and its location click on the respective row and go to ListObjects -> with incoming reference and then you will see the following window.

Heap Dump Incoming Reference

Expand this entry to see all the objects and in this case the entries are

Heap Dump Icoming Reference Expanded

Retained Heap is more important for finding leaks, as it is the total size of all objects being kept alive by this dominator. To get this number, MAT needs to calculate the Retained Size by following all object references. As sorting by Retained Heap is very helpful for finding leaks, MAT displays the largest retained heaps in a pie chart.

The selected ones are the objects in my code which are having high retained heap value and the entries are shown with package name and class name. Now you exactly know where are the objects that are taking the most of memory and you can recheck that code.

Solution:
I checked my code by using the MAT reference and then I realize this AnimationDrawable is not getting destroyed and because of this the frames in it are also becoming a part of the leak. So, i clear all the callbacks of AnimationDrawable as soon as the Fragment is destroyed and after this heap was stable and when orientation changes the heap decreases as the Drawables got destroyed and than increases for the new allocation.

There can be a no. of reasons for a OOM but the main reasons mostly ends in a Leak. So if get closer to the leaks than you can easily get rid of OOM.

Getting a Heap dump at runtime:
When you are getting a OOM error and you want to check the heap and allocation when the error comes than you can add some code that will catch OOM error and than calling the Debugger to dump the Heap from our code. Lemme show you a example to do this:

1. Make a OOM error Listner as a inner class that will implement Thread.UncaughtExceptionHandler

public class MyActivity extends Activity {
 public static class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
 @Override
 public void uncaughtException(Thread thread, Throwable ex) {
 if(ex.getClass().equals(OutOfMemoryError.class))
 {
 try {
   android.os.Debug.dumpHprofData("/sdcard/dump.hprof");
 } 
 catch (IOException e) {
   e.printStackTrace();
 }
 }
 ex.printStackTrace();
 }
 }

2. In your Activity’s onCreate set a listener on the current thread that will catch the OOM error

@Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

Thread.currentThread().setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
}
}

In this way you can check the heap dump at the point when OOM occurs.

Best practices to avoid memory leaks or OOM

  • Do not keep long-lived references to a Context / Activity (a reference to an Activity should have the same life cycle as the activity itself)
  • Try using the context of application instead of a context of activity
  • If you are using a large bitmap as background or something in your application than don’t pull the full image in to the main memory. You can use the insample size property of bitmap to bring the size your screen needs.
  • Memory fragmentation: You should plan the allocation of the variables so that after deallocation the memory should not be scattered or fragmented. If 10 MB memory is available in chunks and process requests for 2 MB and couldn’t find a chunk of 2 MB then it will ask for more heap and this in turn can cause OOM.
  • Sending large files to server should be done in chunks because loading a large image in bytes can cause OOM.
  • A activity that is using so many images should remove the callbacks to the drawable used by calling setCallBack(null) on the drawable to avoid OOM. 

Spinner selection not working Listview Android

When the ListView is loaded with Spinner in each row then the spinner selection is not static. It change. because of the recycling property of ListView.
Sample MainActivity.java:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

public class MainActivity extends Activity {
SpinnerAdapter ratingAdapter;
ListView listView;
List listString;
ArrayAdapter dataAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listString = new ArrayList();
for(int listCount = 0; listCount < 20 ; listCount++){
listString.add("ListCount"+listCount);
}

List spinnerData = new ArrayList();
for (int i = 0; i < listString.size(); i++) {
spinnerData.add("MyTest="+i);
}

dataAdapter = new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1, spinnerData);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

fillAdapter();
}

private void fillAdapter() {
ratingAdapter = new SpinnerAdapter(getApplicationContext());
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(ratingAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

class ViewHolder {
Spinner spinner;
}

class SpinnerAdapter extends ArrayAdapter {
Context context;
HashMap selectedItems = new HashMap();

public SpinnerAdapter(Context context) {
super(context, R.layout.item_list_spinner_adaoter, listString);
this.context = context;
}

public int getCount() {
return listString.size();
}
public Object getItem(int position) {
return listString.get(position);
}
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item_list_spinner_adaoter, null);
holder.spinner = (Spinner) convertView.findViewById(R.id.spinner1);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.spinner.setAdapter(dataAdapter);
if ( selectedItems.get( position ) != null ) {
holder.spinner.setSelection( selectedItems.get( position ) );
}
holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView arg0, View arg1,
int arg2, long arg3) {
selectedItems.put( position, arg2 );
}

@Override
public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub

}
});

return convertView;
}
}
}

Building Android applications using Android Annotations

If you have previously written applications for Android, you already know that there is a lot of code that has to be written over and over again. It not only takes more time to write the initial version of the code, it makes it so much more harder to manage and maintain. Android Annotations facilitates the writing and maintenance of Android applications by simplifying this process for you and taking out most of the repetitive code. Here’s a small example of how simple it makes to do common things (code sample taken from AndroidAnnotations Wiki):

@EActivity(R.layout.translate) // Sets content view to R.layout.translate
public class TranslateActivity extends Activity {  
    @ViewById // Injects R.id.textInput
    EditText textInput;

    @ViewById(R.id.myTextView) // Injects R.id.myTextView
    TextView result;

    @AnimationRes // Injects android.R.anim.fade_in
    Animation fadeIn;

    @Click // When R.id.doTranslate button is clicked 
    void doTranslate() {
        translateInBackground(textInput.getText().toString());
    }

    @Background // Executed in a background thread
    void translateInBackground(String textToTranslate) {
         String translatedText = callGoogleTranslate(textToTranslate);
         showResult(translatedText);
    }

    @UiThread // Executed in the ui thread
    void showResult(String translatedText) {
         result.setText(translatedText);
         result.startAnimation(fadeIn);
    }

    // [...]
}

Including Android Annotations in a project is very easy. We just need two JARs, one for the Android Annotations API and another for processing the annotations at compile time. Begin by downloading these JARs from here. We now need to add the JARs and enable annotation processing in eclipse.

  • Put androidannotations-X.X.X-api.jar in the libs folder.
  • Put androidannotations-X.X.X.jar in a different folder, such as ext-libs.
  • androidannotations-X.X.X.jar must not go in the libs folder
  • In project properties, set Compiler Compliance level for Java Compiler to 1.6
  • In project properties > Java Compiler > Annotation Processing, make sure that Enable Annotation processing is checked.
  • Add androidannotations-X.X.X.jar in Java Compiler > Annotation Processing > Factory Path

I will list some of the most important features that I have found useful while working with Android Annotations. To create an activity, we just need to annotate a class that extends Activity with @EActivity with a parameter for the content view for the activity.

 

@EActivity(R.layout.main)
public class MyActivity extends Activity {  
    @ViewById(R.id.myTextView)
    TextView myTextView;

    @AfterViews
    void afterViews() {
        // TODO: Do something after the views are injected
        myTextView.setText("Hello World!");
    }

    @Click(R.id.myButton)
    void onMyButtonClick() {
        // TODO: My button clicked. Do something
    }

    @ItemClick(R.id.myListView)
    void onMyListViewItemClicked(int position) {
        // TODO: Item at position clicked in my list view. 
    }
}

You will notice in the example that we didn’t have to set the content view for the activity in onCreate() or to manually set the onClick or the onItemClick listeners for the myButton and myListView or to find the views by id. Android Annotations takes care of most of the things and makes the code more readable and easier to write and maintain. And this is just the beginning. It handles a lot more things. It does this by generating a class that extends your annotated class. For this reason, while registering our Activity in the manifest file, we must register the file that Android Annotations generates and not the original annotated file that we write. This is very important because otherwise, we wouldn’t see anything as we didn’t actually set the content view in the annotated file. Instead of registering MyActivity, register MyActivity_ in AndroidManifest.xml.

<activity android:name=".MyActivity_" />  

You should always remember to refer to MyActivity_ instead of MyActivity everywhere else in the code.

Another heavily used feature in Android is the support for Fragments. Android Annotations supports fragments too. All you need to do is to annotate a class that extends Fragment with @EFragment and you can use all the annotations that you were able to use above with Activities. To inject a fragment in an activity, you can use the @FragmentById or@FragmentByTag tag. Make sure that you use the generated class name for the fragment (with the _) rather than the original one whenever you refer to the fragment. Android Annotations doesn’t create the fragment for you. So also make sure that the fragment should exist either in the layout or created programmatically in onCreate or some other method.