Android-Specific Debugging

Most Android debugging is just like Kotlin debugging. However, you will sometimes run into issues with Android-specific parts, such as resources, that the Kotlin compiler knows nothing about. In this section, you will learn about Android Lint and issues with the build system.

Using Android Lint

Android Lint (or just “Lint”) is a static analyzer for Android code. A static analyzer is a program that examines your code to find defects without running it. Lint uses its knowledge of the Android frameworks to look deeper into your code and find problems that the compiler cannot. In many cases, Lint’s advice is worth taking.

In Chapter 8, you will see Lint warn you about compatibility problems. Lint can also perform type-checking for objects that are defined in XML.

You can manually run Lint to see all the potential issues in your project, including those that are less serious. In fact, let’s add a small issue to the project. Suppose you did not like the question text being centered, so you decided to left-align it. Open activity_main.xml and make that change.

Listing 5.6  Shifting the text left (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ...>

    <TextView
        android:id="@+id/question_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:gravity="left"
        android:padding="24dp"
        tools:text="@string/question_australia"/>

    ...
</LinearLayout>

Once you have made that change, select AnalyzeInspect Code... from the menu bar. You will be asked which parts of your project you would like to inspect. Choose Whole project and click OK. Android Studio will run Android Lint as well as a few other static analyzers on your code, such as spelling and Kotlin checks.

When the scan is complete, you will see categories of potential issues in the inspection tool window. Expand the Android and Lint categories to see Lint’s information about your project (Figure 5.10).

Figure 5.10  Lint warnings

Lint warnings

(Do not be concerned if you see a different number of Lint warnings. The Android toolchain is constantly evolving, and new checks may have been added to Lint, new restrictions may have been added to the Android framework, and newer versions of tools and dependencies may have become available.)

Expand Internationalization and then, under it, expand Bidirectional Text to see more detailed information on this issue in your project. Click Using left/right instead of start/end attributes to learn about this particular warning (Figure 5.11).

Figure 5.11  Lint warning description

Lint warning description

Lint is warning you that using right and left values for layout attributes could be problematic if your app is used on a device set to a language that reads from right to left instead of left to right. (You will learn about making your app ready for international use in Chapter 18.)

Dig further to see which file and line or lines of code caused the warning. Expand Using left/right instead of start/end attributes. Click the offending file, activity_main.xml, to see the snippet of code with the problem (Figure 5.12).

Figure 5.12  Viewing the code that caused the warning

Viewing the code that caused the warning

Double-click the warning description that appears under the filename. This will open activity_main.xml in the editor tool window and place the cursor on the line causing the warning (which, not coincidentally, is the change you just made).

Undo the change you made and rerun Lint to confirm that the bidirectional text issue you just fixed is no longer listed in the Lint results. For the most part, your app will execute just fine even if you do not address the things Lint warns you about. Often, though, addressing Lint warnings can help prevent problems in the future or make your users’ experience better.

We recommend you take all Lint warnings seriously, even if you ultimately decide not to address them. Otherwise you could get used to ignoring Lint and miss a serious problem.

The Lint tool provides detailed information about each issue it finds and provides suggestions for how to address it. We leave it to you as an exercise to review the other issues Lint found in GeoQuiz. You can ignore the issues, fix them as Lint recommends, or use the Suppress button in the problem description pane to suppress the warnings in the future. For the remainder of the GeoQuiz chapters, we will assume you left the remaining Lint issues unaddressed.

Build issues

Everyone will eventually make a mistake while coding: a forgotten piece of punctuation here, a typo there. It is very common to have a build error happen while you are trying to run your app. Sometimes these build errors will persist or appear seemingly out of nowhere. If this happens to you, here are some things you can try:

Recheck the validity of the XML in your resource files

Gradle does a good job of surfacing errors to developers in an actionable way. However, there are times when your app will not compile but Gradle gives you nothing to work with. Often, this is caused by a typo in one of your XML files. Layout XML is not always validated, so typos in these files may not be pointedly brought to your attention. Finding the typo and resaving the file should fix the issue.

Clean your build

Select BuildClean Project. Android Studio will rebuild the project from scratch, which often results in an error-free build. We can all use a deep clean every now and then.

Sync your project with Gradle

If you make changes to your build.gradle files, you will need to sync those changes to update your project’s build settings. Select FileSync Project with Gradle Files. Android Studio will rebuild the project from scratch with the correct project settings, which can help to resolve issues after changing your Gradle configuration.

Run Android Lint

Pay close attention to the warnings from Lint. With this tool, you will often discover unexpected issues.

Clean your project

If you have gotten this far down the debugging trail, things are not good. On very rare occasions, clearing out the caches that Android Studio uses could help solve your problem. Select FileInvalidate Caches/Restart.... Android Studio will perform some maintenance on the project and restart itself when it is done.

If you are still having problems with resources (or are having different problems), give the error messages and your layout files a fresh look. It is easy to overlook mistakes in the heat of the moment. Check out any Lint errors and warnings as well. A cool-headed reconsideration of the error messages may turn up a bug or typo.

Finally, if you are stuck or having other issues with Android Studio, check the archives at stackoverflow.com or visit the forum for this book at forums.bignerdranch.com.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset