Leakcanary: Add leakcanary to Android library project?

Created on 12 Jun 2015  路  3Comments  路  Source: square/leakcanary

This may be related to #9. Our setup is a multi-project Gradle build, where multiple Android application projects depend on a common Android library project (which we want to use leakcanary to verify leaks).

I was trying to add leakcanary to a library project when I realized that Gradle only builds _release_ variant of library projects, which is a known Gradle Android plugin issue, which means leakcanary-android-no-op would be built instead of leakcanary-android if we include leakcanary in our library project as follows:

dependencies {
    ...
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
}

Forcing Gradle Android plugin to publish library as 'debug' of course works, but that would affect our 'release' setup.

android {
    defaultPublishConfig "debug"
}

Another workaround would be to always compile leakcanary-android, and then exclude leakcanary logic based on some build type specific configuration during runtime, but that would defeat the purpose of having leakcanary-android-no-op.

dependencies {
    ...
    compile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
}
if (debug) {
    LeakCanary.install(this);
}

Have anyone encountered the same problem? Any other workarounds?

Most helpful comment

You can depend on a none default config of a library, ie.

app.gradle

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

All 3 comments

Did you try with Android Studio? in latest version of Android Studio, I see my library set to debug in the build variant panel.

You can depend on a none default config of a library, ie.

app.gradle

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

Damn, I missed that one. Thanks. The above configuration works, provided the library is configured with

android {
    publishNonDefault true
}
Was this page helpful?
0 / 5 - 0 ratings