3

I wrote a library which has been uploaded to a private server. When I include the library as a dependency in my app project, and view the source code from one of the library classes, the source code isn't actually decompiled. It only shows the class name and methods. For example:

package com.example.library.ui

public final class RoundedDrawable public constructor() : android.graphics.drawable.Drawable {

    public final var backgroundColor: kotlin.Int /* compiled code */

    // ... other similar fields

    public open fun draw(canvas: android.graphics.Canvas): kotlin.Unit { /* compiled code */ }

    // ... other similar functions

}

As you can see, it only displays /* compiled code */ comments, and not the full source code. There is an option presented to "Decompile to Java"; which works, but I would much rather see the Kotlin source. Is this possible?

I found similar question that explains how to show the original Java code, but nothing for Kotlin.

Bryan
  • 14,756
  • 10
  • 70
  • 125
  • 1
    Actually, this seems like a duplicate of [this](https://stackoverflow.com/q/46565591/7366707), which hasn't yet been answered after 3 months :/ – Salem Jan 26 '18 at 19:51
  • @Moira That it does, good find. Based on that question, it appears I do have to upload the source files separately from the aar file using `task sourcesJar`. Though I'm not exactly sure how that works, or if the server I am using is prepared to handle that. I guess I need to do some more research. – Bryan Jan 26 '18 at 20:07

2 Answers2

0

Are you using Gradle?

You need to download sources for the library:

idea {
    module {
        downloadJavadoc = true
        downloadSources = true
    }
}

If attached sources/documentation are found, IntelliJ will use these when viewing .class files.

With Maven, you can do this from the tool window with Right click -> Download Sources

Other than that, decompiling a .class file to Kotlin is not possible with good results. Despector has a very rough Kotlin "decompiler".

Salem
  • 13,516
  • 4
  • 51
  • 70
  • Apparently Android Studio already [downloads libraries with sources](https://stackoverflow.com/a/17444845/5115932). What do you mean by "attached sources"? Do I have to provide the decompiled code separately on the server? – Bryan Jan 26 '18 at 19:22
  • @Bryan Well, there [seems to be some doubt about that answer](https://stackoverflow.com/questions/17426628/how-to-make-android-studio-download-dependencies-sources-and-javadoc/17444845#comment47808223_17444845), and I can confirm that IntelliJ has never done this for me. Maybe this is specific to the Android plugin, but that would be strange – Salem Jan 26 '18 at 19:45
  • Ah, that's true, I only read it quick. I was looking for a way to set `downloadJavadoc = true` in my Android project, and that was the answer that I found. I don't know where I would place it (assuming the Android plugin supports it). – Bryan Jan 26 '18 at 19:52
0

When Releasing the library with maven publish, you can specify the source jar.

In this example, if your library is in Kotlin and Java, to add both source codes.

task androidSourcesJar(type: Jar) {
       archiveClassifier = 'sources'
       from (android.sourceSets.main.java.sourceFiles, android.sourceSets.main.kotlin.sourceFiles)
}
Loïc Dumas
  • 1,036
  • 10
  • 18