Converting an Application Module to a Library Module
JAR packages can be generated from both application modules and library modules. The process for converting an application module to a library module is straightforward.
Step 1: Modify the Build Configuration
Open your module's build.gradle file and locate the plugin declarasion at the top. Change:
apply plugin: 'com.android.application'
to:
apply plugin: 'com.android.library'
Step 2: Remove Application ID
Comment out or remove the applicationId line in your build.gradle file, as library modules do not require an application identifier.
Generating the JAR Package
Classic Approach (Pre-3.4.2)
Add the following task to the android block in your build.gradle file, alongside defaultConfig and buildTypes:
task extractJar(type: Copy) {
delete 'build/libs/output.jar'
from('build/intermediates/bundles/debug/')
into('build/libs/')
include('classes.jar')
rename('classes.jar', 'output.jar')
}
extractJar.dependsOn(build)
Modern Approach (Android Studio 3.4.2+)
For newer versions, use the updated path:
task extractJar(type: Copy) {
delete 'build/outputs/application.jar'
from('build/intermediates/packaged-classes/release/')
into('build/libs/')
include('classes.jar')
rename('classes.jar', 'application.jar')
}
extractJar.dependsOn(build)
Android Studio 4.0 and Above
For Studio 4.0 and newer versions, the intermediate path has changed:
task extractJar(type: Copy) {
delete 'build/libs/module.jar'
from('build/intermediates/aar_main_jar/release/')
into('build/libs/')
include('classes.jar')
rename('classes.jar', 'module.jar')
}
extractJar.dependsOn(build)
Executing the Build Task
Command Line Method
Open a terminal in your project root directory and run:
./gradlew extractJar
Gradle Tool Window Method
Open the Gradle tool window in Android Studio, navigate to your module, then:
Tasks → build → extractJar
Double-click extractJar to execute the task. The generated JAR file will appear in the build/libs/ directory.
Task Configuration Notes
- The task name can be customized, but ensure consistency between
task nameandname.dependsOn(build) - Adjust the source and destination paths according to your Android Studio version
- The output directory must exist or be created by the build process