Chapter 7
Multi-Module Projects

WHAT'S IN THIS CHAPTER?

  • Adding modules
  • Phone & Tablet module
  • Android Library module
  • Android Wear module
  • Android TV module
  • Glass module
  • Android Auto module
  • Google Cloud module
  • Importing modules
  • Removing modules

Previous chapters dealt with general concepts of application development in Android Studio; this chapter covers the capabilities of Android Studio to work on multiple modules in your Android project.

In addition to the core module you created, you will eventually need additional modules when you want to add support for other Android devices. For example, you might want to add Wear, TV, or Auto modules in your Android Studio project, or even third-party Android libraries.

This chapter explores the details of adding Phone & Tablet modules, Android and Java libraries, and Wear, TV, Glass, and Google Cloud modules. Then the chapter covers importing Gradle and Eclipse projects. AAR and JAR packages are covered as well to help you understand how to include them in your Android Studio projects.

Android Studio works on a module-based project structure, which means that it can handle multiple modules in one project. Having multiple modules in an Android Studio project enables you to work on one project instead of multiple projects so your development team can create a more organized application.

Prior to Android Studio, Eclipse handled multi-module projects with a workspace concept. Eclipse projects do not need to reside in the same project folder to be included in the same project setup. However, this approach relied on Eclipse project dependencies for the build process.

A better organized approach uses Maven. Although Maven is a de facto standard in build and dependency management in Java projects, it was not always supported in Android projects. The introduction of Gradle as the default build system for Android solved this huge problem in standardization of dependency management.

ADDING MODULES TO ANDROID PROJECT

In Chapter 3, you learned how to create a new project with multiple modules. In this section, you learn how to add new modules to an existing Android Project.

We created a new project named ChapterSeven to work on modules. You can recreate that project, create a new project, or work on a project you previously created.

When your project is ready, find the actions from the Android Studio menu to add a new module. Either right-click the project pane or open the File menu and select New ⇨ Module to start adding a new module to your project.

After clicking the Module option, the wizard shown in Figure 7.1 opens. Select the type of module to add to your project.

New Module selection window.

Figure 7.1 New Module selection

By default, you will see ten module actions in the window; you can select to either add a new module or import an existing module to your project.

After selecting the module, Gradle files will be auto-generated to handle a multiple module build and release process. The following sections cover the auto-generated files, folders, new Java packages, and Gradle, manifest, and resource files in more detail for each module type.

PHONE & TABLET MODULE

Most of the time, the Phone & Tablet module (also called the Android module) is the core module in a project. While it might not be common to add multiple Android modules together, in certain cases you may require a second or third Android module in addition to your core module to generate multiple APKs with different properties.

For example, you can develop an Android application that supports multiple screen sizes with configuration of dimensions and a good design of XML resources, bitmap resources with multiple resolutions, and support libraries. However, in that case, the APK files get a lot larger and create significant cost for the user to download and install. To avoid that, you can create multiple modules to generate individual APKs for specified devices.

Alternatively, when you develop an application with trial, demo, enterprise, consumer, or paid versions you would need modules with different APKs to distribute in the Google Play Store: one module with limited access and another with unlimited access to all resources and activities you have developed.

You can also develop a tutorial application, managing the new lightweight application within the project and generating binaries and APKs with a single build system.

There are other situations where you may want to manage multiple Phone & Tablet modules in the same project, such as when starting the project with the Wear module and adding a new Phone & Tablet module and so on.

Managing multiple modules in Android Studio gives you the opportunity to configure Gradle for handling dependencies between modules, use a shared module's resources or libraries, and generate multiple APKs at the same time in the same project directory. Using Gradle eases the development process for multiple teams working on a complex application, and helps the transition to continuous integration.

If you select the Phone & Tablet module (refer to Figure 7.1), you need to configure module name and activity type as you would when creating a new Phone & Tablet project. Figure 7.2 shows that we added a new Phone & Tablet module named chapterseventrial.

Screenshot showing how to Configure a new Phone & Tablet module.

