How to debug android library module in Android Studio?

ε祈祈猫儿з 提交于 2021-02-06 10:43:05

问题


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'

回答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



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



回答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.




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


来源:https://stackoverflow.com/questions/42532062/how-to-debug-android-library-module-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!