Farhad
Farhad

Reputation: 12976

How to debug android library module in Android Studio?

I have an Android Studio project which contains a library module, which is added as another gradle project to it. I would like to debug the library code and set breakpoints on it.

What gradle settings should I use, if I want to debug a library module while running the app on emulator or real device ?


Update 1

this is the settings.gradle file :

include ':app'
include':my-library'

Upvotes: 16

Views: 22126

Answers (4)

Hisham Bakr
Hisham Bakr

Reputation: 559

I faced this issue long time ago. some gradle versions will switch your library to release mode even if you set it to debug. the fix is either update gradle to latest. if it didnt fix it. inside your library, don't use:

if BuildConfig.DEBUG

instead use :

boolean isDebuggable = ( 0 != ( getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) );

Upvotes: 1

ossamacpp
ossamacpp

Reputation: 686

I set both library module Debug and Release build type to debuggable

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        debuggable true
        jniDebuggable true
    }
    debug {
        debuggable true
        jniDebuggable true
        minifyEnabled false
    }
}

Upvotes: 7

Farhad
Farhad

Reputation: 12976

After a few days struggling I found the right configuration for being able to debug the library module :

1- Create a project which consists of two modules, the app and the library-module

2- Add direct module dependency to app , from the library-module. This is what the app's build.gradle :

compile project(':library-module')

3- Remove any automatic signing configuration added in the app build.gradle

4- Remove these lines from both the app and library-module

minifyEnabled true
shrinkResources true

Upvotes: 7

Myon
Myon

Reputation: 957

I'm using this setup to debug my libraries:

|- myApplication
|  |- settigs.gradle
|  |- build.gradle
|     ...
|- myLibrary
   |- build.gradle
      ...

add to settings.gradle:

include ':myLibrary'
project(':myLibrary').projectDir = new File(settingsDir, '../myLibrary')

add to build.gradle (of your app)

compile project(':myLibrary')

Your library gets simply included and you can debug and set breakpoints just like in the app.

Upvotes: 3

Related Questions