“Build North” is a series of articles exploring Android building. It covers Gradle, Android Gradle Plugin, Kotlin Script, and other tools, as well as related architecture applications. To find the problem to solve the problem as the starting point, transfer new knowledge to improve production efficiency as the foothold.

Like this series of friends can pay attention to my public numberEfficient Android Development, focuses on Android engineering efficiency and development experience, covering topics such as infrastructure, Kotlin Multiplatform, Gradle build optimization, etc. (at the same time, it also pushes the latest Podcast of “Binary Radio”).

I recently changed projects and reconfigured the Gradle environment. I found that all Library modules could not Debug or could only fetch some global variables. Mystery, suddenly found that I am clearly dozen assembleDebug Debug package, my Library Module were transformClassesAndResourcesWithProguardForRelease of execution. Debug buildTypes is configured on all of these modules, but it doesn’t work and instead has a confusing release package.

debug {
    debuggable true
    jniDebuggable true
    minifyEnabled false
    zipAlignEnabled false
    signingConfig signingConfigs.debug
    ...
}
release { 
    debuggable false
    jniDebuggable false
    minifyEnabled true
    zipAlignEnabled true
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.cfg'
    signingConfig signingConfigs.release
}
Copy the code

This time, o @ chorale gave me a tip: stackoverflow.com/questions/2… .

Well, Gradle Android plugin simply can’t build the debug version of dependent library modules. This is a well-known, old issue and this is not resolved yet.

It turns out that there is a historical reason for this. This thing is By Design, so we have to find a way around it. For example, the discussion below this question provides an idea:

android {
    publishNonDefault true
}

dependencies {
    releaseCompile project(path: ':yourLibrary', configuration: 'release')
    debugCompile project(path: ':yourLibrary', configuration: 'debug')

    // This is also possible
    customCompile project(path: ':yourLibrary', configuration: 'custom')}Copy the code

However, this is not very elegant when you have multiple Library Module dependencies. Here’s my own idea:

android { ... buildTypes { release { debuggable isDebug() minifyEnabled ! isDebug() zipAlignEnabled ! isDebug() proguardFiles getDefaultProguardFile('proguard-android.txt'), '.. /tools/proguard.cfg'
            signingConfig signingConfigs.release
        }
    }
    
    ...
}

def isDebug() {
    if(gradle.startParameter.getTaskNames().size() = =0) { // for clean etc..
        return true
    }
    return gradle.startParameter.getTaskNames().get(0).contains("Debug")}Copy the code

We’ll just provide a buildType, which is the default release, and make the configuration within Type dynamic. This solution is suitable for cases where there are only two or three types and can be solved within the Library with a small amount of code.

Welcome to comment like, and follow my public accountEfficient Android Development.