Figure 7.2 Configuring a new Phone & Tablet module

After naming the module, you follow steps that are similar to those for creating a new Android project. After naming and configuration, the module's files and folders are generated and the chapterseventrial module appears in the Android Project view as a separate module, as shown in Figure 7.3.

Screenshot of Phone & Tablet module in Project view.

Figure 7.3 Phone & Tablet module in Project view

As shown in Figure 7.3, the new module has its own folder, resources, Java source code, and manifest and Gradle files for configurations. The Build Variants window at the bottom shows the build type of the modules to configure and generate multiple APKs.

Build configurations can be made in Gradle or from the Project Structure window (Command+semicolon [;] on Mac, Ctrl+Alt+Shift+S on Windows). From the Project Structure ­window, you can configure the module for versions, APK signing properties, build types, and flavors and their dependency to each other.

You can initiate a project build by selecting Make Project from the Build menu, or you can just build the module by selecting Make Module <modulename>.

When you select Generate APKs from the Build menu, APKs will be generated under module's build/outputs/apk folder. You can navigate to the directory from the folders where you store the Android Studio project such as /Users/username/AndroidStudioProjects/ChapterSeven/chapterseventrial/build/outputs/apk on Mac OS X or C:UsersusernameProjectsAndroidStudioProjectsChapterSevenchapterseventrialuildoutputsapk on Windows.

ANDROID LIBRARIES

The Android Library module contains the shareable Android source code and resources that can be referenced by other modules in an Android project.

The Android Library module is useful when you want to reuse and share a code base or XML resources within your project modules. This can be either a private library developed by yourself, or a third-party library imported into your project.

To add and develop your own Android Library module, select Android Library from the list shown in Figure 7.1. When you proceed to configuration, the module wizard asks only the library's name.

When you finish adding the library, you can see its package, folders, and Gradle file under the project folder, as shown in Figure 7.4.

Screenshot of Android Library module.

Figure 7.4 Android Library module

Android libraries are useful when you are developing multiple application projects or multiple APKs and want to use a shared resource to lower implementation time and preserve continuity between modules and projects.

Working with Android Libraries

An Android library is not a runnable application on its own; it doesn't generate an installable APK but generates a shareable AAR package. An Android library acts as a shareable resource that is loaded and fetched by the apps using the library.

Adding an Android library is not enough to enable you to start using it in other modules. Before using an Android library in your application module, you should add it from Gradle or from the module settings window as a dependency to the corresponding module.

Dependencies can be configured through an interface from the Project Structure window, which you can open from the File menu. Alternatively, you can access the Project Structure window by right-clicking the module in Project view and selecting Open Module Settings.

Select the module that requires an Android library, and then navigate to the Dependencies tab, as shown in Figure 7.5.

Screenshot of Module dependencies.

Figure 7.5 Module dependencies

Next, click the plus button at the bottom to add the module dependency (see Figure 7.6). When you finish adding the module dependency, the expertlibrary module will appear in the list of dependencies.

Screenshot showing how to add a dependency.

Figure 7.6 Add a dependency

You can also add a dependency directly from a module's build.gradle file. To do that, add the compile project(':androidlibrary') line to the dependency area and sync the project to make your module load the library.

We added the expertlibrary module together with auto-generated dependencies to our project's Gradle file dependencies section, as shown in Listing 7.1.

Now you are ready to use shared resources and Java classes in your module. When you start typing the class name in the editor, autocomplete will list matching item(s). Similarly, when you assign a resource to your layout view, you will see the shared resource in the list of resources when typing (or adding from the list).

Using Android Library Java Packages

To start using Java classes from an Android library, add a new Java file to the library package under the src folder. You can add as many Java classes as you need to create a shareable library and speed the development process.

When a dependency is set and a Java class is added to your Android Library module, you can use it in the corresponding module. When you are in the app module, type the name of the class you want to add and it will be ready for use.

Using Android Library XML Resources

