Tuesday, 10 November 2015

Wrap-up JavaOne'15

It might be a little late for the post but there is much to be said..

Another JavaOne is over, not only a JavaOne but 19th JavaOne on 20th year of Java!!

I just feel JavaOne is turning into a old friends gathering event for me than the technical conference it used to be. The conference is shrinking in terms of contents and number of sessions while Oracle Open World expands to sessions rooms which were part of JavaOne.

Don't get me wrong, I still enjoy going JavaOne, meeting friends and community. However, variety of sessions become much limited. The 20th year of Java keynote must have been the most boring one except for the part Scott McNealy was on screen. Actually it might have been the only worth to watch part of the whole keynote!

So what happened... why J1 is just fading away? Is java and us getting old?

It is not a secret that J1 has lost a lot of blood after being kicked out Moscone in 2010 but it got worsen with loosing the mobile track with the going down of Java ME. Since moved out of Moscone J1 was hosted in the Hotel trio Hilton, Nikko and Park55. However this the trio has become duo leaving another hotel to Oracle Open World. I really wonder if OOW need so many rooms (Moscone South/North/West and now an additional hotel!) while j1 is shrinking.
Also this year before J1, Oracle terminated contracts of many veteran Java Evangelists which left most fun part of Java and J1 in the dark. One may argue that Java does not really need evangelism anymore but than where was the fun stuff such as minority report demo at the keynote?

So what to do now? How can we bring back the spirit?

Here are my 2 cents to Oracle...

- I respect and understand you want to highlight stuff you are doing in the keynote but believe me it is boring. Either find a fun way to talk about it or just delegate this to session speakers. For the last 3 years we are just listening the same jigsaw and lambda slides and how Oracle is investing and engineering java! For the keynote, for once, forget marketing and make fun!
- Let's face it Java ME is long time dead on mobile but may re-born for IoT. This year was a good sign that Java ME can really shine with IOT but thats it.
- So Java lost the mobile phones?? No, hell no.... Even if you like it or not, Android has become the most widely used mobile OS and even if it does not run on JVM, it is still the Java language! The lawsuit is over, make peace and let Java survive on mobile! Let's face it what will happen if google walks away from java on android! Be wise Oracle, even Microsoft is investing on Android...
- Please, please, please keep j1 keynote in moscone!
- Finally, we are on the same boat Oracle... please don't just let it sink..

Tuesday, 14 July 2015

Permissions in Android M Preview

Milkyway? Well I guess so but macaroon, muffin, marsmallow are still good candidates to become the name of the new Android version. We have more to make guesses until the final version is relased in quarter 3. Following last years tradition of releasing a preview version at the time IO, Google has already released M Preview. Unlike last year this preview release pretty much functional and two more previews will be release (end june and end july) before the final version.

Let's forget about the naming and deep dive into technical parts of 'M'. If you are planning to have hands on experience on M, the system images for your devices are located here. If your android studio is subscribed the preview channel for sdk updates, you should have already been informed about an available sdk update which is called "android-MNC".

The most significant change with M is the permissions. Android applications ask for permissions before the installiation of an app. Once the app is given the permissions it is asking for, disabling a permission is not an option for the user. This was a simple design decision (both for users and developers) but introduced undesired app behavior for a user. However, on M, permissions are granted or denied at runtime.

If the permission is denied the app may receive an empty object, a null object or may even got a security exception. Since apps relied on permissions to be granted automatically until M, this would introduce an important change to legacy apps.

If you do not update you app compiled with target sdk pointing M, the application will ask for list of needed permissions at the time of install. However, the user can always turn of a granted permission which leaves the app to deal with denied permission. A dialog saying the app may crash will be shown while if user disables a permission. If you compiled your app with M as the the target sdk, the app will install silently and ask for each needed permission at the time execution.

This a big change which may effect previous code. Simply you need to revisit your old apps make sure you handle the cases where permission is not granted. In most cases the app should recover and continue working.

I think this a very good change. Users tend to not read permissions while installing (do any of you read agreements?) but asking permissions in runtime always gets the attention of the user. Plus the user would realize what current context and judge if the permissions makes sense.

Welldone Android team, let's see how old apps will survive the change..

Monday, 9 March 2015

Hitchhiker's Guide to Modern Android Development: The Pitfalls and Cryptic Errors

No platform or language is perfect, each has its own problems, pitfalls and cryptic errors of its own. Thanks to Java heritage, Android usually offers smooth, easily learning curve with a huge database of blogs, forums and stackoverflow answers.
After running and mentoring the Study Jam series for two weeks, I decided to compile a set of common pitfalls developers came across which tend to be cryptic or not google friendly.

Error: Gradle DSL method not found: 'runProguard()'
Possible causes:<ul><li>The project 'XX' may be using a version of Gradle that does not contain the method.
<a href="open.wrapper.file">Open Gradle wrapper file</a></li><li>The build file may be missing a Gradle plugin.
<a href="apply.gradle.plugin">Apply Gradle plugin</a></li>

This is one of the most cryptic error you may come across on Android. If you have been using Android Studio from Beta, you will/did probably face this error after upgrading to gradle version 0.14.0 (Android Studio 0.9). Simply the Android Tools team decided to rename runProguard to minifyEnabled. Similarly zipAlign has been enabled to zipAlignEnabled. You may find the full list of changes here.

Error:The project is using an unsupported version of the Android Gradle plug-in (X.X.X). The recommended version is X.X.X.
<a href="fixGradleElements">Fix plugin version and re-import project</a>

