How to keep Source code for Android library?

Viewed 176

I created a small Android library for personal use and distribute it over Jitpack. If I add it to my projects via Gradle and go to inspect the source code of an imported method, I can only see a decompiled .class file. How can I provide the consumers of my library the source code?

2 Answers

Then do not add it as a Gradle dependency. Instead add it as a module.

File -> New -> New Module

Add your source code here.

Add this module path in app level Gradle dependency

For example if your module name is MyModule

implementation project(':MyModule')

That's it. You are good to go.

So in the end I solved it by using a JAR like the comments of Henry and Morrison suggested:

In my libraries build.gradle:

apply plugin: 'maven-publish'

task sourceJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier "sources"
}

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                // Applies the component for the release build variant.
                from components.release

                groupId = 'REPLACE WITH YOUR JITPACK ID (com.github.xxx)'
                version = 'x.x'

                // Adds javadocs and sources as separate jars.
                artifact sourceJar
            }
        }
    }
}
Related