16

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'
8
  • Anothe gradel project, meaning you have added the source of library as compile project(':somelibrary')? Commented Mar 1, 2017 at 12:51
  • yes exactly @AnuragSingh Commented Mar 1, 2017 at 12:59
  • 1
    In settings.gradle just use: include ':somelibrary' and you can debug just as other modules that are not library. Commented Mar 1, 2017 at 13:06
  • Not working.I have included the settings.gradle and updated the question. Commented Mar 1, 2017 at 13:19
  • this is custom library I have written, the source code is in the library module available and is added in the project. Commented Mar 1, 2017 at 13:27

4 Answers 4

7

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
Sign up to request clarification or add additional context in comments.

Comments

7

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

Comments

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.

5 Comments

and how to provide path-to-my-library ?
It's the relative path from your application folder
No chance,I have added another setting.gradle file inside app module folder and deleted the project-level setting.gradle, as you said.
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?
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.
1

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 ) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.