Sie sind auf Seite 1von 7

Mobile, Android, iPhone, Java and me: What a java programmer missing

Pgina 1 de 7

Compartilhar

Denunciar abuso

Prximo blog

Criar um blog

Login

Mobile, Android, iPhone, Java and me


Thursday, July 14, 2011 Facebook Share

What a java programmer missing


Recently I have some opportunities to look into Android code from some highly skilled java developer. Codes are fine on the aspect that they are following all the standard java methodology. But, those standard codes are not good for android. And so many cases those codes will crash. So it keeps me thinking what things a Java developer is missing? I tried to dig little bit deeper. Looking into code and try to figure out really what is happening. Following are the summary or list of issues I think all java developer should concern about when they are working for Android. I believe the first key things for developing for any mobile platform is the mindset. You are no more developing something for PC platform. Always think your application will run on small tiny devices which has very small amount of memory and processing capabilities. This mindset will help you to write optimize code. Do not think that you know java so you will be able to write good code for Android. In so many cases Android is so much different from regular java. Android code run on tiny devices, so be cautious when writing code. Know where the places are android coding is different from standard java coding. Get the list of packages what are available and what are not. Memory is very important part where all wannabe android developer should concern about. On desktop there is no reason to worry about memory. J2SE jvm heap hardly exceeded range. But on Android, heap out of range is very common phenomenon especially when you work with images. Following error on Android is very common and very frustrating. 01-06 04:40:58.668: ERROR/dalvikvm-heap(263): 1463040-byte external allocation too large for this process. 01-06 04:40:58.698: ERROR/GraphicsJNI(263): VM won't let us allocate 1463040 bytes 01-06 04:40:58.698: DEBUG/skia(263): --- decoder->decode returned false 01-06 04:40:58.698: WARN/dalvikvm(263): threadid=1: thread exiting with uncaught exception (group=0x40015560) 01-06 04:40:58.708: ERROR/AndroidRuntime(263): FATAL EXCEPTION: main 01-06 04:40:58.708: ERROR/AndroidRuntime(263): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 01-06 04:40:58.708: ERROR/AndroidRuntime(263): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 01-06 04:40:58.708: ERROR/AndroidRuntime(263): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460) 01-06 04:40:58.708: ERROR/AndroidRuntime(263): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336) 01-06 04:40:58.708: ERROR/AndroidRuntime(263): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) 01-06 04:40:58.708: ERROR/AndroidRuntime(263): at android.content.res.Resources.loadDrawable(Resources.java:1709) 01-06 04:40:58.708: ERROR/AndroidRuntime(263): at android.content.res.Resources.getDrawable(Resources.java:581) 01-06 04:40:58.708: ERROR/AndroidRuntime(263): at android.widget.ImageView.resolveUri (ImageView.java:501) 01-06 04:40:58.708: ERROR/AndroidRuntime(263): at android.widget.ImageView.setImageResource(ImageView.java:280) Complexity analysis, can you do that? After writing a function, try to count space and time complexity. Think how you can simplify your code. What could be better O(n) for the same function. Remember, in computer science all solutions are feasible solution and there is always a better solution out there. When you are working on mobile device take extra precaution when creating new variables. Ask your self do I really need a new variable? Why I have to create a new one?

0
shares Share

UpTweet

0
uptweets

uptweet

Blog Archive 2011 (2) July (2) Android and its Fragmentation What a java programmer missing 2010 (2) 2007 (1)

http://minhazr.blogspot.com/2011/07/what-java-programmer-missing.html

20/08/2011

Mobile, Android, iPhone, Java and me: What a java programmer missing

Pgina 2 de 7

Why cant I use previously defined variables? Remember variable declaration is expansive and it is way more in the case of mobile platform. Do you need an int or an integer tries to stick with primitive type if you can. Fragmentation: Java is platform Independent what basically means that written java code will run on any platform without any problem. Unfortunately this thing is not true for android. Android has serious fragmentation issues. There is always a possibility that your nice little cozy app will not run on another platform. Or, maybe it will run but it will crash thousand times. Why? Because of fragmentation. Well, android devices have limitation. They do not come with same internal memory processing capabilities. So if you test your apps on 500Mhz processor and later an android user whose phone has less processing capacity what will happen on his case? Simply it will take more time. And, for example if a network operation used to take 50 millisecond now it is taking 100 milliseconds, enough time for android platform to get board and kick your app. In this few years I have couple strange experience with android and its fragmentation. So yehh this is our life as an Android developer. Boring. Assumption is another very bad thing for android developer. Do not assume. Do not assume that your user has an active network connection. If the network connection is active probably it is somehow jammed. Probably another screw-up app screwing with network interface. Always assume worst case, when writing android code be pessimistic, or try to be super pessimistic. Always think that something will not work. Probably a new will not create a new object. Initializer will fail, Hard-coded URL value will throw malformed URL exception, or something bad will happen. That is why you will always write code defensively. When getting value try to be cautious. It could happen that one function will return error or null. if it is possible try to be extra cautious. For example: public boolean getDisplay(){ int height, width; Display display; display = getWindowManager().getDefaultDisplay(); if (display!=null){ width = display.getWidth(); height = display.getHeight(); return true; } display = ((WindowManager) getSystemService(this.WINDOW_SERVICE)).getDefaultDisplay (); if (display!=null){ width = display.getWidth(); height = display.getHeight(); return true; } DisplayMetrics metrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(metrics); if (metrics!=null){ height=metrics.heightPixels; width=metrics.widthPixels; return true; } return false; } When you will see this code probably you will say i am paranoid, but it is also true probability of returning false from this function is slim Do you forget to handle localized code? Android has nice and easy ways to handle different localize issues. Do you even know what this is? Try to read more on this from developer.android.com site. ANR: How many of you out there hate ANR? Well, I do. When writing code always think of ANR. Your nice little cozy code may suspect to ANR anytime. Carefully review your code of IO, network and database communication. For ANR assume the following 1. all time consuming calculation will suspect to ANR (ex. SQL Query) 2. All network communication will suspect to ANR

