Search code examples
androidandroid-studiojavadocaar

How to export AAR library with its documentation?


I'm wondering why my library always lost its documentation once it was built to AAR format. Also, Android Studio shrinks the code, i.e. the code before built always different with the code after built into AAR format, none originals. I want my library to has its own documentation and has the original code. You can see the different below:

Before built

before

After built into AAR format

after

Where is the documentation?! Also, you'll notice that the whole code are really shrunk and different.

So, how do I export AAR library with its documentation without shrink the code?


Solution

  • The code "shrinks" because, once you create the AAR, the original source code isn't included in the AAR. It's just the Java byte code.

    Then when you use your AAR library in an app, the source code is reverse engineered from the byte code. The original variable names are not available and there can be some coding style differences due to byte code compiler's optimizations and whatnot. And of course the comments are not included in the byte code because it's for the computer and the comments are for the people.

    So when using the AAR library, if you want to see the original source code when debugging etc., you need to deliver it with the AAR as a separate source JAR and probably tell Android Studio to use the source JAR if it doesn't do it automatically.

    I've been able to automate the source JAR creation as part of the process of "uploading" the generated AAR/JAR to a local Maven repository. I don't know what kind of a build system you have so I won't go into those details.

    Anyway you can add this to your modules's build.gradle:

    task sourcesJar(type: Jar) {
        classifier = 'sources'
        from android.sourceSets.main.java.srcDirs
    }
    

    You'll then need to run it separately whenever you want to create/update the source JAR:

    Gradle -> Tasks -> Other -> sourcesJar

    And probably there's some way to automate the task to be part of the assembleRelease task but I'm not very much familiar with Gradle yet.

    The source JAR will appear in the ...\MyLibrary\app\build\libs\ path.