My First Android App
First off, I do now know that allergic is misspelled in my GitHub repo but I'm planning on keeping it that way as an inside joke.
The idea for this app started from grocery shopping for a family friend that has a lot of food allergies.
I have a very hard time remembering everything she's allergic to and checking all the labels gets tedious to the point I miss ingredients that can make her sick.
Update 1.
- Before I go into some of the background on how I built this app, here are the links to the code (commit b68787463cf08dca682afe3b3e7ad46df5d661b0) and apk (amiallergic_2021-02-12T1644.apk) that mostly work.
- Taking the advice of my best friend Matt, I've created a branch of the working code and apk called prod.
If you go to the amialergic main repo, the latest code and/or apk might not work (like today, they won't work) because I'm actively working on fixing bugs.
What I want the app to do is basically what it's already doing as listed in points one through four below.
This app is very buggy, but when it works, this is what it does: 1. Scans a barcode 2. Takes the returned barcode value and puts it in a food API to get the info of that product in JSON. 3. Gets the ingredients object from the food API's JSON and saves it as a string. Uses string.match() to search the entire string for *corn* using regex. 4. If corn is found anywhere in the ingredients string, output is True. If corn is not found in the ingredients string, output is False. 5. If the request from the food API doesn't find the item the barcode scans then the app crashes and closes.
OK, we've covered how I got the idea for the app and what functionality I'm trying to achieve. What I have right now is a very minimal viable product with the commit and apk linked above. Like, "viable" is quite subjective here.
o To the Google!
I had no idea where to start, I knew literally nothing when I started down this path so I googled something like 'barcode reader android app' and didn't really find anything that made sense to me.
o To the YouTube!
If you have come this far in life and not learned by now that all the knowledge of the universe is on YouTube, I am honored to be the one to tell you.
Wanna swap out an alternator? YouTube.
Wanna bachelor's of science? YouTube.
Wanna build an Android app? YouTube.
After finally finding the video that made the most sense to me and watching it repeatedly, I was able to get an Android app that scans barcodes. This very first iteration of the app only printed to the screen what was encoded in the barcode. Sometimes it was a number, sometimes it was a URL, it all depends.
To make this app, I downloaded the latest version of Android Studio and follow a link on the YouTube page to the presenter's code. To find the code in this link you will have to scroll down to "Java Code:".
Somethings I learned the slow way googling errors:
1. Android Studio has a file called build.gradle(:app)
If pressing alt+enter doesn't auto fill your import statements like you think it should or just all around jankey, check that file to see if the package you're trying to use is implemented in the dependencies:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.jayway.jsonpath:json-path:2.5.0'
//implementation 'com.android.support:appcompat-v7:28.0.0'
//com.google.android.material:material
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.journeyapps:zxing-android-embedded:4.0.2'
}
2. Android Studio ties into GitHub really easily. I followed the steps in this blog[1] to connect Android Studio with GitHub.
As I remember it, these steps include directions on how to create an a personal access token, but just in case I'm wrong, here's a link[2] with those steps.
[1] https://medium.com/code-yoga/how-to-link-android-studio-with-github-312037a13b99
[2] https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
When you make a signed apk in Android Studio the apk may not be named the same as your project. The apk (on Windows) default location can be found at C:\Users\<YourUserName>\AndroidStudioProjects\amialergic\app\release\
I created a soft link to that release folder in my Windows filesystem to my WSL filesystem and wrote this script to rename the apk that Android Studio auto generates:
$ cat ~/.bin/renameapp #! /bin/bash mv $@ amiallergic_$(date -uIm | sed 's/\+00\:00//g' | sed 's/\://g').apk
Using the date for versioning has been very helpful and I highly recommend it.
So I have an apk and code that will scan barcodes committed and pushed up to my repo, what next?
o Back to Google!
From previous googling I found this free and open source API that has all the info about food you could possibly want called openfoodfacts.org.
Here's an example of their API for a can of Coke(r):
https://world.openfoodfacts.org/api/v0/product/04963406.json
Well, that's cool and all but how do I get that info into my app?
I'm glad you asked because after much googling I found out about a http client called okHttp3. How does it work exactly? I honestly don't know, but I was able to build a client to openfoodfacts.org and pull that JSON file by following the code examples on their website.
At this point I was able to figure out how to paste to the screen what okHttp3 was grabbing and I got so excited y'all! I could see all the JSON from the API after scanning a can of Coke(r) with my cell phone! This is really happening, I'm really making an app!
This is where I got stuck for a very long time. I thought okHttp3 pulled in the JSON as a string but there's no way it did, I needed one piece of code to be able to search the ingredients.
After much crying and gnashing of teeth, I finally understood what I wanted was called JsonPath.
JsonPath is a query language for JSON that let me pull out the ingredients object from all the rest of the JSON objects and save that as a string.
Now that I have the ingredients saved in a string it was too easy to do an if statement on a string.match to print true if found and false if not found.
That leaves us with an app that I have validated with a can of Coke(r) in real life!
My brand new baby app returned "True" after scanning a Coke(r) can and searching the ingredients for corn! Can you believe it!
There's still so much to do because what I think are simple code changes breaks this app and every thing I know about Android development I learned in the last four weeks or so.
Still super excited to get on this TODO list and keep making it better!
TODO:
o I'm pretty sure I'm not getting false positives on allergens but I really want to be extra confident before I start using this in real life.
o Add some error handling so the app doesn't crash when the barcode results aren't found in the food API.
o Have better output than "True" or "False". I'd like the screen to go red if allergens found and list which ones were found and green if no allergens found.
o Check the food API and see if I could be implementing it better.
o Allergens are hard coded in the app, would like to be able to add or remove allergens from the app's UI.
o Be able to track allergens for more than one person.
//end
Congratulations brother
This is very cool, I'm not a coder, but anytime I have cobbled something like a webapp together, that's been a real struggle and ultimately works, has given me an incredible feeling of accomplishment.. Congrats on the App Aaron!
Good job!
This is great! Keep that write up going, blog-style... I’m a fan!
I'm going to dl when I can, I think it's always a wise idea to build something "you" can use to accomplish a task or some objective. I personally don't think you could run out of idea's with that approach. Very cool idea the way you leverage barcodes and #foodApi in your logic. Look forward to trying it out Aaron Berger #brotherAaron