Sharing resources between modules is really practical when you need to use your own defined color values in both your phone and wear apps. Using resources makes the development of user interface elements easier because you don't have to rewrite the same definition repeatedly for each module.

When you're finished with dependency configurations, add a new XML resource to the library. We added colors.xml, which was not present when the project was created. This XML file defines the colors’ names and RGB values to use in all other modules.

Finally, go to your module's layout file to assign the color resource you want to use. If Gradle sync worked, you will see the resource in the list or in the XML editor. It will be listed right after you start typing.

Generating an AAR Package from an Android Library Module

As mentioned earlier in this section, an Android library is not an application but is loaded into the dependent modules from the provided binary dex file and is stored in the resources folders.

You may want to share your library with other people or make it open source and distribute it. The easier way to distribute your Android library is to share the output AAR file. The AAR file is similar to an APK file, which is a compressed file that includes the necessary content of an Android Library module for easy export and import. Having a standard file extension such as .aar helps developers recognize that the library is for Android applications, not for Java.

When you select the Build APK option from the Build menu, an AAR file is generated and placed in the module's build/outputs/aar directory. When you build our Android Library module, you will see the expertlibrary-release.aar or expertlibrary-debug.aar file in the output directory, as determined by the Build Variant configuration.

JAVA LIBRARIES

The Java Library module includes Java packages to link with your modules and Java classes to reuse as needed in your projects. Depending on the specific requirements of your project, an Android Library module can also be used for this, but as stated in the following note, that might not be the best solution.

Add a new Java library to your project by selecting New Module and then select Java Library from the list of modules (refer to Figure 7.1). The Create New Module wizard shown in Figure 7.7 will open.

For this configuration, name the library, which becomes the name of your library and module. Provide a name for the initial Java class you want to create in the library. You can edit the name of the package by clicking on the Edit link to the right of the Java package name line. The default name for the package is com.example, as shown in Figure 7.7.

Java Library module configuration screen.

Figure 7.7 Java Library module configuration

As shown in Figure 7.8, the new Java Library module is shown in Project view after you click Finish and when Gradle sync completes.

Screenshot of Java Library module in Project view.

Figure 7.8 Java Library module in Project view

Like an Android library, a Java library must be added as a module dependency for the corresponding module. In order to reference a library in the module, you can edit the module's build.gradle file, as shown in Listing 7.1. You can also add the library from the Project Structure window by selecting the module and selecting the Dependencies tab as we did in the “Android Libraries” section (refer to Figure 7.6).

When you are done with dependency configuration, you are ready to use the Java classes in the module that the Java Library referenced. Java class names are autocompleted when you start typing them in the referenced module's Java source file.

ANDROID WEAR MODULE

Wearable technologies are getting better day-by-day and Android is the frontier for wearables, with multiple devices on the market running Android Wear. As a result, there is a large installable base online for a wearable Android application developed with the Android Wear API.

In order to develop a wearable project, you can either start a standalone Android Wear project with Android Studio or add a new module to your existing Android project.

Having a Wear module in your project makes sense when you want to distribute your application's extended features, such as designing a basic user interface with touch or voice recognition for a Wear application that interacts with your main Android phone or tablet application.

After you select the Android Wear module from the Create New Module window shown in Figure 7.1, the configuration window asks for the name of the module and the initial wear activity and its name.

When you finish adding the Android Wear module, it appears as a separate module in the application project window.

During UI development, keep in mind that Wear projects use totally new form factors. However, you can share the color, theme, font, and text resources you generated in your Android library project between your Android Phone & Tablet project and Wear project.

After you add the Wear module with an Empty Activity, you will see that three layout files have been created: two for multiple form factors on wearable devices (round and rectangle), and one for the main activity (see Figure 7.9). The round and rectangle layouts should be designed according to their shape.

Wear module preview screen.

Figure 7.9 Wear module preview

As shown in Figure 7.9, the module has its own resources, activity class, and Android manifest file. Although further enhancements and development are similar to Android application development, you should consider during the design and developing that you will deploy this application to far less powerful and smaller devices than a smartphone.

