Android CI Github Action for Barcode Scanner

Android CI is a Github action very useful when developing Android applications. Let’s see how I used that for improving the Barcode Scanner application.

Android CI

Enabling the Android CI Github Action

When managing a Github repo, the web application suggests some workflows that can help with different tasks involving the repo. If the content of the repo is an Android application, Github is able to detect that and will suggest the Android CI workflow.

Github suggested workflows

Press the Configure button and customize the yml file, if needed: for the Barcode Scanner application I just had to set the Java version to 17.

Android CI yml file

The action is ready and by default it will be run after a commit is pushed to the main branch.

Fixing a few Barcode Scanner issues

Enabling the CI action revealed a few issues that did not prevent the app from building. These issues were detected also by Android Studio and highlighted when opening a specific file. However, I was not able to find a GUI element where those issues were all listed (probably my fault), while the github CI GUI provides that.

As an example, running the CI action at commit:

092b589 Update README.md

returned the following result:

> Task :app:lintDebug FAILED
Lint found 14 errors, 70 warnings.

To see this output go to Actions, click on the failed workflow and then click on the job build.

Action result

So, I have tried to fix at least the errors and in the following paragraphs you can find how.

DISCLAIMER: I’m not an expert Android app developer, so my solutions could be a bit hackish or also totally wrong. Feel free to let me know if there are better options (or directly submit a PR to the github repo).

Missing permission android.permission.ACCESS_FINE_LOCATION

The error reported is:

/home/runner/work/BarcodeScanner/BarcodeScanner/app/src/main/java/com/google/zxing/client/android/wifi/WifiConfigManager.java:200: Error: Missing permissions required by WifiManager.getConfiguredNetworks: android.permission.ACCESS_FINE_LOCATION [MissingPermission]
    Iterable<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();

This error is quite clear and the Android documentation helps a lot.

Basically, we need to:

  • Add the missing permissions
  • Add code to ask for runtime permissions and to check that they have been granted

The first part requires just two simple lines in the AndroidManifest.xml.

The second one is a bit trickier, since:

  • A negated permission should not asked anymore by the app: this required storing the user decision inside the shared preferences.
  • The CaptureActivity flow should be properly managed to consider the result of the location permission request pop-up: to manage this, I’ve thought to issue again the result decoding in the onResume method after the permission has been granted.

The code is available at commit “Manage ACCESS_FINE_LOCATION runtime permission ask” and can be tested with QR codes created at this site.

Replace getColumnIndex with getColumnIndexOrThrow

/home/runner/work/BarcodeScanner/BarcodeScanner/app/src/main/java/com/google/zxing/client/android/share/ShareActivity.java:180: Error: Value must be ≥ 0 but getColumnIndex can be -1 [Range]
      id = cursor.getString(cursor.getColumnIndex(BaseColumns._ID));

The description is crystal clear: since we trust the original developers, we can suppose that the involved columns exist, so we replace getColumnIndex with getColumnIndexOrThrow.

The code is available at commit “Replace getColumnIndex with getColumnIndexOrThrow“.

Replace android:showAsAction with app:showAsAction

/home/runner/work/BarcodeScanner/BarcodeScanner/app/src/main/res/menu/capture.xml:22: Error: Should use app:showAsAction with the appcompat library with xmlns:app="http://schemas.android.com/apk/res-auto" [AppCompatResource]
          android:showAsAction="withText|ifRoom"/>

Not much to add, the code is available at commit “Replace android:showAsAction with app:showAsAction.

Missing translation

/home/runner/work/BarcodeScanner/BarcodeScanner/app/src/main/res/values/strings.xml:115: Error: "preferences_history_summary" is not translated in "no" (Norwegian) or "nb" (Norwegian Bokmål) [MissingTranslation]
  <string name="preferences_history_summary">Store your scans in History</string>

Unfortunately, I don’t know the Norwegian, so the best fix is to mark that string not translatable.

This last commit makes the CI pass. Overall, a project can really take advantage of the clear hints and checks performed by the Android CI github action. Related to the Barcode Scanner app, there is still to be fixed, though, the usage of the deprecated API.