Creating your first app called Hello Android Studio
After downloading Android Studio, install it and go through the setup wizards. The wizard checks for some requirements, whether the Java Development Kit (JDK) is available, and other important elements that the installation wizards guide you through.
Once the installation is complete, it is time to develop your first Android app using Android Studio, just to check whether everything has been installed correctly and works the way it should. It probably will be no surprise that this is where the Hello Android Studio recipe comes in.
Getting ready
To go through this recipe, you will need a running Android Studio IDE, an Android Software Development Kit (SDK), and an Android device. No other prerequisites are required.
How to do it...
Let's create our first Android app using Android Studio to check whether everything works fine with the help of the following steps:
- Start Android Studio. The Welcome to Android Studio dialog will be shown to you after a few seconds.
- Select the Start a new Android Studio project option. Then, the Configure your new project dialog appears.
- For Application name, enter
HelloAndroidStudio
; and for the Company domain field, enterpacktpub.com
(or use the domain name of your own company if you prefer to do so). - Package names such as
packtpub.com
andhelloandroidstudio
are suggested and updated while you type. If you wish, you can edit the Project location before you click on the Next button. - In the Target Android Devices dialog box, check the Phone and Tablet option. Leave the other options unchecked. We will create some of those other interesting targets, such as an Android Wear app, later. For the Minimum SDK, choose API 14. If that one is not (yet) available, click on any of the other available SDKs. We will install more SDKs later. Click on the Next button to continue.
- In the next dialog box, Add an activity to Mobile, choose the Blank Activity option and click on the Next button.
- The final dialog Customize the activity will be displayed after this. Leave all the values the way they are and click on the Finish button.
- Android Studio is now going to create this new app for you. After a while, the project view, a MainActivity class, and an activity_main.xml layout are displayed. If you change the perspective of the project view on the left-hand side of your Android Studio by clicking on the button, that displays the little green Android guy and the text that reads Android, from Android to Project, the layout will look a little bit more like you are used to, that is, if you have used Eclipse before.
- Double-click on the app folder to expand it. You will notice a file called the
build.gradle
file (note that this file also exists on the root level). - Double-click on the
build.gradle
file to open it and have a look at the values forcompileSdkVersion
,minSdkVersion
, andtargetSdkVersion
. By default, thecompileSdkVersion
value is always related to the latest (available) SDK. The value forminSdkVersion
is the one that you have chosen in the Target Android devices dialog box.Note
If you want, use a different SDK to compile against; you must change the value for
compileSdkVersion
. The version you choose might need to be installed first. If you are happy with the current configuration, go to step 14 right away. - If you want to check which SDKs are installed, go the Tools option from the main menu and choose Android from the SDK Manager submenu.
- The Android SDK Manager dialog box displays which SDKs are installed. If you need to install a different SDK, you can check the elements you need and click on the Install n packages… button.
- After installing the SDKs that you need and having configured your
build.gradle
file, you can now run your app. - If you are using a physical device for Android development, you need to unlock the developer options first. On your device, start the Settings app and go to the Device info option. (This option may be on the General tab or section or at another place, depending on the version and flavor of Android that your device is running on).
Note
If you do not have a real device, I strongly recommend you get one as soon as possible. To get started, you can use an emulator for now. You can use the emulator that comes with the Android SDK or you can read the recipe about Genymotion first to find out how to use emulated devices.
- In the Device Info view, scroll all the way down until you see the Build number option. Now, click seven (7) times on Build number to unlock (enable) the developer mode. (No, this is not a joke). You now have the developer's menu unlocked.
Note
On older Android versions (below 4.2), this step may be skipped, or if the developer options are already available as a menu item in the settings app, this step may be skipped.
- Now that you have got a new option in your Settings app, called Developer options, click on it and have a look at it. It is important that you enable the USB debugging option within this menu. In addition, you might want to enable or disable some of the other options.
- Connect your device and run your app from Android Studio by clicking on the green triangle next to the drop-down box that reads the app. Or, choose the Run... option from the Run menu. Then, the Choose Device dialog box appears. Your device should now appear in the list of the Choose a running device option. ( If your device does not appear in the list, reconnect your device).
- Click on the OK button. (For Windows, before you are able to connect your device, it is often necessary to install a driver first.)
- On your device, a dialog box may pop up, requiring you to accept the finger print. Choose Allow in order to continue.
The app is now being deployed on your device. If everything goes well, your new app is now shown on your device that says Hello world! Hurrah! I admit this is not really a very exciting app, but at least we know now that Android Studio and your device have been configured correctly.
How it works...
Android Studio will take care of the basic parts of your app setup. All you need to do is choose the target and minimal SDK for your app. Using the API level 14 (Android 4.0) is currently the best option, as this will allow your app to run on most Android devices.
The app will be compiled against the chosen (compile) SDK by Android Studio.
The app will be copied to your device. Having the USB debugging option enabled will help you troubleshoot any issues, as we will find out later.