Build Your First Android App (With No Step Skipped)
Have you ever wanted to start learning how to build android applications?. Cool, Simply sit tight and I will take you through all the steps. (With no step skipped.)
# Create an Android Project
- In the Welcome to Android Studio window, click Start a new Android
- Otherwise, If Android Studio is already open or if you already have an open project, Click File > New > New Project...
- In the Choose your project window, select Empty Activity and click Next.
- In the Configure your project window:
- Type a name for your app. i.e Name: My Android App
- Type a package name for your app. It's typically your website domain name in reverse order plus the app name in lowercase. i.e Package name: com.stevenmwesigwa.myandroidapp
- Choose where you want to save your app if you want to save it in a different directory. i.e Save location: F:\ANDROID\learn
- Choose the programming language you want to use to write this app. In my case, I'll go with Java from the dropdown menu i.e Language: Java
- Choose the minimum android version you want your app to support. i.e Minimum API level: API 16: Android 4.1 (Jelly Bean)
- Click the Finish button.
- It may take some time as Android Studio prepares your app for the first time. Once it's done, you should see your editor appear like the one below.
- Open the Project window (View > Tool Windows > Project). Or use a keyboard
shortcut (ALT + 1)
- In the Project window, we have a couple of important files we will put more focus on namely :-
- app > java > com.stevenmwesigwa.myandroidapp
- This file acts as the main entry point for our app.
- app > res > layout >
- This is where the layout of the activity's user interface is defined.
- app > manifest >
- This is where we define each component as where as define the main attributes about our app.
- Gradle Scripts > build.gradle
- As you may well notice, we have 2 build.gradle files. One for "app
Project" and another for "app Module".
- We'll use these files to configure 'build settings' to provide custom packaging options.
- As you may well notice, we have 2 build.gradle files. One for "app Project" and another for "app Module".
- Let's now run our app. Here we have a couple of options namely:
# Running The App On An Actual Device (SECTION A).
- Connect your smart phone to your laptop or Desktop that you're currently working with using a USB cable. Make sure that your computer can recognize your phone. i.e That it is visible among the connected devices. If you're using Windows as your main operating system and your computer can't recognize your phone, you may need to install the appropriate USB driver for your smart phone.
# Enable USB Debugging on your smart phone.
- Depending on your phone's android version, how to achieve this may vary. These steps have been tested on "Android v7.0" or higher.
- Open Settings app.
- If your device uses Android v8.0 or higher, select System. Otherwise, proceed to the next step.
- Scroll down until you reach and select About phone.
- Scroll until you reach and select Build number 7 times
- A tool tip may appear for a few seconds stating that "You are now a developer"
- To confirm, Tapping Build Number again may show a tooltip stating that "No need, you are already a developer".
- Tap your smart phone's "back" button once(previous screen), then scroll up or down and select Developer options.
- In the Developer options window,Turn On the top most button slider to allow "development settings".
- Scroll down or up till you find USB debugging and turn On the bottom slider to allow "USB debugging".
# Running The App On Your Device(Smart phone) (SECTION B)
- Once you've set your phone in debugging mode as explained above and your physical smart phone is still connected to your Computer / Laptop, make sure that the Notification window states that "Android system. USB debugging connected."
- Now we will edit the run/debug configuration settings to make sure that when we click the Run button in Android Studio, our app will run on our smart phone.
- In Android Studio's tool bar, click Run > Edit Configurations...
- In the Run/Debug Configurations window, under Deployment Target Options, select "Open Select Deployment Target Dialog" .Then click the Ok button. This will allow us to have a window automatically opened up for us when we click the Run button in the tool bar for more Run options.
- Now click the Run button in the tool bar. Then click Run 'app' to open up a Select Deployment Target dialog box where we can click the connected smart phone to run our app.
- Now, as you can see from the image above, my smart phone was detected, but had a label of UNAUTHORIZED. For some reason it wasn't able to know that my phone was in debugging mode. I had to turn debugging Off and On from Settings app > Developer options > USB debugging 'Debug mode when USB is connected' . After that, my smart phone prompted me with an Allow USB debugging dialog box with the computer's RSA key fingerprint Tap the box to 'Always allow from this computer'. Then click the Ok button to accept.
- After that, your smart phone should be successfully detected by Android Studio as shown in the diagram below. i.e TECNO TECHNO WX3P (Android 7.0, API 24)
- Click Ok. Android Studio may prompt you with an Instant Run dialog box. Instant Run allows you to push code changes without building a new Android Package (APK). But for our use case, we will keep things simple by clicking Proceed without Instant Run button.
- After that Android Studio will build an Android package for your smart phone and run it. The building process may take some time depending on your machine.(It took 2 minutes for my case).
- Yes!. This is it. We built our first app that displays "Hello World". Hooray!!, If all went well, you should be able to see your first Android app launching on your smart phone as shown in the diagram below.
- If you made it to this point, am proud of you.
# Running The App On An Emulator
- I assume you have an Android Emulator installed on your machine.
- . The emulator simply simulates (mimics) actual Android devices on your computer so that you can test your application on a variety of devices and Android API levels without needing to have multiple physical devices.
# Let's open android studio and try creating an Android Virtual Device (AVD).
- On the toolbar, Click Tools>AVD Manager.
- When the AVD Manager window opens up, Click the Create Virtual Device... button.
- In the Virtual Device Configuration window on step Select Hardware , Select Phone as your category, then choose the virtual phone you plan on creating. In my case, I went for Pixel 2. Leave other settings as they are for now and click the Next button.
- In the Virtual Device Configuration window on step System Image , Select a system image of your preference. In my case, I went for the latest at the time of writing this article which is Q as the release name with API Level 29. If the system image wasn't download prior to this step, you will get a warning A system image must be selected to continue. To resolve this, click the Download link next to your preferred system image to download it.
- From the step above, the SDK Quickfix Installation window will open up to show you the System Image download progress. Wait until the download is complete. WARNING: Be prepared to provide enough disk space for this system image. In my case, the system image was 1.1 GB.
- Once installation is complete a screen displaying a successful installation process should show up. Click the Finish button.
- As you would notice, we no longer have a Download link next to our chosen System Image. Click Next button to proceed to the next step.
- In the Verify Configuration Window, Type in a name you would love displayed in our newly created system image. In my case, I labelled it "Pixel 2 API 29 Android 10".
- Finally, If all went well, an Android Device Manager window will pop up displaying a list of currently installed virtual devices.
# Run Your App On An Emulator.
Let's click the target device drop-down menu and choose which virtual device we would love to run our app on. In my case, I selected my newly created virtual device.( Pixel 2 API 29 Android 10 )
- After selecting a virtual device to run on, Click the green Run button in the toolbar to run your app. Or you may go to the toolbar > Click Run > then Run 'app' which is equivalent to pressing SHIFT + F10 on a Windows machine.
- Android Studio will now install our app on our selected virtual device and will start the Emulator. After the install process is done, you should be able to see your app display "Hello World".
- That's it. You've now been able to build your first Android app. This is your first step to building more complex applications.
- Subscribe to my newsletter so that you don't miss out on any upcoming articles.
I hope you learn a thing or two from this blog. Thanks for checking it out.
If anything is unclear or you wish to make any corrections, don't hesitate to leave a comment. Your feedback is greatly appreciated.
Sharing is caring. Share this blog to help out others getting started with building Android applications.
- How To Use @Getter And @Setter Annotations With Project Lombok
- How To Use @Data Annotation With Project Lombok In Java Applications
- How To Use @Value Annotation With Project Lombok In Java Applications
- How To Use @EqualsAndHashCode Annotation With Project Lombok
- How To Use @FieldDefaults Annotation With Project Lombok In Java Applications
- How To Install Project Lombok In Apache Netbeans
- How To Use @NonNull Annotation With Project Lombok In Java Applications
- Introduction To Java Streams And Functional Programming
- Build Your First Android App (With No Step Skipped)
- How To Use @ToString Annotation With Project Lombok
- Writing Your First Lambda Expression For Beginners In Java