1.使用BuildConfig.DEBUG,這個在住modul里面是有效的,但是在有依賴庫里面使用就會一直返回false,可以通過下面的方法解決:在library的build.gradle中添加以下代碼
gradle.startParameter.getTaskNames().each { task -> println("task: " + task) //library里 BuildConfig.DEBUG默認一直是flase;所以需要自定義 if(task.contains("Debug")){ android{ defaultPublishConfig "debug" } }else if(task.contains("Release")){ android{ defaultPublishConfig "release" } } }
2.不需要使用BuildConfig.DEBUG
public boolean isDebug(Context context){ boolean isDebug = context.getApplicationInfo()!=null&& (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)!=0; return isDebug; }