Android Development Best Practices, Tips and Solutions

This story is dedicated to solution of common problem, which a Android developer encounter during development of Android application. Though we have https://stackoverflow.com/ and other sites, where Android developer can post their development related queries and get answers to their queries from Android development around the world. But main problem with stackoverflow site is that if you didn’t get answer of your queries after searching on stackoverflow , you have to frame your question in proper wording as per standard of stackoverflow and if you don’t frame your question correctly they down vote your question and people won’t take interest in answer your queries. Also they block you to ask further question on stackoverflow. IMO Android developer’s search on stackoverflow, Google when they stuck some where during their development and they want solution of their problem/queries ASAP. In such situation they don’t have enough time to frame their question as per the standard of stackoverflow. If you guys agree with me then this story for all of you. I am adding here solution of problem, which I encounter during Android application development and I spend hours to find solution of these problems. As I encounter problem’s and find solution of those problems, I will keep updating this story.

  1. px to dp converter : During UI development we frequently need to convert dp to px and px to dp. I found https://pixplicity.com/dp-px-converter quite handy. This site help us in converting dp to px and px to dp on different device density.
  2. Json to Pojo converter: A common thing during Android development is that we frequently need to convert json to pojo. We can use http://www.jsonschema2pojo.org/ to convert json to pojo. This site allows to select source type and annotation type for converted pojo object.
  3. Convert Json data to Object Data : Android developer frequently need to convert Json data to Object data , which he get through network call or through some source, then https://jsoneditoronline.org/ is a helpful tool which help in converting Json Data to Object data.
  4. Add html in String.xml : Some time we need to add html in String.xml, we can use <string name=”about_us_text”><![CDATA[ ”about us html ”]]></string> to add html in string.xml
  5. Add android:fillViewport=”true” to make constraint layout to occupy whole screen when we enclose it in scroll view.
  6. Use of @Bind : When we use dagger2 for dependency injection and for Interface injection field we want to inject implementation of that field and if @provides doesn’t work then we should consider using of @Bind to make it working.
  7. Make textview as clickable link : To make textview as link we should remove autolink “web” from textview xml and then add setMovementMethod().
  8. Make Image stretchable and shrinkable : Android system has capability that it automatically stretch and shrink images as per device density. So we have to add image in base device density folder in Android studio and it get automatically stretched and shrink on different devices as application runs on that. Ofcourse we need to take care of tablets and phones resolution.
  9. Choosing Base Device for UI design : IMO we should consider a device with density XXHDPI as base device for UI design for both tablet and phone , because this density is now very common for current set of device which we have available in market. Also just in case if you required different size of image for different density devices then you can think of XXHDPI images as 3x , get 4x for XXHDPI, 1x for MDPI , 2x for HDPI and 3x for XHDPI.
  10. Encryption using AndroidKeyStore : We can store sensitive information using encryption and storing keys in “AndroidKeyStore”. The mistake I was doing that on every app start I was generating keys and storing in AndroidKeyStore and due to this decryption get failed every-time I close and start application. To solve this problem I stored a flag in sharedpreferences and whenever I start application I check this flag before generating new encryption key and storing in AndroidKeyStore. If this flag is true it means I have already encryption key present in AndroidKeyStore otherwise I generate new encryption key and store it in AndroidKeyStore.
  11. Resolve conflicting libraries or classes : Most of the time we face problem of conflicting libraries in gradle file. Either Android studio give this warning or we know during compilation. Solution of this problem has two steps first step is identifying conflicting library and then either remove that library , exclude that classes which creating conflict or use same version of conflicting classes. To know which library is creating issue you can see compiler error which will be look like “program-type-already-present-com-google-android-gms-internal-zzfq” or sometime Android Studio also show warning. We can also use ./gradlew app dependencies to know dependencies of libraries with set of duplicate libraries. We can exclude a module/dependent library from a library as below
implementation("com.lyft:lyft-android-sdk:$rootProject.lyftSdkVersion") {
exclude group: 'org.jetbrains', module: 'annotations-java5'
}

12. Copy Code from Develop Branch to Master Branch Without merging: Sometime we just want to copy one branch code in to another branch without merging in another branch. In my case I wanted to copy develop branch code in to master branch. First I tried with merge, but some body already merged develop branch and reverted it. So when I tried to merge develop in master some of the changes was not refelecting and it was looking tedious to merge develop branch code in master by manually copying files and resolving coflict. I changed my approach instead of merging develop in master. I merged master in develop using below command

git checkout develop
git merge -s ours master

and finally

git checkout master
git merge develop

Now both develop and master both are having identical code.

If you like this story, please hit on clap button provided below . Your valuable suggestions and comments also welcome.

Thank you.