
Android Programming with Kotlin for Beginners
By :

Before we explore any of the code and learn our first bit of Kotlin, you might be surprised to learn that we can already run our project. It will be a fairly featureless screen, but as we will be running the app as often as possible to check our progress, let's see how to do that now. You have three options:
The first option (debug mode) is the easiest to set up, because we did it as part of setting up Android Studio. If you have a powerful PC, you will hardly see the difference between the emulator and a real device. However, screen touches are emulated by mouse clicks, and proper testing of the user experience is not possible in some of the later apps, such as the drawing app. Furthermore, you might just prefer to test out your creations on a real device – I know I do.
The second option, using a real device, has a couple of additional steps, but, once set up, is as good as option one, and the screen touches are for real.
The final option takes about five minutes (at least) to prepare, and then you need to manually put the created package onto a real device and install it (every time you make a change to the code).
The best way is probably to use the emulator to quickly test and debug minor increments in your code, and then use the USB debugging mode on a real device fairly regularly to make sure things are still as expected. Only occasionally will you want to export an actual deployable package.
If you have an especially slow PC or a particularly aging Android device, you will be fine just running the projects in this book using just one option or the other. Note that a slow Android phone will probably be OK and cope, but a very slow PC will probably not handle the emulator running some of the later apps, and you will benefit from running them on your phone/tablet.
For these reasons, I will now go through how to run the app using the emulator and USB debugging on a real device.
Follow these simple steps to run the app on the default Android emulator:
You're done. Here is what the app looks like so far in the emulator. Remember that you might (probably do) have a different emulator, which is fine:
Clearly, we have more work to do before we move to Silicon Valley and look for financial backing, but it is a good start.
We need to test and debug our apps often throughout development to check for any errors, crashes, or anything else unintended.
We will see how we get errors and other feedback for debugging from our apps in the next chapter.
It is also important to make sure it looks good and runs correctly on every device type/size that you want to target. Obviously, we do not own one of each of the many thousands of Android devices. This is where emulators come in.
Emulators, however, are sometimes a bit slow and cumbersome, although they have improved a lot recently. If we want to get a genuine feel for the experience our user will get, then you can't beat deploying to a real device. So, we will want to use both real devices and emulators while developing our apps.
If you are planning on using the emulator again soon, leave it running to avoid having to wait for it to start again.
If you want to try out your app on a tablet, you're going to need a different emulator.
Creating a new emulator
It is simple to create an emulator for a different Android device. From the main menu, select Tools | AVD Manager. In the AVD Manager window, left-click Create New Virtual Device. Now left-click on the type of device you want to create – TV, Phone, Wear OS, or Tablet. Now simply left-click Next and follow the instructions to create your new AVD. Next time you run your app, the new AVD will appear as an option to run the app on.
Now we can look at how we can get our app onto a real device.
The first thing to do is to visit your device manufacturer's website and obtain and install any drivers that are needed for your device and operating system.
Most newer devices won't need a driver, so you may want to just try the following steps first.
The next few steps will set up the Android device for debugging. Note that different manufacturers structure the menu options slightly differently to others. But the following sequence is probably very close, if not exact, for enabling debugging on most devices:
Some manufacturers have different, obscure methods for achieving this step. If this step doesn't work, do a web search for your device and "unlocking developer options."
We are now ready to learn some Kotlin and add our own Kotlin code to the Hello World project.