Running your First Android App on Emulator or Device
In the previous tutorials, we learned how to create our first Android Application Project and how to create an Android Virtual Device for testing purpose. Now we are going to Run our first android application on one of the created emulator. So if there is no AVD started yet, go to the AVD manager and start an AVD (virtual device).
Next, open your Android Application Project (that we created earlier) & let Gradle complete its build for the first time. Then click on Run menu option and look for Edit Configuration. See the Image below.
Clicking on the Edit Configuration option will open a window as shown in image below. There will be two configuration options, one for our Android Application project and other stands out for Default Configuration. Click on app inside the Android Application & we will get to see several other options to configure for our Android Application Project.
As we can see, in General section, we can specify Module(to choose which kind of application our first app is like for Phone, Tablet, TV, Wear), Installation Options(to deploy default APK or else), Launch Option(to launch specific activity of the application at start) and Deployment Target Option(whether to run on Emulator or on a physical (USB connected) device).
In the Miscellaneous option, Logcat and Installation Options are available. Logcat shows us the Logs about the various events that are triggered when our application runs on the Emulator or USB device. Using Logcat logs we can check state of our application execution and value of various objects, strings & instances used in our project. From here we can configure to launch LogCat automatically & to clear previous execution Logs when app starts running.
Similarly when installation takes place during testing, we can skip new installation if our application does not have any changes compared to previous installation. If we have made some changes and then we go for testing our application, we can always choose option to stop the running activities of our previous execution.
Other two optons are Debugger and Profiling. From Debugger, we can select type of the Debugger to test our running Android Application Project. It has several options available like Java, Native and Hybrid.
While on the other hand, Profiling will let us choose Graphics Trace Options from where we can disble precompiled shaders and programs to be traced by Graphics Tracer.
While on the other hand, the Default Configuration section contains several configurable settings of our Android Application Project. As we can see in above image, options like Android Application(same as previous one), Android Native(for Native Development), Android Test(test purpose configuration), App Engine DevAppServer(App Engine Module Configuration), Application(application level settings), Compound, Gradle(configuration for project building) and many more are available here. From these options, we can set default configurations for our android application project. These are all the available configurable settings that we can change while running/debugging our application.
Next, if you have made any change in the default settings then click on Apply otherwise select OK for default. Now click on dropdown menu option below Run menu and select app, then click on the Run option, green triangle(play button) just to its right.
It will show window with the list of connected devices with the system. From here we can select application Deployment Target and run our application on desired emulator or USB Device for testing. As we can see there is an option to Create New Emulator at the left below corner if we want one. Select desired connected device and click on OK, Android Studio will start building your project and at the end it will install a debug
apk on the selected emulator. Build process will take time so we should have patience, but at the end we will get a candy to crack as below image.
Hope this tutorial taught you something new about Run/Debug Configurations for Android App Project that we can set. Follow the procedure and test your android app project in the emulator.
What's happening behind the scenes?
Finally you have run your first HelloWorld app on your emulator, Congratulations! But here are a few things that happened when you clicked the Play button.
- All your Java source code gets compiled into
.class files by the Java compiler.
- All your
.class files gets converted into a single
.dex file by the dx tool, which is the part of Android SDK Tools.
.dex file is a Dalvik Executable file that runs inside the Android Runtime(ART) when the app is launched.
.dex file is packaged with Android Manifest and other app resources files by Android Application Packaging Tool (aapt) to yield android package file,
- Only a signed app can run on a device/emulator to ensure its authenticity, therefore the
.apk file is signed using the jarsigner utility.
- zipalign utility is used to optimize the
.apk file, and finally makes it ready for installation.