My company is making several Android projects on Android Studio, which all of them share some similar codes, such as custom views, customised HTTP clients, and many other things.
The problem I am facing is, I am new to Android Studio and I am not sure how to extract these common codes across several projects to a single Android Library modules that will be referenced by these projects.
In Eclipse it is very simple, just create a new Android Library project, and then move your code over there, and set the Android Application projects to reference the common library.
How can such refactoring be done by Android Studio?
Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Library Dependency in the dropdown. In the Add Library Dependency dialog, use the search box to find the library to add.
A project with multiple Gradle modules is known as a multi-module project. In a multi-module project that ships as a single APK with no feature modules, it's common to have an app module that can depend on most modules of your project and a base or core module that the rest of the modules usually depend on.
The main difference is aar is splitted inside android to jar. If your app will be used only in user app only in android studio then aar is preferred. If you are planning for app to communicate with c/c++ compiled lib.so file jar is preferred.
Select the source directory of the Module you want to import and click Finish. Open Project Structure Dialog (You can open the PSD by selecting File > Project Structure) and from the left panel click on Dependencies. Select the module from the Module(Middle) section In which you want to add module dependency.
Our company used a structure with multiple projects with shared modules. Suppose you have 2 projects, project1 and project2 which are 2 independent Android Studio projects and want to share some modules. The folder structure will be like this:
source-code-root-folder/
+ android-studio-project1/
+ project1-app-module/
+ project1-internal-module/
+ android-studio-project2/
+ project2-app-module/
+ project2-internal-module/
+ shared-module1/
+ shared-module2/
You can first create the projects and modules from Android studio. Then relocate the folders like the structure above. Then update the setting in project1 by putting this settings in the source-code-root-folder/android-studio-project1/settings.gradle
:
include ':android-studio-project1'
include ':project1-app-module'
include ':project1-internal-module'
include ':..:shared-module1'
include ':..:shared-module2'
Then open the android-studio-project1/project1-app-module/build.gradle
and update the dependencies:
...
dependencies {
...
compile project(':project1-internal-module')
compile project(':..:shared-module1')
compile project(':..:shared-module2')
}
This will make project1 be able to load the internal module and the shared modules as well. Try sync and build your project1 by running build.gradle in the project1 and it should work. Of course similar setting can be used for the project2.
Hope that this can help you.
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