Exploring Android Studio: Library Projects

Larry

Larry Schiefer

Welcome to Part 7 of this review of the Pluralsight course Exploring Android Studio by Larry Schiefer.

Larry Schiefer is the CTO and co-founder of HiQES, a mobile, embedded, and application development service company. Larry has over 16 years of experience leading teams, and designing and developing high performance and robust software, and is a Google GDE.

Library Projects With Android Studio

Overview

Android Studio supports libraries in the form of JAR files, as well as modules within the project.

Larry warns that the current version of Android Studio (as at Oct 2014) does not support importing Eclipse projects.
This support has now been added, but it’s not covered in this course.

Creating and Using a Library Module

We start with the code we wrote in the previous modules. We want to extract this class into a utility library for other project to use.

Select File->New->New Module…

Select Android Library and click Next

Larry describes which names and drop down option to select in the next screen.
We don’t need an activity for our library so we uncheck that and click Finish.

This creates an empty library module.
We add a new Java class and we cut and paste the code we need into here.
Android Studio is smart enough to figure out which import statements we need to import as well.

There are some formatting issues, but Larry explains how we can fix this.

We see some compiler errors. To fix this we start by by going to “Open Module Settings”, choosing Modules of the left, and then clicking the Dependencies tab on the right.

From here we can add a module dependency. We still see errors but now we get blue popups suggesting we import the class from our Util Lib.

Larry says this is much easier than trying to do the import and edit the Gradle file manually. If Gradle is new to you don’t worry because it is covered later on in this course.

Manual Library Migration

We can also manually migrate an existing project from Eclipse. We see an example Eclipse project PkgUtils.

Again in Android Studio we create a new Android Library. We name and version it exactly the same as the Eclipse project.

Once created, we can delete the res directory and strip down the AndroidManifest.xml. Then we can just copy our existing Java class into our new Android library module.

The last thing is to update the app module so it utilizes this new library.

Using JAR Files

We take a different approach to solving this problem: by using a JAR file directly.

First we want to remove the work we did in the previous lesson. We see the settings.gradle includes ‘:pkgutils’ and we remove that.
We also remove pkgUtils from our module dependencies.

We find the JAR file in the bin folder of our Eclipse project, and copy it into our libs folder.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s