Ready to Start Your Career?Create Free Account
February 28, 2017
Let’s Build an Android App
February 28, 2017
February 28, 2017
It’s now time to roll up our sleeves and get our hands dirty with a little mobile app development. This next post in the series on Mobile App Development will focus on putting things in place for developing Android mobile apps. The last time I had my hands on Android code was over two years ago – a lifetime in technology years. Back then my PC was running Windows 7 and my Android IDE was Eclipse. In the interim, I upgraded to Win10, had it crash hard and was forced to reformat and reinstall Win10. Oh, and I went through a career change where the only coding I do now is inserting the occasional emoticon in email correspondence. Does that count? So in effect, I’m beginning with a clean slate, which has turned out to be a good thing since we'll be at the same point if you’re just getting started.
Not your father’s IDEI was a big fan of the Eclipse open-source IDE for many years. For Java development, it was the de facto IDE. Competing IDEs emerged over the years with NetBeans and InteliJ IDEA being its main competitors. Since Java is the language the Android SDK is written in, Eclipse was the natural choice for an IDE. Google began working on their own IDE based on InteliJ IDEA a few years back. The initial versions were clunky and it was a challenge to migrate Eclipse projects. The latter reason is why I stubbornly remained with Eclipse despite the writing on the wall that Google was going to eventually jettison Eclipse in favor of Android Studio.So I was in for a pleasant shock when I downloaded Google’s Android Studio 2.2 last night along with the Android SDK and platform tools. The GUI has a revamped look and feel and the IDE is jam-packed with nifty features. You can download Android Studio and the SDK on Google’s Android Developer site. There is also a nice summary there of Android Studio’s features and all of the different platforms for developing Android apps from wearables to Android Auto. You should also make the time to view the introductory videos on the Developers' site covering Android development. Google is beginning to approach Microsoft’s level of support for its development community in terms of products, resources, and training.
Embracing Java and some housekeepingHaving been at the software development game for a while, I’ve learned to brace for the worst when it comes to getting tools and platforms installed and running. My previous episodes with installing Eclipse and the Android SDK on multiple machines always took longer than anticipated due to one snag or the other. Just to note: I’ve only installed the Android development platform on Windows machines, though it’s also available for installation on OSx and Linux.Despite my apprehension, I was pleasantly surprised how smoothly the installation went. I even lucked out that the appropriate version (8) of the Java Runtime Environment (JRE) and Java Development Kit (JDK) were installed and properly pathed on my PC after my reformat and reinstall of Win10. This typically was the snag and bottleneck during previous installations. If you don’t have the latest version of Java installed, then you can download both the JRE and JDK from Oracle’s website. You may also need to configure your path environment variables to point to where you’ve installed Java. Everything went smooth as glass and the only things remaining were installing the necessary SDK versions and configuring an Android Virtual Device (AVD) for the emulator.I’ll save you some time and grief with regard to deciding on how backwardly compatible you should make your Android apps. I used to be of the mind that in order to earn the maximum potential income from a commercial app that you needed to support every Android version from the beginning of time. This may seem like a good idea in theory, but practice is something else entirely. Google has released a ton of versions of Android over the years with several being vastly different than preceding versions. Attempting to support all of them is an exercise in futility and frustration. On top of that, older versions have a relatively short shelf life as people continuously upgrade phones which typically come with newer versions of the Android OS installed.Oh, and before I forget, you’re going to need to have a solid foundation in Java and Object Oriented Programming principles. There’s really no way around this, but the good news is there are plenty of free and paid resources to get you well on your way. The best and first place to begin is the Java Learning Trail pages on Oracle.com and though not an introductory book, “Effective Java” by Joshua Bloch is often touted as the go-to book on Java coding best practices.
Sample code FTWOne thing I noticed right out of the box with Android Studio is the helpful project creation wizard. It has a nice range of project options from opening an existing project to choosing from a selection of app templates for creating an app from a foundation. I chose to go with the option to import one of the many sample Android apps from Github. The samples are handily grouped by category and I chose one of the basic ones from the “UI” category: FloatingActionBasicButton.This particular sample app is actually somewhat complex as it also contains a logging class, but the UI is simple and there are only a few Java class files. All of the core features of an Android application are contained in this project and it’s definitely worth the time to poke around and expand the class and resource files that comprise the project. You can also view the source code editor and explore some of its features like code completion and the code exploration feature. The resource editor is a big improvement over the extremely clunky one in Eclipse. At this point, I was feeling good and was raring to build and launch my first Android app under the new Android Studio IDE!
It’s always somethingI should have not let my guard down so quickly by allowing myself to get lulled into a false sense of complacency by how smoothly the installation process went. One thing that hasn’t changed from the days of Eclipse - and it shouldn't really have come as any surprise - is the heavy resource load the Android development platform imposes on the average PC. There’s still no getting around that it’s a CPU and memory hog and my creaky Dell Inspiron desktop with a mere 6 GB of ram barely cuts it. I was used to Eclipse taking a while to load and it’s no different with Android Studio. And don’t get me started on the emulator (AVD)! You could literally go brew a pot of coffee while the emulator image loaded and it would still be loading when you got back to your desk.There were all sorts of hacks for speeding up the Android emulator with most of them consisting of configuring hardware acceleration using a hypervisor. That’s why most developers prefer to use a physical device, i.e. an Android phone or tablet. It’s certainly much faster, but sometimes an AVD is the only way to test your app across a variety of display sizes. That’s when I discovered the GenyMotion emulator which was way faster than the native Android emulator. Anyway, I was initially encouraged when I opened the Android Device Manager in Android Studio and saw it now supported hardware acceleration without messing around with external configuration. The bad news was the red error message it slapped me in the face with informing me that my lowly CPU was not capable of supporting it. Aaarrg!No worries, I thought, I’ll use my phone. My phone was the other thing that had changed since I last played around with Android development. It took some gyrations to get AS to see it. I’ll summarize the necessary steps below in the hope of saving you some time and grief:
- Connect phone to PC over USB
- Install USB device driver for your phone if not already installed. A cool feature with my new phone was the setting to load the driver from my phone like it was a CD with the driver!
- Enable USB debugging on your phone: Settings -> Developer options -> USB debugging = Enabled
- Enable Android debugging on your phone via “secret handshake”: About phone -> tap 7 times on Build number.