http://minhazr.blogspot.com/2011/07/what-java-programmer-missing.html

20/08/2011

Mobile, Android, iPhone, Java and me: What a java programmer missing

Pgina 3 de 7

3. Any processing blocking or delaying UI to be drawn on display will suspect to ANR. Warnings are not warnings; you may ask if I am crazy. No, if you want stable apps your code should be warning free. If you have 100 warnings on your application it means there are 100 cases you app will screw up. Treat all warning as error. Is there anyone reviewing your code? Software engineering is all about technique. People do mistake its normal and thats including your rockstar developer. Always make sure that code is reviewed properly by others and that including yours. You will be surprises to see what type of mistake people make and how creative people are on problem solving techniques. When planning to create a library for you applications create an android project and make it Android library do not come up with java library. ADT has great optimization and compilation techniques on the case of Android project. External Storage: SD card is a great place to store information. It is easy to access, spacious and all phone has one. So whats wrong with it? There are basically two mistakes all the programmers do most of the time 1. in access time 2. storing data time What happen in access time? Consider the following lines of code Public static final String FILE_ACESS = "/mnt/sdcard/twitter_acess.txt"; And, later you can store information File storage = new File(FILE_ACESS); Developer of this code accessing external storage assuming that on Android sdcard is mount as sdcard. what happen if external storage is not mount as sdcard. it is basically better to ask android platform the name of the external storage. You can replace this piece of code by : public static boolean isExternalStorageSpaceAvailable(){ String state = Environment.getExternalStorageState(); if (state!=null){ if (state.equals(Environment.MEDIA_MOUNTED)){ File path=Environment.getExternalStorageDirectory(); if (path!=null){ //file creating code return true; } } } return false; } You can read more on this here. Usually external storage is spacious so people use it for storing massive amount of data and also for security tokens. Bad thing of storing information on SD Card is that it is no longer secured by Android platform so any application can access that information. Anyone can steal information and can be verified with that security token. So the best course of action will be encrypting all sensitive data before storing on SD card. Well you might dont want to encrypt your music video but at least with security tokens, password and other sensitive information. Portability: when think of your mobile application think of portability. Think how you can reach maximum number of mobile user. Think how you can develop your app that will run on most of the application. One now come up and say come on java is platform independent. yehh you are right. it is but Android has big issue with Fragmentation. So what we suppose to do? We have to write code such a way that it run of maximum number of devices. if you are using some external funky features available on only on latest Android version why not then dive into android source code and copy that module and put with your package. This way you can confirm that your application will run on maximum number of devices. You can also choose older Android version inorder to compile your code. So if you compile your source code on SDK version 3 hopefully it will run without an issue on all (brand/model) SDK versions 3 and forward. Aeroplane Mode: So what is aeroplane mode? Oh!! yehh we all know it now-a-days. And, how it is going to help us? Check your program on aeroplane mode. Some very stable apps behave differently when it comes to the case of aeroplane mode. by did you test your apps with low signal cases, go to some basement where there are possible no signal. This is very

http://minhazr.blogspot.com/2011/07/what-java-programmer-missing.html

20/08/2011

Mobile, Android, iPhone, Java and me: What a java programmer missing

Pgina 4 de 7

important dont forget it. Do not write or use java style asynchronous or multi-threaded code. Android has couple nice and cozy Asynchronous class specially made for mobile environment. Write android style code. Code optimization is another important part of mobile platform. Always try to write optimized code. If number of thread in your thread pool is one you do not have to create a thread pool. It can be done without a thread pool. Maintaining a pool will create an extra overhead on processor. There are couples of techniques you can follow to write optimized java code. details you can view from this site. Call Back Function: Function pointer reappeared on java as callback function. It is an awesome feature for so many cases but on Android it could be yehh!!! Not so good. There are some cases using call back function Android is not wise. Case like, when outcome of your callback function depend on network communication. It could take longer time depend on network. Probably user will close the app or Android platform will call ANR. Android has more advance classes to resolve issues like that. And at the end, read blog, technical articles on Android. There are lots of articles from Google with sample code at developer.android.com. Read those blog find out your solution there. Some of the blog even has step by step code for beginners. Posted by Minhaz Rafi Chowdhury at 2:41 PM Labels: Android, Android Code java, Java, Java Android, Java code Android

0 comments:
Post a Comment

Newer Post

Home

Older Post

Awesome Inc. template. Powered by Blogger.

http://minhazr.blogspot.com/2011/07/what-java-programmer-missing.html

20/08/2011

Mobile, Android, iPhone, Java and me: What a java programmer missing

Pgina 5 de 7

http://minhazr.blogspot.com/2011/07/what-java-programmer-missing.html

20/08/2011

Mobile, Android, iPhone, Java and me: What a java programmer missing

Pgina 6 de 7

http://minhazr.blogspot.com/2011/07/what-java-programmer-missing.html

20/08/2011

Mobile, Android, iPhone, Java and me: What a java programmer missing

Pgina 7 de 7

http://minhazr.blogspot.com/2011/07/what-java-programmer-missing.html

20/08/2011

Das könnte Ihnen auch gefallen