Now there is a scenario where you need to write a base SDK for upper-level business calls and consider introducing Kotlin, so it takes you three minutes to write all the code quickly, then assembleRelease outputs the AAR, and then introduces the AAR into the main project. When you want to debug the KT code in the main project, you find that you cannot debug it, and the effect is as follows:

Due to the time constraints of the project, WHEN I encountered this problem, I changed kt to Java immediately due to the small amount of code. But the Java syntax is too verbose in some cases, and in order to introduce KT’s coroutine features, if I continue to use KT in the base library, I need to fix the problem that KT’s AAR package can’t debug.

The process of solving the problem is always so tortuous, the feeling after solving the problem is always so refreshed. Conclusion first, there are two ways to solve this problem:

  1. By importing submodules, configure a switch to import the source code in the submodule when you need to debug the code and rely on the AAR when you publish it
  2. Using Maven libraries, whether local or remote, you distribute code with the source code

Submodule mode

This approach relies on a single switch variable to operate on, and I don’t want to maintain another switch value at all, so I don’t recommend it. The following is a brief explanation of how to operate. It was originally aar mode, but now it is changed to sub-module mode, as shown in the figure below:

include 'basic-net'
project(':basic-net').projectDir = new File(settingsDir, '.. /TestKtAarLib/basic-net')
Copy the code

There is no excuse not to debug the source code in this way. You can modify the source code in time to support your idea, but only to support it. If the base library code is not maintained by you, or you have a clear division of labor, it is not recommended that you modify the commit.

Maven repository way

Recommended this way, the reason is very simple, after the release of no matter, the upper business use student permissions are only debug level, you will not modify your code for some inexplicable reason without knowing it.

If you want to create a Maven service, go here to download it and extract the code: hcXU. Then unzip it and enter the CMD directory: Nexus-3.22.0-02-win64 \nexus-3.22.0-02\bin>, run nexus-3.22.0-02-02 \bin>, run nexus-3.22.0-02-02 \bin>, run nexus-3.22.0-02-02 \bin> When you see the words Started Sonatype Nexus OSS 3.22.0-02, you are ready to serve, and then open http://localhost:8081/

Then log in (admin/admin123) and copy maven-Releases and Maven-snapshots:

  • http://localhost:8081/repository/maven-releases/
  • http://localhost:8081/repository/maven-snapshots/

Using gradle.properties to configure maven’s POM properties as well as maven’s repository address and account secret. Maven’s POM properties can also be placed under each module instead of being written to gradle.properties.

# MAVEN required configuration PROJ_GROUP_ID = com.leeeyou.testktaar.basic.net PROJ_ARTIFACTID = basic -.net PROJ_VERSION = 1.1.0 PROJ_DESCRIPTION =test kt aar debug PROJ_TYPE = aar # Here is the maven address and account secret MAVEN_REPO_RELEASE_URL=http://localhost:8081/repository/maven-releases/ MAVEN_REPO_SNAPSHOT_URL=http://localhost:8081/repository/maven-snapshots/ NEXUS_USERNAME=admin NEXUS_PASSWORD=oooo9999Copy the code

Now prepare a maven_pusk. gradle file to publish aar and source code to the Maven repository, and apply from: ‘maven_pusk. gradle’ to the build.gradle file.

apply plugin: 'maven'
apply plugin: 'signing'

configurations {
    deployerJars
}

repositories {
    mavenCentral()
}

// Call it "Release or Snapshots"
def isReleaseBuild() {
    return! PROJ_VERSION.contains("SNAPSHOT");
}

// Get the repository URL
def getRepositoryUrl() {
    return isReleaseBuild() ? MAVEN_REPO_RELEASE_URL : MAVEN_REPO_SNAPSHOT_URL;
}

uploadArchives {
    repositories {
        mavenDeployer {
            beforeDeployment {
                MavenDeployment deployment -> signing.signPom(deployment)
            }

            pom.version = PROJ_VERSION
            pom.artifactId = PROJ_ARTIFACTID
            pom.groupId = PROJ_GROUP_ID

            repository(url: getRepositoryUrl()) {
                authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) // Maven authorization information}}}}// Perform a digital signature
signing {
    // Only executed when the published version & "uploadArchives" task exists
    required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
    sign configurations.archives
}

// Upload the source code
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

artifacts {
    archives androidSourcesJar
}
Copy the code

Finally, after the uploadArchives task is executed, the AAR and the source code are successfully published to the repository

Now that we’re done with the project configuration and publishing to the Maven repository, it’s time to use the newly published AAR. First configure the repository address in the build.gradle root, and then introduce basIC-NET dependencies in the specific Module.

/ / root build. Gradle
maven {
    url 'http://localhost:8081/repository/maven-releases/'
}

/ / module build. Gradle
implementation '- net: com.leeeyou.testktaar.basic.net:, basic 1.1.0'
Copy the code

At this point I have successfully pulled down the 1.1.0 code, and the test contains the source code, so I feel free to debug the Post and GET functions of NetRequest. Long lost debug interface, really sweet

Afterword.

My sample program is as clean as Lake Baikal, while yours is as dirty as your neighborhood’s garbage dump. No demeaning here, but the simplicity and complexity of the environment can cause a lot of strange problems when you introduce dependency libraries, such as re-import problems, dependency passing problems, etc., which depend on our ability to solve the problems ourselves.

Sonatype is also one of the easiest to use and has many complex features, such as permissions and groups, that you can use to find information later. Updating Assets: Repository does not allow updating Assets. Then you will enter the Sonatype configuration page to allow anonymous access.

In general, if you want to debug an AAR library, find a way to get the source code, source code in hand, DEBUG I have.


Reference:

  • Maven uses nexus3.3 to build private servers on Windows
  • Android Studio teaches you to set up and use Maven repositories in 5 minutes
  • In response to the debug Kotlin aar question, the official Kotlin forum suggested the introduction of the source Module
  • Repository does not allow updating assets
  • Build. gradle manages dependent versions