How to debug android library module in Android Studio?

15,131

Solution 1

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

Solution 2

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
    }
}

Solution 3

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.

Solution 4

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 ) );
Share:
15,131
Farhad Faghihi
Author by

Farhad Faghihi

Updated on June 12, 2022

Comments

  • Farhad Faghihi
    Farhad Faghihi almost 2 years

    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'
    
  • Farhad Faghihi
    Farhad Faghihi about 7 years
    and how to provide path-to-my-library ?
  • Myon
    Myon about 7 years
    It's the relative path from your application folder
  • Farhad Faghihi
    Farhad Faghihi about 7 years
    No chance,I have added another setting.gradle file inside app module folder and deleted the project-level setting.gradle, as you said.
  • Tom Rutchik
    Tom Rutchik about 5 years
    You can use the absolute path of the external library project. Using this technique, I've been able to see the code in my external library, set breakpoints, view the class and it member fields and step through the code. What I'm not able to do is to see the values of local variables in a member function. Besides promoting local variable to class member fields does anyone have a solution on how to see the values of local variables when debugging?
  • Tom Rutchik
    Tom Rutchik about 5 years
    Silly me, yes you can see the value of local variables, but you need to be sure you're referencing a debuggable library. I was linking in a release version of the external library. You might also have to make sure that you don't have minifyEnabled set to true.