Android Refactor – Part 2 – Android Studio
In the last post, I walked through the first steps taken to refactor my Android app. The end result was a much more dynamic layout and an easier way to add any new content in the future. In this post, I’ll be detailing the tools available to you in Android Studio that can help clean up the remnants of my bad code.
Android Lint
While some might assume linting is just a basic step, the linting tool in Android Studio / IntelliJ is quite powerful and very useful. You can access it by selecting Analyze -> Inspect Code, which then presents some options to refine the examination. The Inspection scope can be the entire project, or if you have any module dependencies in your project that you don’t want to inspect, you can select by Module.


The results of the inspection are categorized into descriptive sections so that you’re not overloaded with errors and warning (Visual Studio, take note!) Each section has further sections within that contains the individual warnings. These can range from simple suggestions such as making a field local or reducing access privileges, to deleting unused resources and probable bugs in your code. In my app, even though it’s quite small I still have over 150 items to correct. Some can be typos, which in most cases is generally OK, but if you want to write clean code, the typo needs to make sense.
Drawables and Resources
Every app has it’s own resources, usually consisting of images, icons, and maybe some audio/video files. Memory management is still an important issue for mobile development even though phones are getting more and more RAM pumped into them. So if you’re building an app, keep an eye on your memory usage to see if there are any problems. Android Studio has a built-in real-time memory monitor (Alt+6 is the shortcut) that displays a graph of your app’s memory usage. This tool has helped me solve quite a few performance issues with various apps and with various causes. While I was adding content to my Goofs & Gafs app, I noticed a huge spike in memory usage . Originally running between 4-10MB, it now ran at 75MB+!! What?! I couldn’t believe what I was seeing. Something wasn’t right.
I had added various new elements, so I would’ve given some lee-way for an increase, but this was too large an increase for that. After some investigation, I determined it was caused by a single image used. I had set the background of a View to an image which was less than 200KB. Strange, I thought, this should be ok? Upon further inspection, I discovered that this image was huge! It was over 2000 pixels in width and height, something that I overlooked due to the file size. Reducing this considerably in Adobe Photoshop solved my issue, and my memory usage was back to below 10Mb. This is due to how Bitmaps work with Image loading on Android. I was loading such a large resolution image into a View that didn’t need anything close to that, and so the memory wasted was substantial. Some details about this can be found at this post on the Android Developer documentation, and some useful code is kindly provided! (Note that I haven’t used this code in my situation, but it is planned to improve memory usage)
Icons
For icon resources in your app, it’s important to consider screen size and density, as this can seriously affect your app. Utilising the different density versions of icons is all that needs to be done, which is much easier using the Android Drawable Importer plugin. Although Android Studio 1.5+ comes with a similar tool, this plugin offers slightly more functionality, such as being able to search icons. In either case, the icons are Material Design styled icons and when imported, they can be set to various screen densities, automating the process for you! All that needs to be done by you is to pick what icon you want, select the desired densities, and voila! (Unfortunately it’s a bit more complicated with custom images)
That’s it for now, this post will get updated with some more tips & tricks soon once I get some more time.
Extra – Check out Harry Singh’s Medium post series, Android Studio Tips and Tricks where he provides some info and examples of some great shortcuts to shave off a lot of time during development in Android Studio!