Also consider that your application will be running on a very small screen. That means that multi touch is hard to accomplish, and there is space for only a little information using a small font that might be hard to read.

Google provides plenty of information about Wear. Visit the following URL for Android Wear application guidelines and training: http://developer.android.com/wear/index.html.

Running and Debugging an Android Wear Module

Android Wear devices are not as common as smartphones, but they will be eventually, and they will be cheap and easy to afford. For now, you can take advantage of virtual devices to run and debug Android Wear modules.

Creating an Android Wear virtual device is similar to creating a phone or tablet device. (See Chapter 2 if you need a refresher on creating a new virtual device.) Instead of selecting Phone, as in Chapter 2, here you select Wear from the virtual device category as shown in Figure 7.10.

Wear virtual device selection screen.

Figure 7.10 Wear virtual device selection

After selecting the hardware profile, select the Android version to run on the Android Wear virtual device. We selected Android 6.0, Marshmallow, for this example.

Finally, select the device configuration to finish creating a Wear virtual device, as shown in Figure 7.11.

Wear virtual device configuration screen.

Figure 7.11 Wear virtual device configuration

Now, you can run the selected module: Go to the Run menu and select the Run action instead of Run <name of default module>, or press Control+Option+R on Mac, Alt+Shift+F10 for Windows. This action will list the runnable modules, as shown in Figure 7.12.

Screenshot of List of runnable modules.

Figure 7.12 List of runnable modules

Launching the Wear module on a square Wear device will install the generated APK of the Wear module and show the Wear app, as in Figure 7.13.

Wear app in Android Wear AVD window.

Figure 7.13 Wear app in Android Wear AVD

To debug your app, select the Debug action or press Control+Option+D on Mac, Alt+Shift+F9 for Windows to show the debug windows.

Building APKs with Android Wear Support

After adding a Wear module, you need to make sure the build process handles the APK generation correctly.

Chapter 4 covered the steps to generate a signed APK. Follow those steps to assign a signature to a Wear module. Android Studio handles all modules separately. Select the Build APK option or the Generate Signed APK option from the Build menu to generate the APKs. Android Studio will open a notification window when it is finished, as shown in Figure 7.14. Follow the link at the bottom to open path in the file browser.

Screenshot of APK generation completed notification.

Figure 7.14 APK generation completed notification

When the build process is finished and you navigate to the project folder, you will see the APK under the module's build/outputs/apk directory.

ANDROID TV MODULE

Although Android TV might seem new in the market, it has a long history. It was first announced as Google TV at Google I/O 2010. The first available devices were from Logitech and Sony. The first generation of Google TV devices were all designed on Intel's x86 platform, although the second generation of Google TV ran on ARM devices.

Another effort to bring Android to a big screen was Nexus Q, announced at Google I/O 2012. Nexus Q was a high-quality device with an integrated amplifier, NFC support, and nice design. However, the price tag was three times more than similar devices like Apple TV so it never really got far from being a prototype device.

Two years later, at Google I/O 2014, a new Android-based device was announced—Android TV. The first model, labeled ADT-1, was a developer-only device. A few months later, an Intel-based Nexus Player was announced, followed by several TV manufacturers offering Android TV capabilities integrated in their TV sets.

Android TV is creating its market presence, and developers are becoming more focused on the Android TV platform, extending their applications for TV use. These applications can be game or media players offering a “leanback” user experience.

Adding a new Android TV module is similar to adding a Wear module. You should first name the module and then select an activity. Android TV has only one template activity to select when you configure the module. It is not a simple template to manage at the beginning for a simple application but it is a stable template and can run on an Android TV device immediately.

The activity customization window asks for many additional configurations to add multiple fragments and activities, as shown in Figure 7.15.

Android TV module activity configuration window.

Figure 7.15 Android TV module activity configuration

