判断android是否是debug

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

 

posted @ 2018-01-09 17:35  贺长寿  阅读(4136)  评论(0编辑  收藏  举报