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 two kinds of Android-specific issues: Android Lint issues and issues with the R class.

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 7, 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 of the potential issues in your project, including those that are less serious. 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.

Once 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.9).

Figure 5.9  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 on Using left/right instead of start/end attributes to learn about this particular warning (Figure 5.10).

Figure 5.10  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 17.)

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 on the offending file, activity_main.xml, to see the snippet of code with the problem (Figure 5.11).

Figure 5.11  Viewing the code that caused the warning

Viewing the code that caused the warning

Double-click on the warning description that appears under the filename. This will open res/layout/land/activity_main.xml in the editor tool window and place the cursor on the line causing the warning:

    <Button
            android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:text="@string/next_button"
            android:drawableEnd="@drawable/arrow_right"
            android:drawablePadding="4dp"/>

The line Lint is concerned about sets the gravity of the NEXT button so that it appears in the bottom-right corner of the landscape layout. To fix the warning, change the value bottom|right to bottom|end. This way, the button will be positioned on the bottom left on a device set to a language that reads right to left instead of left to right.

Listing 5.7  Addressing bidirectional text warning (res/layout/land/activity_main.xml)

...
<Button
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_gravity="bottom|end"
        android:text="@string/next_button"
        android:drawableEnd="@drawable/arrow_right"
        android:drawablePadding="4dp"/>
...

Rerun Lint to confirm that the bidirectional text issue you just fixed is no longer listed in the Lint results. To preview how the layout will look on devices with right to left locales, open the Design tab in the editor tool window. Change the selection in the Locale for Preview dropdown from Default (en-us) to Preview Right to Left (Figure 5.12). If you do not see the Locale for Preview dropdown, click >> at the top of the preview pane to expand preview options.

Figure 5.12  Previewing left to right and right to left

Previewing left to right and right to left

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 (as in this case). 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 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.

Issues with the R class

You are familiar with build errors that occur when you reference resources before adding them or delete resources that other files refer to. Usually, resaving the files once the resource is added or the references are removed will cause Android Studio to rebuild without any fuss.

Sometimes, however, 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

If your R.java file was not generated for the last build, you will see errors in your project wherever you reference a resource. 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 cause R.java to regenerate.

Clean your project

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 file, 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.

If you are still having problems with resources (or 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
3.128.198.21