When you create a new android project, there is no option to create Android Library
project.
When you created a new android project (an app), you can created File -> New Module -> Android Library
. But this library only live within this app project and very difficult to be shared with other projects.
NOTE: This is one of those Android thing which you thought should work by default without a hack, but sadly that is not the case.
Create Android Library Project
Create a new project File -> New -> New Project
, and give a name like MyLib
. Since this is intented as a library project, choose Add No Activity
.
By default Android Studio create a default module named app
. Right click the app
module, Refactor -> Rename
to mylib
.
In mylib
modules's build.gradle
file, change apply plugin: 'com.android.application'
to apply plugin: 'com.android.library'
. Remove applicationId ...
as well.
You should use variable for libraries such as kotlin and android support library (e.g. implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
). The benefit is that your library will always follow the project's build.gradle
gradle variable when you include this library in another project.
project's build.gradle
buildscript {
ext {
kotlin_version = '1.2.41'
android_support_library_version = '27.1.1'
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
mylib
's build.gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "com.android.support:design:$android_support_library_version"
implementation "com.android.support:appcompat-v7:$android_support_library_version"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
In AndroidManufest.xml
, remove the content leaving the manifest
tag only.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="YOUR_PACKAGE_NAME" />
You can delete the following files and directories:
- mipmaps
- drawable ic_laucher
- values/styles
- values/colors
Now, the Android Library
project is completed.
Include Android Library Project In Another Project
Edit settings.gradle
include ':mylib', ':app'
project(':mylib').projectDir = new File(settingsDir, '../MyLib/mylib')
Edit app
module build.gradle
to add implementation project(":mylib")
in dependencies.
dependencies {
// existing line
implementation fileTree(dir: 'libs', include: ['*.jar'])
// include this
implementation project(":mylib")
}
NOTE: You can also use File -> Project Structure -> Modules - app -> Depencies
, Click + (Green Plus Button)
and select Module Dependency
then pick mylib
.
References: