在开发Android应用程序时,我们可以在
AndroidManifest.xml
中设置其
debug
属性,比如示例1
示例1
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div style="margin: 0px; padding: 0px;"><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="tag" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);"><application</span></div><div style="margin: 0px; padding: 0px;"><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">android:icon</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"@drawable/ic_launcher"</span></div><div style="margin: 0px; padding: 0px;"><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">android:label</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"@string/app_name"</span></div><div style="margin: 0px; padding: 0px;"><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">android:theme</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"@style/AppTheme"</span><span class="pln" style="margin: 0px; padding: 0px;"> </span></div><div style="margin: 0px; padding: 0px;"><span class="pln" style="margin: 0px; padding: 0px;"> </span><span class="atn" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">android:debuggable</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"true"</span><span class="tag" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">></span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>
只有android:debuggable="true"时我们才可以在手机上调试Android程序。
但是当我们没在
AndroidManifest.xml中设置其debug属性时:
使用Eclipse运行这种方式打包时其
debug属性为
true
,
使用Eclipse导出这种方式打包时其
debug属性为法false.
在使用ant打包时,其值就取决于ant的打包参数是release还是debug.
因此在
AndroidMainifest.xml
中最好
不
设置
android:debuggable
属性置,而是由打包方式来决定其值。。
当然对于测试人员还是应该检查该属性的,比如
使用aapt工具:
aapt list -v -a myfile.apk
这个命令将会打印和apk相关的所有详细信息,找到“android:debuggable",它的值分为:
0x0: debuggable false
0xffffffff: debugabble true
例如,在我的测试中,这一行的信息是:
A: android ebuggable(0x0101000f)=(type 0x12)0x0
这说明我的Release Build已经关闭了debuggable!
另外,还有一种测试方法:
使用android cts测试,http://source.android.com/compatibility/cts-intro.html
这是一种单元测试的方法,具体用到的类是android.permission.cts.DebuggableTest。
testNoDebuggable:如果是true,说明debuggable false;
testNoDebuggable:如果是false,说明debuggable true。
当然,我们也可以在Android应用程序中来判断当前应用是否处于debug状态来做一些操作,如
示例2。
示例2
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">static</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">boolean</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> isApkDebugable</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 0, 102);">Context</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> context</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">{</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">try</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">{</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="typ" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 0, 102);">ApplicationInfo</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> info</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> context</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;">getApplicationInfo</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">();</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">return</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;">info</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;">flags</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">&</span><span class="typ" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 0, 102);">ApplicationInfo</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;">FLAG_DEBUGGABLE</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">)!=</span><span class="lit" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 102, 102);">0</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">;</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">}</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">catch</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 0, 102);">Exception</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> e</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">{</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">}</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">return</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">false</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">;</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">}</span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>
另外,我们也可以在Android应用程序中来判断当前其他应用程序是否处于debug状态,如
示例3。
示例3
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">static</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">boolean</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> isApkDebugable</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 0, 102);">Context</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> context</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="typ" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 0, 102);">String</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> packageName</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">{</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">try</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">{</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="typ" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 0, 102);">PackageInfo</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> pkginfo </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> context</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;">getPackageManager</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;">getPackageInfo</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">(</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> packageName</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="lit" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 102, 102);">1</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">);</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">if</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;">pkginfo </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">!=</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">null</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">{</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="typ" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 0, 102);">ApplicationInfo</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> info</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> pkginfo</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;">applicationInfo</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">;</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">return</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;">info</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;">flags</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">&</span><span class="typ" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 0, 102);">ApplicationInfo</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;">FLAG_DEBUGGABLE</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">)!=</span><span class="lit" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 102, 102);">0</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">;</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">}</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">}</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">catch</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 0, 102);">Exception</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> e</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">{</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">}</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">return</span><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="kwd" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(0, 0, 136);">false</span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">;</span></div><div style="margin: 0px; padding: 0px; line-height: 22px;"><span class="pln" style="margin: 0px; padding: 0px; line-height: 25px;"> </span><span class="pun" style="margin: 0px; padding: 0px; line-height: 25px; color: rgb(102, 102, 0);">}</span></div>