0

I want to compile libs/*.jar, dependent libraries and my java/kt code file into dex in library module, then the android app can download these dex files, then dynamically load them by DexClassLoader.

Android Studio provides a Gradle task "dexRelease" to compile my own code files into a dex file, but can not compile libs/*.jar and dependent libraries into dex file.

gradlew testsdk:dexRelease

I expect compile libs/*.jar, dependent libraries and my java/kt code file into dex in library module.

Teffy
  • 51
  • 6
  • I'm assuming you're trying to achieve a fat-aar (which can be dexed). Have you seen [this question](https://stackoverflow.com/q/28605367/1233652)? – Alex Lipov Aug 11 '19 at 04:57
  • Yes, I had seen many questions, but those questions don't quite meet my needs. I actually only need the final dex file, with my own code, jar package, third-party dependencies inside. – Teffy Aug 14 '19 at 08:40

1 Answers1

1

I'm not familiar with a way to achieve your requirement directly on a library module.

As a workaround, you can:

  1. Write your code in a library (that is, built with com.android.library plugin) module.
  2. 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.
  3. 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.

Alex Lipov
  • 13,503
  • 5
  • 64
  • 87