Android Barcode Scanning Capability - Bundling ML Kit

Hi,

I have been testing the Android Barcode Scanner and here is what I experienced:

  • There is no indication that the barcode scanner won’t work if the correct ML Kit library isn’t installed. It just returns an error.
  • Apparently Play Store downloads it with no indication to the user. In my case, it didn’t work the entire first day, but the next day it was suddenly able to scan. There was no notification that anything was downloaded or installed.
  • At first, it couldn’t scan anything and seemed slow. A few minutes later it started working as intended.

I think it would be an awful experience for my clients to have to go through this, not knowing when their app will be able to properly scan. The only thing I can do is put up a message box asking them to wait…

So I’m hoping there might be some way for me to include the ML Kit in my APK.

For Code Scanner documentation here https://developers.google.com/ml-kit/vision/barcode-scanning/code-scanner it says:

This API uses an unbundled library that must be downloaded before use. See this guide for more information.

But the guide they link to suggests that “Barcode Scanning” can be bundled at build time, while “Google Code Scanner” is unbundled.

Is there any way to set the Xojo Android Barcode Scanner to use the Barcode Scanning library and bundle it? Or is there some recommended guidance for handling the strange behavior by the Google Code Scanner while it awaits the library download? Any way to use this in an offline environment?

Thanks!

-Mike

From what I understand, Android actually “updates” on a schedule, usually when the user has their device plugged in and charging without use.

This is their way of remaining “cheap” on use of space. I was coding in Kotlin for a brief stint (and, for the record, I hate it with a passion) and Gradle would update ONLY as necessary, which means libraries are constantly in and out of use and are being swapped around periodically during updates.

I don’t know if there’s even a way to verify that the necessary library is installed.

I think this is why I’ve seen devices with instructions mandating that users turn them on to set up and then leave them for one whole day to “charge” and “allow for system updates to happen?”

I read the guide and it looks like Google Play has to get updated when the app triggers the need - I’m trying to figure out how to bundle it. So your devices needs to sit until Google Play gets updated, which by default should occur either in the background or when the device is plugged in and not moving for a few hours.


It looks like you will lose access to any newly improved functionality that is created after the point of time in which a library / model becomes bundled with your app, not sure if you want to lock a feature like that.

How to download models

When using the unbundled model option, you can specify how you want models to be downloaded to the device:

  • You can enable install-time model downloads by adding a declaration to your app’s AndroidManifest.xml file. For example, the code snippet below shows how to configure your app to automatically download the Barcode Scanning model after your app is installed from the Play Store:
<application ...>
      ...
      <meta-data
          android:name="com.google.mlkit.vision.DEPENDENCIES"
          android:value="barcode" >
      <!-- To use multiple models: android:value="barcode,model2,model3" -->
</application>
  • You can request explicit download through Google Play Services ModuleInstallClient API.
  • If you don’t enable install-time model downloads or request explicit download, the model will be downloaded the first time you run the feature. Until the download is completed, inference requests will fail.

How to update models

To update your models when using the bundled model or the dynamically downloaded models option:

  1. Update your app’s gradle file to use the latest ML Kit feature client library.
dependencies {
  implementation 'com.google.mlkit:barcode-scanning: 17.3.0' // The latest version number of the API
}
  1. Rebuild your app.

The above text came from ML Kit model installation paths on Android  |  Google for Developers and it looks like if you edit the app’s manifest file, you can trigger the download to occur, and then trigger updates.

I hope this helps!

1 Like

Yeah, that would explain why it started working overnight for me.

For context I have been developing apps for a plastics company for a few years, and these devices are used for 24-hours a day to scan material coming into the plant and moving into silos.

I have been using an old hybrid app technology but would LOVE to switch to Xojo.

I suppose, if they have extra devices, they could take each device out of rotation and let it sit overnight, but it’s an unexpected hassle that I don’t really want to offload onto them.

The above text came from [ML Kit model installation paths on Android | Google for Developers](https://developers.google.com/ml-kit/tips/installation-paths#download-models) and it looks like if you edit the app’s manifest file, you can trigger the download to occur, and then trigger updates.

I hope this helps!

Ah, yes, that could be exactly what I was looking for!

I will have to look through the docs to see how to modify the manifest for a Xojo app. And then if i can’t figure it out I might come crawling back here to ask. :slight_smile:

Thank you!

I’m just going to post my attempts to edit the manifest here.

I know I can change .apk to .zip and simply extract it, and then try to modify the androidmanifest.apk file. But, I’m pretty sure that at least some portion of it is signed and encrypted. It doesn’t look like I can just start typing into it at this point.

Maybe if there were an option to pause the build to edit the manifest before any signing takes place…

I also tried a tool called APK Editor which is available for Mac and PC.

That looked like it might allow a few edits to values like the Min and Target SDK, but all the other properties on the left are uneditable.

You can set up a keystore for the tool in preferences. I used the same one as Xojo.

It seems like if I’m using the same signing information here I might be able to edit the manifest contents, save it using APK Editor, and have it properly signed with my edits.

Then I clicked “Open Contents” back on the main screen, which opens finder pointing to ApplicationManifest.xml. I opened that in Xcode to edit it.

The dependency for com.google.mlkit.vision.DEPENDENCIES was already there in the metadata. So no need to add anything there.

So I looked into this:

That looks like I have to get into the build.gradle file. I’m not positive, but if it’s a build file I would expect it to have to be edited before building with Xojo.

My guess is that they are generating all the stuff when you press Build in Xojo and there is probably no way to pause and edit it in order to attempt to add anything related to ModuleInstallClient.