The above title is just a summary, this article is actually a record of trampling pits.

The cause of

  1. When I was working on the Kotlin project, my senior told me that the first-party Kotlin library that the project relied on couldn’t be debugged.
  2. Check the call stack, rely on the first party library class unexpectedly no source, is decompiled out.

Direct cause

  1. In fact, when my predecessor told me cause 1, I didn’t understand why Kotlin couldn’t debug it.
  2. This afternoon when we checked the memory leak problem, we found that the aar package of the first party was not associated with the source code.
  3. So I went to the Maven warehouse*-source.jarDownload the source code package locally and unpack it, and find that for mixed-development projects,*-source.jarThe package only containsJavaThe source code,KotlinThe source code is not packaged.
  4. All of a sudden, I remembered what the elder said to me can not debug is what situation, no source code! Unable to trace debugging.

To solve the process

  • The gradle Task that lib uses to package and publish to Maven looks like this:

    task sourcesJar(type: Jar) {
      if (project.hasProperty("android")) {
          from android.sourceSets.main.java.sourceFiles
      } else {
          println project
          from sourceSets.main.allSource
      }
      classifier = 'sources'
    }
    Copy the code
  • I went to the Internet to search, it seems that everyone is so configured from

  • When you go to Google for the detailed keywords of “Kotlin” + “AAR” + “source code”, you can find most of the maven-publish and other configurations of the open source packaged plug-in, while our company’s is written by ourselves, so we can’t refer to it.

  • Try the use from android. SourceSets. Main. AllSource, from android. SourceSets. The main configuration, such as an error. This may be because the project uses the default source path and is not configured with sourceSets, so it cannot be checked

  • But occasionally I use from Android.sourcesets. Main and get the following error message

    * What went wrong:
    Cannot convert the provided notation to a File or URI: source set main.
    The following types/formats are supported:
    - A String or CharSequence path, for example 'src/main/java' or '/usr/include'.
    - A String or CharSequence URI, for example 'file:/usr/include'.
    - A File instance.
    - A Path instance.
    - A Directory instance.
    - A RegularFile instance.
    - A URI or URL instance.
    Copy the code

Inspiration, from can follow the path. Try the following configuration

from android.sourceSets.main.java.getSrcDirs()
Copy the code

*-source.jar also packages kt source code. The problem of not being able to debug is gone.

conclusion

When using. SourceFiles, Gradle filters files of type. Java for packaging.

When.getsrcdirs () takes the entire directory as an argument, Gradle no longer checks for file suffixes and packages all files in all directories.

Finally, the packaging task was modified as follows to solve the packaging problem of the project with Kotlin:

task sourcesJar(type: Jar) {
    if (project.hasProperty("kotlin")) {
        from android.sourceSets.main.java.getSrcDirs()
    } else if (project.hasProperty("android")) {
        from android.sourceSets.main.java.sourceFiles
    } else {
        println project
        from sourceSets.main.allSource
    }
    classifier = 'sources'
}
Copy the code