When you click Finish without changing the activity and fragment names, the Project view will show many Java classes and resources generated under the res/drawable folder, as shown in Figure 7.16. We named our TV module MirrorApp, a common use case for TV applications to mirror the current content of a smartphone or desktop application to a TV screen.

Android TV module view in the project window.

Figure 7.16 Android TV module view in the project window

The Android TV activity template is focused on media playback and adds Java classes and resources to manage video playback seamlessly in the application.

The Android TV manifest is a little different than other applications because of its input method. Because TVs don't have a touch input, the launcher Intent uses the LEANBACK_LAUNCHER category flag to be accessible from the Android TV launcher and signify to the Google Play Store that it's compatible with TVs.

The auto-generated Android TV module manifest file is shown in Listing 7.2. It disables the touch screen input requirement and requires the leanback feature for the application, making this module a TV-only application.

Running and Debugging Android TV Modules

If you already have an Android TV device such as the Nexus Player, you can use it to run and debug your applications; otherwise, you will need an Android TV emulator to run and debug your TV module.

Let's create a TV emulator by opening AVD Manager and clicking New Virtual Device to select TV from the left panel. Then select the resolution (1920 x 1080) and density (xhdpi) of the TV emulator, as shown in Figure 7.17.

Screenshot showing how to select TV resolution and density.

Figure 7.17 Select TV resolution and density

The next window lists existing Android TV images with the corresponding Android API version. Figure 7.18 shows that Android 6.0, Marshmallow, has been chosen as the TV emulator.

Screenshot showing Android TV image selection.

Figure 7.18 Android TV image selection

Right after you select the Android API version, click Next, review the virtual device on the next screen, click the Finish button, and you are done. Then you are ready to launch sample module mirrorapp on the virtual Android TV.

To launch the TV module, follow the same steps as you have for other modules: Select Run from the Run menu or press Control+Option+R on a Mac, Alt+Shift+F10 on a Windows to select your TV module from the popup dialog box shown in Figure 7.19.

Screenshot of TV module selection.

Figure 7.19 TV module selection

Next, select the recently created virtual device or your Nexus Player to launch the TV module. When the virtual device launches, the custom Android TV template application appears on the TV emulator, as shown in Figure 7.20. We didn't make any changes on the Android TV template activity so it will only show the auto-generated text and layout.

Screenshot of Android TV module debug selection.

Figure 7.20 Running Android TV application on AVD

To display the Debug menu shown in Figure 7.21, click Debug from the Run menu, or press Control+Option+D on a Mac or Alt+Shift+F9 on Windows.

Running Android TV application on AVD window.

Figure 7.21 Android TV module debug selection

While developing a new TV application, you can follow the standards for Android application development, but the user interface is a lot different than the interface for smartphones and wear devices.

The user interface should be designed for a huge screen instead of small screen device. Users interact with applications with a remote control or a game pad. So, although core application development adheres to similar principles, the user experience design changes.

Building APKs for Android TV Modules

Android TV apps and modules are developed to run on Android TV devices so you need a separate APK file to publish them on Google Play and distribute your app to users.

In order to build an APK for the TV application, select Build APK from the Build menu to generate an unsigned APK.

The Project Structure window can be used for configuring the TV module's Properties, Signing, Flavors, Build Types, and Dependencies.

If you've configured signing in the Project Structure window, you can populate the signed APK for the TV module by selecting the Generate Signed APK option from the Build menu. APK files are stored in the project's root folder, under the specified TV module's sub-folder, in build/outputs/apk.

GLASS MODULE

Google Glass is a wearable device first announced at Google I/O 2013. The first batch of devices was sold to a limited number of developers, called Glass Explorers. The Explorer program was later extended to more developers and continued until January 2015. The Glass Explorer program was discontinued, but Google announced that it is committed to newer versions of Glass.

Although Google has stopped producing the Google Glass prototype device, that doesn't mean that there will be no new devices available on the market. Also, you can still get access to Google Glass Preview API version 19 to create Glass modules based on the Glass Explorer edition.

