For the More Curious: The Android Build Process

By now, you probably have some burning questions about how the Android build process works. You have already seen that Android Studio builds your project automatically as you modify it, rather than on command. During the build process, the Android tools take your resources, code, and AndroidManifest.xml file (which contains metadata about the application) and turn them into an .apk file. This file is then signed with a debug key, which allows it to run on the emulator. (To distribute your .apk to the masses, you have to sign it with a release key. There is more information about this process in the Android developer documentation at developer.android.com/​tools/​publishing/​preparing.html.)

How do the contents of activity_main.xml turn into View objects in an application? As part of the build process, aapt2 (the Android Asset Packaging Tool) compiles layout file resources into a more compact format. These compiled resources are packaged into the .apk file. Then, when setContentView(…) is called in MainActivity’s onCreate(Bundle?) function, MainActivity uses the LayoutInflater class to instantiate each of the View objects as defined in the layout file (Figure 1.22).

Figure 1.22  Inflating activity_main.xml

Inflating activity_main.xml

(You can also create your view classes programmatically in the activity instead of defining them in XML. But there are benefits to separating your presentation from the logic of the application. The main one is taking advantage of configuration changes built into the SDK, which you will learn more about in Chapter 3.)

You will learn more details of how the different XML attributes work and how views display themselves on the screen in Chapter 10.

Android build tools

All of the builds you have seen so far have been executed from within Android Studio. This build is integrated into the IDE – it invokes standard Android build tools like aapt2, but the build process itself is managed by Android Studio.

You may, for your own reasons, want to perform builds from outside of Android Studio. The easiest way to do this is to use a command-line build tool. The Android build system uses a tool called Gradle.

(You will know if this section applies to you. If it does not, feel free to read along but do not be concerned if you are not sure why you might want to do this or if the commands below do not seem to work. Coverage of the ins and outs of using the command line is beyond the scope of this book.)

To use Gradle from the command line, navigate to your project’s directory and run the following command:

    $ ./gradlew tasks

On Windows, your command will look a little different:

    > gradlew.bat tasks

This will show you a list of available tasks you can execute. The one you want is called installDebug. Make it so with a command like this:

    $ ./gradlew installDebug

Or, on Windows:

    > gradlew.bat installDebug

This will install your app on whatever device is connected. However, it will not run the app. For that, you will need to pull up the launcher and launch the app by hand.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.234.139.149