debugging android application

Upload: vinay-kumar

Post on 07-Apr-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Debugging Android Application

    1/86

    2008 Haim Michael

    Debugging

  • 8/6/2019 Debugging Android Application

    2/86

    2008 Haim Michael

    Introduction

    The Eclipse IDE and the android SDK provide a rich set of

    tools that assist us with developing our application for the

    android platform and with debugging it.

  • 8/6/2019 Debugging Android Application

    3/86

    2008 Haim Michael

    Eclipse Java Editor

    Developing for the android platform we can enjoy the same

    Eclipse IDE features we know when using the Eclipse IDE

    for other platforms.

  • 8/6/2019 Debugging Android Application

    4/86

    2008 Haim Michael

    Eclipse Java Editor

  • 8/6/2019 Debugging Android Application

    5/86

    2008 Haim Michael

    Eclipse Java Editor

    When using the Eclipse IDE we get error messages on the

    fly concurrently with coding our program.

    We even get suggestions for fixing our code.

  • 8/6/2019 Debugging Android Application

    6/86

    2008 Haim Michael

    Eclipse Java Editor

  • 8/6/2019 Debugging Android Application

    7/86

    2008 Haim Michael

    Eclipse Java Editor

    There are three available ways for toggling a break-point.

    We can select the line and select from the top menu

    Run->Toggle Break Point

    We can double click in the left margin of the editor at the line

    we want to toggle.

    We can use the keyboard pressing Ctrl+shift+B.

  • 8/6/2019 Debugging Android Application

    8/86

    2008 Haim Michael

    Eclipse Java Editor

  • 8/6/2019 Debugging Android Application

    9/86

    2008 Haim Michael

    Eclipse Java Debugger

    There are three available ways for toggling a break-point.

    We can select the line and select from the top menu

    Run->Toggle Break Point.

    We can double click in the left margin of the editor at the line

    we want to toggle.

    We can use the keyboard pressing Ctrl+shift+B.

    We start debugging by selecting from our top menu the

    'Debug As' option.

  • 8/6/2019 Debugging Android Application

    10/86

    2008 Haim Michael

    Eclipse Java Debugger

  • 8/6/2019 Debugging Android Application

    11/86

    2008 Haim Michael

    Eclipse Java Debugger

  • 8/6/2019 Debugging Android Application

    12/86

    2008 Haim Michael

    Logcat

    Logcat is a general purpose logging facility. The Logcat

    pane is available as part of the debugger perspective. The

    Logcat pane includes a log of messages.

  • 8/6/2019 Debugging Android Application

    13/86

    2008 Haim Michael

    Logcat

  • 8/6/2019 Debugging Android Application

    14/86

    2008 Haim Michael

    Logcat

    Each one of the messages has a different entry priority. The

    Log class includes separated static methods for each one of

    the available entry priorities.

    static int d(String tag, String msg, Throwable tr)

    Send a DEBUG log message and log the exception.

    static int d(String tag, String msg)

    Send a DEBUG log message.

  • 8/6/2019 Debugging Android Application

    15/86

    2008 Haim Michael

    Logcat

    static int e(String tag, String msg)

    Send an ERROR log message.

    static int e(String tag, String msg, Throwable tr)

    Send a ERROR log message and log the exception.

    static int i(String tag, String msg, Throwable tr)

    Send a INFO log message and log the exception.

    static int i(String tag, String msg)

    Send an INFO log message.

  • 8/6/2019 Debugging Android Application

    16/86

    2008 Haim Michael

    Logcat

    static int v(String tag, String msg, Throwable tr)

    Send a VERBOSE log message and log the exception.

    static int v(String tag, String msg)

    Send a VERBOSE log message.

    static int w(String tag, String msg)

    Send a WARN log message.

    static int w(String tag, Throwable tr)

    Send a WARN log message.

  • 8/6/2019 Debugging Android Application

    17/86

    2008 Haim Michael

    Logcat

    static int w(String tag, String msg, Throwable tr)

    Send a WARN log message and log the exception.

  • 8/6/2019 Debugging Android Application

    18/86

    2008 Haim Michael

    Sample

    publicclass LoggerDemo extends Activity {

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);setContentView(R.layout.main);

    Button bt = (Button)findViewById(R.id.Button01);

    bt.setOnClickListener(new OnClickListener()

    {@Override

    publicvoid onClick(View v)

    {

    int sum = 0;

    for(int i=1; i

  • 8/6/2019 Debugging Android Application

    19/86

    2008 Haim Michael

    Sample

  • 8/6/2019 Debugging Android Application

    20/86

    2008 Haim Michael

    Sample

  • 8/6/2019 Debugging Android Application

    21/86

    2008 Haim Michael

    Android Debug Bridge

    The android debug bridge (adb) is a special command line

    tool the android platform comes with.

    Using the android debug bridge we can remotely control the

    device or the emulator we are working with.

    We can invoke the android debug bridge client from the

    command line prompt.

  • 8/6/2019 Debugging Android Application

    22/86

    2008 Haim Michael

    Android Debug Bridge

    Typing 'adb logcat' in the command line will get us the

    detailed logcat messages.

  • 8/6/2019 Debugging Android Application

    23/86

    2008 Haim Michael

    Android Debug Bridge

  • 8/6/2019 Debugging Android Application

    24/86

    2008 Haim Michael

    Android Debug Bridge

    Typing 'adb' in the command line will get us a detailed list of

    all available adb's commands.

  • 8/6/2019 Debugging Android Application

    25/86

    2008 Haim Michael

    Android Debug Bridge

  • 8/6/2019 Debugging Android Application

    26/86

    2008 Haim Michael

    Android Debug Bridge

  • 8/6/2019 Debugging Android Application

    27/86

    2008 Haim Michael

    Delvik Debug Monitor Service

    Once the android SDK plug-in for the Eclipse IDE is

    installed, we can start using the Delvik Debug Monitor

    Service (DDMS).

    The DDMS perspective provides a window based interface

    for android specific debug information on the emulator (or

    the real handset).

  • 8/6/2019 Debugging Android Application

    28/86

    2008 Haim Michael

    Delvik Debug Monitor Service

  • 8/6/2019 Debugging Android Application

    29/86

    2008 Haim Michael

    Delvik Debug Monitor Service

  • 8/6/2019 Debugging Android Application

    30/86

    2008 Haim Michael

    Traceview

    The Traceview utility allows tracking the exact methods that

    are been called as well as the exact time each one of these

    methods executions takes.

  • 8/6/2019 Debugging Android Application

    31/86

    2008 Haim Michael

    Traceview

    The Traceview utility includes two parts. The first is a small

    utility that creates a log file that includes detailed data about

    each and every method invocation. The second is a

    graphics based application you can execute passing over

    the log file and get a detailed graphics representation of all

    methods calls.

  • 8/6/2019 Debugging Android Application

    32/86

    2008 Haim Michael

    Traceview

    In order to use the Traceview utility we should first import

    the android.os package and add to our code the following

    lines.

    ...

    Debug.startMethodTracing(mytrace);

    ...

    ...

    Debug.stopMethodTracing();

    ...

  • 8/6/2019 Debugging Android Application

    33/86

    2008 Haim Michael

    Traceview

    The Tracebiew utility will fill in the log file on the android SD

    card with data generated during the code execution.

    If we work with the emulator then we should create an AVD

    with a virtual SD card.

  • 8/6/2019 Debugging Android Application

    34/86

    2008 Haim Michael

    Traceview

    The first step should be creating the file on the SD card:

    mksdcard 10M traco

  • 8/6/2019 Debugging Android Application

    35/86

    2008 Haim Michael

    Traceview

  • 8/6/2019 Debugging Android Application

    36/86

    2008 Haim Michael

    Traceview

    The second step should be creating the AVD we want to use

    specifying the size of the requested SD card.

  • 8/6/2019 Debugging Android Application

    37/86

    2008 Haim Michael

    Traceview

  • 8/6/2019 Debugging Android Application

    38/86

    2008 Haim Michael

    Traceview

    The third step should be telling the emulator we want to use

    a virtual SC card. In the Eclipse IDE you should choose

    Window > Preferences > Android > Launch. Within the box

    for the emulator options you should add the following code

    -sdcard ./traco

    Make sure to specify the complete path to the file, so the

    emulator can always find it.

  • 8/6/2019 Debugging Android Application

    39/86

    2008 Haim Michael

    Traceview

  • 8/6/2019 Debugging Android Application

    40/86

    2008 Haim Michael

    Traceview

    The fourth step would be executing our code. Our code

    should include the call to start tracing the methods.

    Debug.startMethodTracing("traco"); //traco is the filename

    In addition, our code should include a call to stop it.

    Debug.stopMethodTracing();

    When the application calls startMethodTracing(), the

    system creates a file called ___.trace (e.g. traco.trace). This

    file contains the binary method trace data and its mapping

    table with thread and method names.

  • 8/6/2019 Debugging Android Application

    41/86

    2008 Haim Michael

    Traceview

    When using the Traceview utility the execution times are

    significantly slower. Therefore, we shouldn't refer these

    times as the accurate one. We can only compare them with

    each other.

    Once the execution completes we can get the log data

    displayed in a graphics way.adb pull /sdcard/traco.trace /tmp

    traceview /tmp/traco

  • 8/6/2019 Debugging Android Application

    42/86

    2008 Haim Michael

    Traceview

  • 8/6/2019 Debugging Android Application

    43/86

    2008 Haim Michael

    Traceview

  • 8/6/2019 Debugging Android Application

    44/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 1

    Debugging

  • 8/6/2019 Debugging Android Application

    45/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 2

    Introduction

    The Eclipse IDE and the android SDK provide a rich set oftools that assist us with developing our application for the

    android platform and with debugging it.

  • 8/6/2019 Debugging Android Application

    46/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 3

    Eclipse Java Editor

    Developing for the android platform we can enjoy the sameEclipse IDE features we know when using the Eclipse IDE

    for other platforms.

  • 8/6/2019 Debugging Android Application

    47/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 4

    Eclipse Java Editor

  • 8/6/2019 Debugging Android Application

    48/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 5

    Eclipse Java Editor

    When using the Eclipse IDE we get error messages on thefly concurrently with coding our program.

    We even get suggestions for fixing our code.

  • 8/6/2019 Debugging Android Application

    49/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 6

    Eclipse Java Editor

  • 8/6/2019 Debugging Android Application

    50/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 7

    Eclipse Java Editor

    There are three available ways for toggling a break-point.

    We can select the line and select from the top menu

    Run->Toggle Break Point

    We can double click in the left margin of the editor at the line

    we want to toggle.

    We can use the keyboard pressing Ctrl+shift+B.

  • 8/6/2019 Debugging Android Application

    51/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 8

    Eclipse Java Editor

  • 8/6/2019 Debugging Android Application

    52/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 9

    Eclipse Java Debugger

    There are three available ways for toggling a break-point.

    We can select the line and select from the top menu

    Run->Toggle Break Point.

    We can double click in the left margin of the editor at the line

    we want to toggle.

    We can use the keyboard pressing Ctrl+shift+B.

    We start debugging by selecting from our top menu the

    'Debug As' option.

  • 8/6/2019 Debugging Android Application

    53/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 10

    Eclipse Java Debugger

  • 8/6/2019 Debugging Android Application

    54/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 11

    Eclipse Java Debugger

  • 8/6/2019 Debugging Android Application

    55/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 12

    Logcat

    Logcat is a general purpose logging facility. The Logcatpane is available as part of the debugger perspective. The

    Logcat pane includes a log of messages.

  • 8/6/2019 Debugging Android Application

    56/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 13

    Logcat

  • 8/6/2019 Debugging Android Application

    57/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 14

    Logcat

    Each one of the messages has a different entry priority. TheLog class includes separated static methods for each one of

    the available entry priorities.

    static int d(String tag, String msg, Throwable tr)

    Send a DEBUG log message and log the exception.

    static int d(String tag, String msg)

    Send a DEBUG log message.

  • 8/6/2019 Debugging Android Application

    58/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 15

    Logcat

    static int e(String tag, String msg)

    Send an ERROR log message.

    static int e(String tag, String msg, Throwable tr)

    Send a ERROR log message and log the exception.

    static int i(String tag, String msg, Throwable tr)

    Send a INFO log message and log the exception.

    static int i(String tag, String msg)

    Send an INFO log message.

  • 8/6/2019 Debugging Android Application

    59/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 16

    Logcat

    static int v(String tag, String msg, Throwable tr)

    Send a VERBOSE log message and log the exception.

    static int v(String tag, String msg)

    Send a VERBOSE log message.

    static int w(String tag, String msg)

    Send a WARN log message.

    static int w(String tag, Throwable tr)

    Send a WARN log message.

  • 8/6/2019 Debugging Android Application

    60/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 17

    Logcat

    static int w(String tag, String msg, Throwable tr)

    Send a WARN log message and log the exception.

  • 8/6/2019 Debugging Android Application

    61/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 18

    Sample

    publicclass LoggerDemo extends Activity {

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    Button bt = (Button)findViewById(R.id.Button01);

    bt.setOnClickListener(new OnClickListener()

    {

    @Override

    publicvoid onClick(View v)

    {

    int sum = 0;

    for(int i=1; i

  • 8/6/2019 Debugging Android Application

    62/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 19

    Sample

  • 8/6/2019 Debugging Android Application

    63/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 20

    Sample

  • 8/6/2019 Debugging Android Application

    64/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 21

    Android Debug Bridge

    The android debug bridge (adb) is a special command linetool the android platform comes with.

    Using the android debug bridge we can remotely control the

    device or the emulator we are working with.

    We can invoke the android debug bridge client from the

    command line prompt.

    The server runs in the background. The server communicates either with the

    emulator or the handset it self. The communication itself is carried out using the

    TCP/IP protocol.

  • 8/6/2019 Debugging Android Application

    65/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 22

    Android Debug Bridge

    Typing 'adb logcat' in the command line will get us thedetailed logcat messages.

  • 8/6/2019 Debugging Android Application

    66/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 23

    Android Debug Bridge

  • 8/6/2019 Debugging Android Application

    67/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 24

    Android Debug Bridge

    Typing 'adb' in the command line will get us a detailed list ofall available adb's commands.

  • 8/6/2019 Debugging Android Application

    68/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 25

    Android Debug Bridge

  • 8/6/2019 Debugging Android Application

    69/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 26

    Android Debug Bridge

  • 8/6/2019 Debugging Android Application

    70/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 27

    Delvik Debug Monitor Service

    Once the android SDK plug-in for the Eclipse IDE isinstalled, we can start using the Delvik Debug Monitor

    Service (DDMS).

    The DDMS perspective provides a window based interface

    for android specific debug information on the emulator (or

    the real handset).

  • 8/6/2019 Debugging Android Application

    71/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 28

    Delvik Debug Monitor Service

  • 8/6/2019 Debugging Android Application

    72/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 29

    Delvik Debug Monitor Service

  • 8/6/2019 Debugging Android Application

    73/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 30

    Traceview

    The Traceview utility allows tracking the exact methods thatare been called as well as the exact time each one of these

    methods executions takes.

  • 8/6/2019 Debugging Android Application

    74/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 31

    Traceview

    The Traceview utility includes two parts. The first is a smallutility that creates a log file that includes detailed data about

    each and every method invocation. The second is a

    graphics based application you can execute passing over

    the log file and get a detailed graphics representation of all

    methods calls.

  • 8/6/2019 Debugging Android Application

    75/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 32

    Traceview

    In order to use the Traceview utility we should first importthe android.os package and add to our code the following

    lines.

    ...

    Debug.startMethodTracing(mytrace);

    ...

    ...

    Debug.stopMethodTracing();

    ...

  • 8/6/2019 Debugging Android Application

    76/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 33

    Traceview

    The Tracebiew utility will fill in the log file on the android SDcard with data generated during the code execution.

    If we work with the emulator then we should create an AVD

    with a virtual SD card.

  • 8/6/2019 Debugging Android Application

    77/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 34

    Traceview

    The first step should be creating the file on the SD card:mksdcard 10M traco

  • 8/6/2019 Debugging Android Application

    78/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 35

    Traceview

  • 8/6/2019 Debugging Android Application

    79/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 36

    Traceview

    The second step should be creating the AVD we want to usespecifying the size of the requested SD card.

  • 8/6/2019 Debugging Android Application

    80/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 37

    Traceview

  • 8/6/2019 Debugging Android Application

    81/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 38

    Traceview

    The third step should be telling the emulator we want to usea virtual SC card. In the Eclipse IDE you should choose

    Window > Preferences > Android > Launch. Within the box

    for the emulator options you should add the following code

    -sdcard ./traco

    Make sure to specify the complete path to the file, so the

    emulator can always find it.

  • 8/6/2019 Debugging Android Application

    82/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 39

    Traceview

  • 8/6/2019 Debugging Android Application

    83/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 40

    Traceview

    The fourth step would be executing our code. Our codeshould include the call to start tracing the methods.

    Debug.startMethodTracing("traco"); //traco is the filename

    In addition, our code should include a call to stop it.

    Debug.stopMethodTracing();

    When the application calls startMethodTracing(), the

    system creates a file called ___.trace (e.g. traco.trace). This

    file contains the binary method trace data and its mapping

    table with thread and method names.

  • 8/6/2019 Debugging Android Application

    84/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 41

    Traceview

    When using the Traceview utility the execution times aresignificantly slower. Therefore, we shouldn't refer these

    times as the accurate one. We can only compare them with

    each other.

    Once the execution completes we can get the log data

    displayed in a graphics way.

    adb pull /sdcard/traco.trace /tmp

    traceview /tmp/traco

  • 8/6/2019 Debugging Android Application

    85/86

    008 Haim Michael 10

    008 Haim Michael

    10/11/10 2008 Haim Michael 42

    Traceview

  • 8/6/2019 Debugging Android Application

    86/86

    008 Haim Michael 10

    10/11/10 2008 Haim Michael 43

    Traceview