I'm new to the gradle build system and IntelliJ.
So how do I create an Android Library Project (e.g. com.myapp.lib1) and the application project (e.g. com.myapp.app) and make the build system include com.myapp.lib1 on the application project?
I went to the Project Structure -> Modules -> My App project and added a dependency to the lib project. IntelliJ now can recognize classes from the lib project when used in the app project, but when I run the app project, there are errors like:
Gradle: error: package com.myapp.lib1 does not exist
You can add a new module to any application as Blundell says on his answer and then reference it from any other application.
If you want to move the module to any place on your computer just move the module folder (modules are completely independent), then you will have to reference the module.
To reference this module you should:
On build.gradle file of your app add:
On settings.gradle file add the following:
The simplest way for me to create and reuse a library project:
file > new > new module
(and answer the UI questions)include ':myLibrary'
check/or add if in the file build.gradle:
dependencies { ... compile project(':myLibrary') }
To reuse this library module in another project, copy it's folder in the project instead of step 1 and do the steps 2 and 3.
You can also create a new studio application project You can easily change an existing application module to a library module by changing the plugin assignment in the build.gradle file to com.android.library.
to
more here
Don't forget to use
apply plugin: 'com.android.library'
in yourbuild.gradle
instead of apply plugin:'com.android.application'
Purpose: Android library at single place - Share across multiple projects http://raevilman.blogspot.com/2016/02/android-library-project-using-android.html
Check out this link about multi project setups.
Some things to point out, make sure you have your settings.gradle updated to reference both the app and library modules.
settings.gradle:
include ':app', ':libraries:lib1', ':libraries:lib2'
Also make sure that the app's build.gradle has the followng:
You should have the following structure:
The app's build.gradle should use the
com.android.application
plugin while any libraries' build.gradle should use thecom.android.library
plugin.The Android Studio IDE should update if you're able to build from the command line with this setup.
Documentation Way
This is the recommended way as per the advice given in the Android Studio documentation.
Create a library module
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/
.Add your library as a dependency
You can add your library as a dependency to another project like this:
In the settings.gradle file, make sure your library name is there.
In the app's build.gradle file, add the compile line to the dependencies section:
That's it. You should be able to use your library now.
Notes
If you want to make your library easily available to a larger audience, consider using JitPac or JCenter.