Friday, April 26, 2024

Android Studio tutorial for beginners

Share

ASFeaturedPic

Android is a very good platform to develop on. Google has spent a lot of time and energy to make things as easy as possible. Succeeding Eclipse as the main IDE, Android Studio has come along way since its introduction in 2014. Here’s how to use it.

The Basics

ASNewProj
Android Studio replaced Eclipse as the main IDE for Android development in 2014. With this change Google has revamped the way developers can take advantage of all the Android development tools.

One of these improvements is the way setting up a new project works. It has never been easier to set up a new project, just click File>New>New Project and Android Studio will step you through exactly what you need to get started. Once this step is completed, click on File>New Module to create the actual Graphical User Interface of the app. This creates a new Module with a few folders and other files. Starting with the “manifests” folder which holds the AndroidManifest.xml. The file holds basic information including the name of the app that shows up on the device and any permissions that you define. Here is a sample AndroidManifest.xml:

AndroidManifest

This is the AndroidManifest.xml that Google provides when you make a new module. The java folder hosts the java classes needed to make the app work, these can get very complex depending on what you want to accomplish. The “Hello World!” application only has 37 lines of code. Under the “res” folder you will find any images that are needed as well as icons and the layout XMLs. The “activity_main.xml” controls the content and design of the main activity.

Android Studio has a user interface that allows you to dragged and dropped different UI items into place on the activity, or if you prefer you can add the items straight in the code. Under the “values” folder there is the ability to change themes for the activity, the default is “android:Theme.Holo.Light.DarkActionBar” which provides a white background and a gray action bar.

What is Gradle?

Gradle is a build automation tool that sets out to be easier than traditional XML based project configurators and was made for large projects. One benefit is that it knows what parts of the build-tree are up to date, so those parts do not have to be re-executed. Gradle is written in Java and Groovy, which makes it relatively easy to do the basic things needed for an application. Gradle was introduced in 2007, but has only been used for Android since the release of Android Studio.  Note that each module in a project will have its own Gradle file. Gradle provides an easy way to configure app details including build version and SDK version. A Gradle file will look something like this:
Gradle
Just like the Android Manifest, this can get more complicated, especially if dealing with an Android Wear module as well as a phone module. In a Gradle file you can configure the SDK versions needed for your app, the app version and the ProGuard options. ProGuard has a lot of benefits including the ability to obfuscate and shrink the code in the application, making the overall footprint smaller, more efficient, and harder to reverse engineer. However, ProGuard is not compatible with every app. In my experience developing for Android Wear, ProGuard will not work if the watch is pulling the weather from the phone. This may just be on my end but I haven’t found a workaround to get ProGuard to work.

The Android SDK, AVD Manager and ADM

SDK Manager
The Android SDK includes all the necessary libraries and files for Android Developers to get started. What is nice about Android Studio is that the SDK is built right in and is easy to access by just clicking a button on the top toolbar. The items beside the SDK Manager icon include the Android Virtual Device Manager and Android Device Monitor. The AVD Manager allows you to set up Android virtual devices to test apps on. You can configure just about anything from the device size to the instruction set architecture. If you select an Intel x86_64 instruction set architecture you can run the AVD in something known as “fast virt mode”, this uses Intel’s Hardware Accelerated Execution Manager (HAXM) which allows for a very smooth experience when running an AVD. The Android Device Monitor allows the user to monitor everything that is happening on the device at any given time, it acts like a turbocharged LogCat with a graphical user interface essentially, this makes it very easy to debug apps and see what outside sources may be conflicting.

How to run an application in Android Studio

With the tools mentioned above it is extremely easy to run and manage applications. To run an application just click the green arrow in the taskbar, this will run Gradle to make sure there are no errors then pull up the device menu. From here you can select what device you want the app to run on, whether it be an AVD or a physical device. Projects with one module will default to that module, but a module will have to be chosen when there are more than one. An extra dialog box will pop up and let the user choose a module they want to run. When the app is running you can check the LogCat or ADM to make sure everything is running right.

How to compile and export a signed APK

Once the application performs like it should the next step is to export it. This is a fairly simple process. Click Build>Generate Signed APK… and a box will pop up, select the correct module then create a new keystore. A keystore is a binary certificate that is needed when making a release build. It is recommended to work with debug versions of the app until the app is ready for release. The option to change to a release build is in the project’s manifest by adding and setting “android:debuggable” to “false” or by just selected “release” in the dialog box when generating the signed APK. Once finish is clicked Android Studio will generate a signed APK file to the destination specified, this APK can be used like any normal APK by just installing it on a device or can be used to put an application on the Google Play Store. The APK must be signed and must have been generated as a release build to upload to the Play Store. Note: If the application is an Android Wear app, the phone module will need to be exported, not the watch module, with a few lines added to the Gradle file the wear module will be included in the application with the mobile module.

Wrap-up

Android Studio makes life a lot easier when developing for Android. With tools like the Android Virtual Device Manager and Android Device Monitor it has never been easier to make sure the application runs just right in any scenario. Gradle makes it easy to configure the application details without being overly complicated as well as making sure there are no errors in the code. Android development has never been more easy and fun, with just a few steps and a little work anything is possible. Here is the link to grab Android Studio.

Read more

More News