Skip to content Skip to sidebar Skip to footer

Workaround To Link A Shared Library In Debug Mode With Android Studio (v2+)

Long ago, a Google Code ticket was opened as the Gradle plugin does not propagate whether you are doing a debug build or a release build to a dependent android library: DESCRIPTIO

Solution 1:

For this to work, the twobuild.gradle files must not be modified at the same time before syncing once for all. I had to follow the following steps:

  • Step 1: modify the lib's build.gradle, exactly as Kane said:

    // "android" section:
    defaultPublishConfig 'release' 
    publishNonDefault true 
    productFlavors {
        library {
            /* This strange empty flavour is actually needed 
               for the step 3 to be successful               */
        } 
    }
    
  • Step 2: clean/rebuild

  • Step 3: modify the app's build.gradle, also as Kane said:

    dependencies {
        debugCompileproject(path: ':custom_lib', configuration: "libraryDebug")
        releaseCompileproject(path: ':custom_lib', configuration: "libraryRelease") 
    }
    
  • Step 4: Gradle sync.

So it was just a matter of changing the library first, and then cleaning before modifying the app.

I checked the APK produced by the debug and release build modes, and each of them contains the proper variant of the lib, unlike before applying this workaround, so it does work (thanks Kane !).

Solution 2:

Shlublu's solution didn't work for me, no matter what steps I used.

After much digging, found out that one can define available configurations (at least using experimental gradle), and this in turn avoided having to clean/rebuild in hope of fixing all errors.

Using latest gradle with AS2.1, I was able to solve this issue with a very simple dependency content:

Lib's build.gradle (uses experimental gradle 0.7.0):

model {
  android {
...
    productFlavors {
       create('all') {
         ...
       }
    }
    publishNonDefault true
  }
}
configurations {
  allDebug
  allRelease
}

App's build.gradle (uses standard gradle 2.1.0):

debugCompileproject(path: ':lib', configuration: 'allDebug')
releaseCompileproject(path: ':lib', configuration: 'allRelease')

I'm pretty sure the productFlavors in the lib's gradle is not needed if using debug/release configurations only, like this:

Lib's build.gradle (uses experimental gradle 0.7.0):

model {
...
}
configurations {
  debug
  release
}

App's build.gradle (uses standard gradle 2.1.0):

debugCompileproject(path: ':lib', configuration: 'debug')
releaseCompileproject(path: ':lib', configuration: 'release')

Post a Comment for "Workaround To Link A Shared Library In Debug Mode With Android Studio (v2+)"