This question might have been answered somewhere but couldn't find the appropriate one.
I want to know how can I create a common utility library project in Android Studio. I want to write some common classes and some common methods within them to use in Android app projects. Probably like how .dll are in Windows world - a set of common methods that can be shared among multiple consumers.
Thanks in advance.
Android App Development for Beginners The Android Support Library package is a set of code libraries that provide backward-compatible versions of Android framework APIs as well as features that are only available through the library APIs. Each Support Library is backward-compatible to a specific Android API level.
Create a new project to make your library in. Click File > New > New Module > Android Library > Next > (choose name) > Finish. Then add whatever classes and resourced you want to your library. When you build the module an AAR file will be created. You can find it in project-name/module-name/build/outputs/aar/.
To use your new Android library's code in another app or library module within the same project, add a project-level dependency: Navigate to File > Project Structure > Dependencies. Select the Module in which you’ll use the library. In the Declared Dependencies tab, click and select Module Dependency in the dropdown.
Just click‚ synch the project with Gradle. Your library should be available for your project. Click on “ Import Existing Project “. Step 2: Select the desired library and the desired module. Then click finish. Android Studio will import the library into your project and will sync Gradle files.
It turns out there’s a better way to manage multiple projects in Android Studio. The trick is to create separate Android Studio projects for your libraries and to tell gradle that the module for the library that your app depends on is located in the library’s project directory.
Simplest way to do this :
Right click on your opened project in Android Studio and select New > Module
In the left Pane choose Android Library and click on next.
Enter all details, untick Create Activity, Theme and all if not required.
Choose API level same as your project and Next, Next, Next .
Now you will see an another directory inside your project, build.gradle
for library will be automatically configured for you.
If your module/library name is "mylibrary",
include ':mylibrary'
will be automatically added in settings.gradle file inside root directory of your project.
Now open your main module and insert this line in dependency block :
compile project(':mylibrary')
If you want to use same library in other projects, you have to copy the library module to that particular project using File Explore and have to configure settings.gradle and main module's build.gradle manually.
An old question, but even now there doens't seem to be a propper solution for libraries in AndroidStudio. I've been looking into making the migration step for some time now and this is what I found.
Lets say we've got a Library (called lib) which contains shared code and an Application project (called app) which wants to use said library's code.
The key is defining a project in settings.gradle
in the Project's root (/Some/Path/MyProjects/Project/settings.gradle
) This file should already exist and contain something like include ':app'
.
We will modify this file to also include the library AND define the library with the following two lines:
/Some/Path/MyProjects/Project/settings.gradle
... // tell gradle we want to include a module called 'lib' include 'lib' // tell gradle there is a module called 'lib', found in a specific directory // Note the /app on the end, the project structure wraps a project around a module, you want to refer that module! project (':lib').projectDir = new File('/Some/Path/MyProjects/Library/app') ...
Also edit the projects build.gradle /Some/Path/MyProjects/Project/app/build.gradle
to depend on the newly added module lib
/Some/Path/MyProjects/Project/app/build.gradle`
... dependencies { ... compile project (':lib') // we want to depend on our library } ...
When working with multiple developers or for the sake of flexibility, I use my gradle.properties
in my .gradle
directory (for *nix based systems usually found in homedir, not sure where Windows looks for it). Do note that you might need to create the directory and file yourself.
In this file you can create, if you like, constants that can be used by you throughout your gradle files. For example, mine contains something like the following:
theconstant=/Some/Path/MyProjects/Library/app
note the seemingly missing quotes (not sure whether thats really needed tho) Now we can replace
project (':lib').projectDir = new File('/Some/Path/MyProjects/Library/app')
with
project (':lib').projectDir = new File(theconstant)
Now you and your team could define this constant (which might differ per machine) and change the path accordingly.
BuildConfig.DEBUG
!)I havn't found the chance to properly test this through, yet this seems like the most elegant solution for the time being! I would like to hear your thoughts on this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With