Similar to previous issue, this problem occurs each time you get an update to Android Studio and Gradle. Unlike the previous one, this can easily be solved by clicking the Fix plugin version link provided in the Gradle Messages window.


You may come across this error if it is your first network call ever or you are updating an old app to post Honeycomb. Beginning from 4.0 (Ice Cream Sandwich) Android OS does not allow performing network operations on main (UI) thread which should have been this way since day 0. The main thread's main responsibility is running the UI smoothly. Networking, file i/o or any other lengthy operation will block the UI updates resulting in non smooth UI/UX. Networking might be the worst of all since the speed of the connection can rely on many different factors.
The solution is simple. Anything which is not part of UI should not be done in the main thread. Android offers an easy way to use threads which is called AsyncTask. Simply use an AsyncTask to perform lengthy operations.

I use an AsyncTask but still getting android.os.NetworkOnMainThreadException

To start an Asynctask, you need to call the .execute method on the Asynctask object. Calling the .doInBackground method will bypass the creation of a new thread and run the network operation on the main thread.

java.lang.SecurityException: Permission Denial: ... requires Android.permission.X

Android is based on linux and each app runs on its own sandbox with very limited permissions. If you need access a common system resource such as camera, bluetooth, internet or file system, you need to ask users permission. The permission are added to AndroidManifest.xml between the manifest and application tags. You can refer this for a complete set of permissions which can be added with uses-permission tag.

I added the permission java.lang.SecurityException: Permission Denial: ... requires Android.permission.X

Having a valid AndroidManifest.xml does not necessarily mean it is just working. Adding the uses-permission tags in the application tag, does not break the xml structure but doesn't reserve the permissions you asked either. List your uses-permission tags out of the application tag.

Permission Denial: … requires android.permission.WRITE_EXTERNAL_STORAGE

If you are sure you reviewed the previous two items but still getting the error, this should be because of the android:maxSdkVersion="18" property inside the uses-permission tag. To allow your app to use the write permission above version 18 you need to remove maxSdkProperty.
<uses-permission android:name=“android.permission.WRITE_EXTERNAL_STORAGE"/>

My Fragment's overflow menu displays a menu item (settings) not in menu.xml

Fragments can add their own menu items by overriding onCreateOptionsMenu(). However this does not mean cleaning up the menu items added by the activity. Since a fragment is always hosted by activity and uses the activity's context, it would inherit such stuff from the hosting activity. The item which you believe (settings is given as the default menu item) is not relevant belongs to the activity. Navigate the menu.xml which belongs to the activity and remove the unwanted item(s).

The method replace or commit in the type FragmentTransaction is not applicable for the arguments 

You are passing a fragment object to a method which is expecting a fragment object but still complaining? Well, not all fragments are the same. Java's design allow classes with same names as long as they are in different packages. Android SDK team decided to introduce the fragment support package with the same class name (Fragment) but in a different package (android.support.v4.app.Fragment vs android.app.Fragment). Navigate to imports and remove the wrong import and add the appropriate one.

onOptionsItemsSelected(), onCreateOptionMenu(), afterTextChange(Editable s), onPostExecute() or any other method which should have been called is not called?!?

The @Override annotation must have been the most underrated annotation of all time. It exists for a very specific reason. Unlike C++ every method is virtual in Java. Thus, unless it is final any method can overriden. The @Override simply checks if the implementation is really overriding a method from the parent class. All the methods above has a typo which any level of developer may do. Removing the override annotation also removes the ability of the compiler to detect any typos in the so-called overriden methods. This typos may occur in the method or even in the parameter list. Simply if a listener or a method which should have been called will silently failed to be executed. You can easily detect such errors by placing the @Override to every overriding method.

Wednesday, 28 January 2015

Finally my book is out: Professional Java EE Design Patterns

Finally it is on ink and paper! Well or on a screen if you prefer :)

It was quite challenging and tiring to write a book which might be the most underestimated thing I have done in my life! I was lucky to have a great co-author and great people from Wiley (Adaobi, Mary, Jim...) without whom i would definitely end up just putting those into my blog.

You may find more details below and click on the pic to buy from Amazon, hope you enjoy ;)

Master Java EE design pattern implementation to improve your design skills and your application’s architecture.

Professional Java EE Design Patterns is the perfect companion for anyone who wants to work more effectively with Java EE, and the only resource that covers both the theory and application of design patterns in solving real-world problems. The authors guide readers through both the fundamental and advanced features of Java EE 7, presenting patterns throughout, and demonstrating how they are used in day-to-day problem solving.

As the most popular programming language in community-driven enterprise software, Java EE provides an API and runtime environment that is a superset of Java SE. Written for the junior and experienced Java EE developer seeking to improve design quality and effectiveness, the book covers areas including:
  • Implementation and problem-solving with design patterns 
  • Connection between existing Java SE design patterns and new Java EE concepts 
  • Harnessing the power of Java EE in design patterns 
  • Individually-based focus that fully explores each pattern 
  • Colorful war-stories showing how patterns were used in the field to solve real-life problems 
Unlike most Java EE books that simply offer descriptions or recipes, this book drives home the implementation of the pattern to real problems to ensure that the reader learns how the patterns should be used and to be aware of their pitfalls.

For the programmer looking for a comprehensive guide that is actually useful in the everyday workflow, Professional Java EE Design Patterns is the definitive resource on the market.