After you select the Glass module shown in Figure 7.1, you will be prompted to name the Glass Module. As you can see in Figure 7.22, the Glass module for this project is named seefrommyglass.

Glass module naming screen.

Figure 7.22 Glass module naming

After naming the Glass app, you will be prompted to select an activity type. The two selections to choose from are shown in Figure 7.23.

Glass module activity selection screen.

Figure 7.23 Glass module activity selection

You can either pick Immersion Activity or Simple Live Card for the application. Immersions are Google Glass apps with a user interface. Live Cards are like widgets on Android phones, which appear together with a clock, like a notification on the screen.

Name the activity or card selection in the next window, and the Glass module is ready to build. All necessary files will be auto-generated by Android Studio right after the activity or live card is named.

Like all other module types, Glass is also an Android application so development of a Glass module is done following the standards and guidelines applicable to any Android applications, with some exceptions. The user experience design is different because user interaction with the Glass device is different than with other devices. Voice recognition is a dominant factor compared to touch to interact with applications on Glass.

Running and Debugging a Glass Module

If you are lucky enough to have a Google Glass device, you can test your module on it. Select Run or Debug from the Run menu and select the Glass module from the dialog box that opens. Then select the hardware device on which to run the Glass module.

Building APKs for Glass Module

Building APKs for a Glass module is not much different than it is for the other modules. When you click Build APK or Generate Signed APK from the Build menu, a Glass module APK will be generated in the Glass modules folder under the project directory in the build/outputs/apk directory.

ANDROID AUTO MODULE

Android Auto is the new kid on the block. There are no cars yet available with Android-equipped hardware. However, Google is very committed to Android Auto and is working with several car manufacturers. Google also demonstrated Android Auto simulators at Google I/O 2015.

Although Android Auto applications will be deployed on a separate platform, eventually Auto will be available as a module in Android Studio and will generate APKs to deploy applications on cars.

There isn't a defined module for Android Auto yet, but you can enable an Android module to work with Android Auto by adding a media or messaging Android service on your application after you create the Android project. Then you need to further configure your app so it works with Android Auto. You do this configuration in the module's Android manifest file, beginning with setting permissions, as in Listing 7.3.

In addition to Android Service extending MediaBrowserService and Android Manifest file permission configurations, you should add a new automative_app_desc.xml resource file for Auto enabling. This file, shown in Listing 7.4 and located in Android Auto's res/xml folder, allocates resources for your application. Then you need to configure the Android Manifest file so that your application uses the resource, as shown in Listing 7.5.

All the steps mentioned previously are auto-generated if you are creating a new project with Android Auto support. There will be two auto activities to select during project creation, as shown in Figure 7.24.

Android Auto activities screen.

Figure 7.24 Android Auto activities

Figure 7.24 shows that Android Auto is enabled for:

  • Media Service—Control media content through the Android Auto interface to play, stop, or skip media selections.
  • Messaging Service—View and respond through the Android Auto interface.

You can add support for Android Auto but there isn't a virtual device yet for it. However, you can work on an Android Auto–enabled application using the Android Auto Desktop Head Unit Emulator. That can be installed with the Android SDK Manager, under the Extras section.

To test Android Auto features, you need to install the Android Auto app to your smartphone from the Google Play Store, which emulates an Auto device. Next, you need to configure ADB from a terminal to communicate with the Android Auto Desktop Head Unit Emulator's auto executable, which is found in the AndroidSDKPath/sdk/extras/google folder. There is no direct connection to Android Studio at this point, but it will certainly come in the future.

GOOGLE CLOUD MODULE

The Google Cloud module, shown previously in Figure 7.1, is not directly related to the Android API or devices but to the backend or cloud side of the Android application.

The Google Cloud module is used for developing and deploying backend applications to the Google App Engine and to scale your application so it can handle multiple users, store data, collect analytical information, and perform all other tasks one can do in the cloud.

