Android的调试技巧包括熟练使用Android中的日志API,自定义Android日志框架,通过gradle配置调试日志,Android Studio的调试技巧等等。
android 开发常用日志类之Log类:android.util.Log.java
其中tag和message分别是两个String值.从android开发帮助文档中来看,tag和message的定义分别是: tag:Used to identify the source of a log message. Itusually identifies the class or activity where the log call occurs. msg:The message you would like logged. 可看出tag用来标记log消息的源头的。而message则是这条log的内容。
通过这种android自身Log API打印日志的方式,是最常见的一种打印日志的,调试代码的方式,基本上所有的项目中你可能都会遇到这种日志代码。
//java日志类
String TAG = "MainActivit"; System.out.println("普通日志Info"); System.err.println("错误日志Info"); //android 日志类 Log.e("TAG","错误信息"); Log.w("TAG","警告信息"); Log.i("TAG","普通信息"); Log.d("TAG","调试信息"); Log.d("TAG","无用信息"); //经常被程序猿当作注释用
可以直接在ide工具中查看日志。如androidStuido或eclipse
可以在ddms工具中查看日志。
android 开发常用日志类之MLog类:
github地址:https://github.com/yipianfengye/MLog (个人建议:使用时下载源码方便根据自己需要进一步调整)
前面我们发现使用Android原生的Log API打印的日志格式比较简陋,那么可不可以定制化的显示一些友好型的日志信息呢?答案是肯定的,github地址作者介绍了一个自定义的日志框架MLog
作者是这么描述的:
MLog
这是一个建议的日志框架,主要参考了logger日志框架的实现方式;
-
在build.gradle中引用MLog框架
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' compile 'com.yipianfengye:MLog:1.0.0' }
-
在自定义的Application的onCreate方法中初始化MLog框架
/**
* Created by liuchao on 2016/1/26.
*/
public class MApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
MLog.init(true);
}
}
- 在代码中使用MLog框架
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MLog.i("测试MLog框架!!!");
}
});
我的调用如下:
public class MainActivity extends AppCompatActivity { private String TAG ="MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MLog.v(TAG, "this is a verbose log!!!"); MLog.d(TAG, "this is a debug log!!!"); MLog.i(TAG, "this is a info log!!!"); MLog.w(TAG, "this is a warning log!!!"); MLog.e(TAG, "this is a error log!!!"); } }
我的app类:
package czg.myapplication; import android.app.Application; import com.example.aaron.library.MLog; /** * Created by Administrator on 2016/7/19. */ public class CzgApp extends Application { @Override public void onCreate() { super.onCreate(); MLog.init(true); } }
AndroidManifest.xml中CzgApp的注册情况如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="czg.myapplication"> <application android:name=".CzgApp" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>在AndroidMoitor中打印的日志格式和内容:
这样我们在点击日志信息的时候就能够跳转到这条日志的代码打印位置,是不是很方便?而且如果觉得日志格式不是很好的话,还可以定制化展示奥。
作者提供图如下:(由于我没有这方面的需要,也就没有进一步研究,如果有需要耍酷的同学自己去研究吧)
buildTypes { debug { // 显示Log buildConfigField "boolean", "LOG_DEBUG", "true" //混淆 minifyEnabled false //Zipalign优化 zipAlignEnabled true // 移除无用的resource文件 shrinkResources true //加载默认混淆配置文件 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' //签名 signingConfig signingConfigs.debug } release { // 不显示Log buildConfigField "boolean", "LOG_DEBUG", "false" //混淆 minifyEnabled true //Zipalign优化 zipAlignEnabled true // 移除无用的resource文件 shrinkResources true //加载默认混淆配置文件 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' //签名 signingConfig signingConfigs.relealse } }
参考文献:Android实用调试技巧
作者:头条号 / 达人科技
链接:http://toutiao.com/a6308879550420123905/