How to create a library project in Android Studio and an application project that uses the library project
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
Solution 1:
I wonder why there is no example of stand alone jar project.
In eclipse, we just check "Is Library" box in project setting dialog.
In Android studio, I followed this steps and got a jar file.
Create a project.
open file in the left project menu.(app/build.gradle): Gradle Scripts > build.gradle(Module: XXX)
change one line:
apply plugin: 'com.android.application'
->'apply plugin: com.android.library'
remove applicationId in the file:
applicationId "com.mycompany.testproject"
build project: Build > Rebuild Project
then you can get aar file: app > build > outputs > aar folder
change
aar
file extension name intozip
unzip, and you can see
classes.jar
in the folder. rename and use it!
Anyway, I don't know why google makes jar creation so troublesome in android studio.
Solution 2:
To create a library:
File > New Module
select Android Library
To use the library add it as a dependancy:
File > Project Structure > Modules > Dependencies
Then add the module (android library) as a module dependency.
Run your project. It will work.