Multi-module development capability is a powerful feature of Android Studio that provides more capability, and not only when dealing with Android and Java applications or libraries but also, with backend module development, extended features of Android applications with cloud connectivity. Having this capability helps developers manage the client- and server-side processes within Android Studio while also managing the build process and the deployment of properties.

Adding a New Google Cloud Module

Add the Google Cloud module by selecting it from the window shown in Figure 7.1. The configuration wizard opens, as shown in Figure 7.25.

Screenshot of Google Cloud module setup wizard.

Figure 7.25 Google Cloud module setup wizard

The wizard displays four fields: Module type, Module name, Package name, and Client module. These fields are described in more detail in this section.

The list of module types consists of three types of Google Cloud module templates that can be added to a project, as shown in Figure 7.26.

Screenshot of Google Cloud module types.

Figure 7.26 Google Cloud module types

These Google Cloud module types are:

  • App Engine Java Endpoints Module—This is the module used to build the backend Restful API to handle requests and send data with the REST API.
  • App Engine Backend with Google Cloud Messaging—This module is similar to the Java Endpoints module but has Google Messaging Service support to enable your app to send messages to a server and distribute messages to all connected applications.
  • App Engine Java Servlet Module—This module is used by a client application to build requests and send data using httpClient. The Java Servlet module offers easier or simpler use cases than the Endpoints module.

Additional information can be accessed by clicking the link (blue text) under the selection boxes. The links take you to the module templates’ Github home pages so you can see the detailed explanation of each module type and related source code.

In this section, you want to add a messaging service for our Android module, so name the Cloud module “messaging,” select the App Engine Backend with Google Cloud Messaging Module type, and select the app module for the client, as shown in Figure 7.27.

Screenshot of Google Cloud module configuration.

Figure 7.27 Google Cloud module configuration

After you click Finish and Gradle sync successfully finishes, you can see that the messaging module is added to Android Project view, as shown in Figure 7.28.

Google Cloud module after module creation screen.

Figure 7.28 Google Cloud module after module creation

Running and Debugging a Google Cloud Module

Because the Google Cloud module has a different API, it needs to be handled by Android Studio separately. First build the project by selecting Make Project from the Build menu.

Screenshot of Runnable modules with Google Cloud module.

Figure 7.29 Runnable modules with Google Cloud module

If the build is successful, you can select Run or Debug from the Run menu to list runnable modules, as shown in Figure 7.29.

Selecting the messaging module will generate a runnable Cloud module and start a local App Engine Java Development server to run and debug the module locally. Before running, a message window shows the output of the process and enabled servers, as shown in Figure 7.30.

Message output for the Google Cloud module run screen.

Figure 7.30 Message output for the Google Cloud module run

