The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
Android library projects are the primary unit of Android source reuse, particularly where that source involves more than just Java source code, such as Android resources.
In this chapter, we will explore the basics of setting up and using an Android library project.
Understanding this chapter requires that you have read the core chapters of this book.
An Android library project, in many respects, looks like a regular Android project. It has source code and resources. It has a manifest.
What it does not do, though, is build an APK file. Instead, it represents a basket of programming assets that the Android build tools know how to blend in with regular Android projects.
Making a project be an Android library project is simply a matter of choosing the right Android Plugin for Gradle.
Rather than have:
apply plugin: 'com.android.application'
apply plugin: 'com.android.library'
That’s it — the
com.android.library plugin now knows that it is creating
a library, not an app.
The real question is, where are you making this library? In many cases, you will do so as a module in a project, where there is another module that is an app. This covers both:
Adding new modules to an Android Studio project is handled most simply via the new-module wizard, which you can bring up via File > New > New Module… from the main menu. This brings up the first page of the new-module wizard:
Figure 342: Android Studio New-Module Wizard, First Page
To add a library project as a module to an existing project, choose “Android Library” in the list of module types, then click Next to proceed to the second page of the wizard:
Figure 343: Android Studio New-Module Wizard, Second Page
This collects some bits of information, including:
At this point, clicking Next will take you to the same new-activity flow that you saw when creating a new project. If you want an activity to be generated for you in this library, proceed by selecting the activity template and providing the activity template configuration data. If you do not want an activity, choose “Add No Activity” in the grid of templates, then click “Finish” to create the module.
In the end, the new-module wizard will set up the new module for you, in
your designated subdirectory of the project, including modifying
to list this subdirectory as being a module within the project. At this
point, you will be able to start using the library within the project itself.
The preview of this section may contain nuts.
The preview of this section left for Hollywood to appear in a reality TV show.
The preview of this section was fed to a gremlin, after midnight.