Search code examples
androidgradleandroid-libraryjitpack

How to keep Source code for Android library?


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?


Solution

  • 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
                }
            }
        }
    }