Figure 7.30 shows the output from the Google Cloud module (http://localhost:8080). There is a second URL that points to the admin page (http://localhost:8080/_eh/admin) for the server configuration and module monitoring. Let's navigate to that page to see that the module is running, as shown in Figure 7.31.

Google Cloud module launch from browser window.

Figure 7.31 Google Cloud module launch from browser

The links provided on the localhost page take you to additional information where you can learn more and further develop the module.

IMPORTING MODULES

This section covers the inclusion of external modules, such as the Gradle project, the Eclipse ADT roject, and .JAR/.AAR packages. The subsections that follow provide information on each type.

Importing a Gradle Project

Importing a Gradle project means importing a project that already has Gradle build scripts inside. This can be either a Java project or another Android module already developed with Android Studio.

It is relatively easy to import a Gradle project. When Import Gradle Project is selected from the New Module window shown in Figure 7.1, a wizard opens in which you can select the Gradle project directory to be imported.

If you select the folder where the Gradle project is stored, the Module name text box shown in Figure 7.32 will appear so you can provide a unique name for the external Gradle project module.

Gradle project module naming  screen.

Figure 7.32 Gradle project module naming

Android Studio automatically imports the project in the background. When that's done, you can run or debug the imported module.

The method used for running or debugging imported modules depends on the type of module. If it is an Android, Wear, or TV module, it can be run as the examples were in previous sections in this chapter.

When Gradle sync finishes successfully, a new module will appear in Android Project view with the name you gave it in the Create New Module window (see Figure 7.32). As you saw with the previous examples in this chapter, when you select Build APK from the Build menu, an APK file will be generated in the module's build/outputs/apk folder.

Importing an Eclipse ADT Project

Importing an Eclipse ADT project helps developers migrate Android applications previously developed in Eclipse with ADT. When you select Import Eclipse ADT Project from the module selection window shown in Figure 7.1, the import wizard will ask for the path of the Eclipse ADT project.

Select the project path by clicking the button to the right of the Source directory text box. Another text field will become active so you can name the module for your project. In Figure 7.33, we imported a previously implemented application named smartHome.

Module naming for an imported Eclipse ADT project window.

Figure 7.33 Module naming for an imported Eclipse ADT project

Next, you need to open the window shown in Figure 7.34. The import wizard will ask you to confirm replacing jar and library dependencies, and Gradle module creation.

Dependency replacement confirmation for an imported Eclipse ADT project window.

Figure 7.34 Dependency replacement confirmation for an imported Eclipse ADT project

If the Eclipse project's Android SDK version is not installed, the setup wizard will prompt you to install a corresponding version from the Android SDK Manager, as shown in Figure 7.35. You can still continue to add the module, but Gradle sync will raise an error to make you install the indicated Android SDK version.

Screenshot of Missing Android SDK version warning message.

Figure 7.35 Missing Android SDK version warning

If the process is successful, you will see the Eclipse application with the name you gave it as a new module in the Project view.

The imported Eclipse ADT project is run as in the previous examples in this chapter. You just need to select the correct device to run or debug the module.

After the Eclipse ADT project has been imported as a module into your Android project, use the Build APK option to generate the module's APK file in the module's new folder, under the Android Studio project's root folder, such as ProjectRootFolder/smarthome/build/outputs/apk.

Importing a JAR/AAR Package

Importing a JAR or ARR package is done by including an external Android or Java library in your existing project. JAR files are legacy Java library containers. When you import a JAR file, you are also importing the library in your project. An AAR package is an Android library package, which contains a compressed Android Library module to load into your project.

When you select Import .JAR/.AAR Package from the window shown in Figure 7.1, the window shown in Figure 7.36 opens. In the File name box, point to the .jar or .aar file to import into your project. In the Subproject name box, enter a name for the module for the Android project.

Screenshot showing how to import a JAR/AAR package.

Figure 7.36 Importing a JAR/AAR package

As shown in Figure 7.37, after successful Gradle sync, the Android Project view shows the packages and their build.gradle files. Android Project view shows only library packages, so there won't be any access to individual files.

Screenshot of Project view of an imported JAR/AAR module.

Figure 7.37 Project view of an imported JAR/AAR module

You use these libraries the same way you use the Java and Android libraries that you added in previous sections. You should define the dependencies for the modules either in the build.gradle file or in the Project Structure window.

REMOVING MODULES FROM A PROJECT

To remove a module from a project, open the Project Structure window. Then select the module from the list and click on the minus (–) button on top of the left panel. Android Studio will then ask for confirmation, as shown in Figure 7.38.

Module Remove confirmation screen.

Figure 7.38 Module Remove confirmation

Following your confirmation, Gradle sync will update the project and clean up the related files and dependencies.

SUMMARY

This chapter covered the details of existing modules in an Android Studio project. We discussed in detail the processes for adding new Android Phone & Tablet, Wear, Library, Glass, Auto, and TV modules to a project (including building, running, and debugging configurations). The chapter explored the procedures for releasing APK and JAR/AAR files, and identifying their locations in the project folder.

Next, the chapter covered importing external modules into an existing project and discussed the Google Cloud and Java Library modules.

An understanding of the structure of modules and how they are managed in Android Studio will enable you to better approach the development of complex Android projects with multiple modules. We believe you will benefit from understanding when the modules discussed in this chapter are needed and how they are managed within the project build system.

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

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