I'm not familiar with a way to achieve your requirement directly on a library module.
As a workaround, you can:
- Write your code in a library (that is, built with com.android.library plugin) module.
- Add an application module (that is, built with com.android.application plugin, without any sources or dependencies besides your library module) which will function as an wrapper around your library.
- Build the project, and use its generated dex file.
I've created a sample project to illustrate that.
The project structure is the following (I've omitted irrelevant files):
├── app
│ ├── build.gradle
│ └── src
│ └── main
│ └── AndroidManifest.xml
└── testsdk
├── build.gradle
└── src
└── main
├── AndroidManifest.xml
└── java
└── info
└── osom
└── testsdk
└── TestClass.java
testsdk is a library module, app is a library wrapper module. TestClass.java
is a sample source file, similar to source files included in your library module.
Both AndroidManifest.xml
files contain the bare minimum functionality, i.e. app/src/main/AndroidManifest.xml
content is:
<manifest package="info.osom.q57441198"/>
testsdk's build.gradle
file includes a sample dependency, just to simulate your library's dependencies.
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
}
dependencies {
implementation 'com.jakewharton.timber:timber:4.7.1'
}
Wrapper's (app) module build.gradle
file adds a dependency on library module:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 21
}
}
dependencies {
implementation project(':testsdk')
}
Build the project using:
$ ./gradlew clean assembleRelease
and then find the generated dex file under app/build/intermediates/dex/release/mergeDexRelease/out/classes.dex
.
You can use baksmali to verify all your library sources and library's dependencies are bundled inside the dex:
$ baksmali classes.dex
$ tree out
out
├── info
│ └── osom
│ ├── q57441198
│ │ ├── BuildConfig.smali
│ │ └── R.smali
│ └── testsdk
│ ├── BuildConfig.smali
│ ├── R.smali
│ └── TestClass.smali
├── org
│ ├── intellij
│ │ └── lang
│ │ └── annotations
│ │ ├── Flow.smali
│ │ ├── // redacted...
│ │ └── Subst.smali
│ └── jetbrains
│ └── annotations
│ ├── Async$Execute.smali
│ ├── // redacted...
│ └── TestOnly.smali
└── timber
└── log
├── R.smali
├── Timber$1.smali
├── Timber$DebugTree.smali
├── Timber$Tree.smali
└── Timber.smali
The only overhead the wrapper (app) module adds to the dex file are the following two classes: info/osom/q57441198/BuildConfig.smali
and info/osom/q57441198/R.smali
. You can simply disregard them. If you insist on removing them, there are tools to help you with that.