How to export AAR library with its documentation?

半世苍凉 提交于 2019-12-02 15:58:30

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 wan't 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 a 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.

I agree with @MarkusKauppinen.

The main problem after you build the sources jar is you cannot use this jar as your dependencies. This is only a source code that can't be used for writing code, i.e. you can't import anything, because the jar contains *.java files instead of *.class files. So that, do the following:

Step #1 Build an AAR format for your library. Usually, it located in [project]/[library-module]/build/outputs/aar/library-release.aar

Step #2 Add the AAR file as module. Go to File > New > New module... > Import .JAR/.AAR package > Next.

Then, locate the AAR file and click Finish.

Step #3 Go to Project Structure by clicking this icon

, and add the module we just made to app's dependencies.

Step #4 Add @MarkusKauppinen's gradle task to library module's build.gradle and sync the project with gradle.

Step #5 Go to right pane of Android Studio > click Gradle > refresh gradle task by clicking this icon

.

Step #6 Find and run (by double click) sourcesJar in :library-module > Tasks > other.

Step #7 After the task is completed, you'll find the sources jar in

library-module\build\libs\..-sources.jar.

Step #8 Import at least one class to app's module, e.g. in MainActivity. Then, press Ctrl + click on the class. Click Choose sources and browse the sources jar as mentioned in step #7.

Finally, you can see the sources code with its documentation.

In kotlin you can use an open source library called Dokka. It's very simple to use and generates the documentation in html format, like you may do with javaDoc. Basically the library adds a method in your gradle methods list called "dokka". By launching the command the documentation gets generated :).

You can see an example below:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!