0

I am a senior java web services developer, trying to learn Android Studio / kotlin.

I'm doing the CodeLab in the title stated in my stackoverflow question: "Detect objects in images with ML Kit for Firebase: Android", which is here: https://codelabs.developers.google.com/codelabs/mlkit-android-odt/index.html?index=..%2F..index#0

I have the connection to the firebase server all working (which is probably the hard part), but now I'm stuck on what should be a simple part:

Where do I put the downloaded "starter" source code? Unzipped file structure is D:\mlkit-android-master\object-detection\starter The Codelab website instructions don't really make that clear. They are saying that I can just add it all to my existing project, but it looks like I'd have to manually merge it (not fun).

"starter" directory contains an "app" and a "gradle" directory, so I'm assuming that "starter should be my "Project" name, which is also called an "application" in Android Studio (go figure) as opposed to "app" which appears to be the gradle "module" level in Android Studio (just to add to the name confusion).

Ben Ethridge
  • 79
  • 1
  • 1
  • 4
  • what kind of files are inside the starter source code? – beastlyCoder Apr 22 '20 at 22:20
  • Lots of different kinds. At which level would you like to know? Easiest would be to go to the link above, click on "Getting set up" on the upper left and click on "Download source code". – Ben Ethridge Apr 22 '20 at 23:46

3 Answers3

0

sometimes is a bit confusing

Unpack the downloaded zip file. This will unpack a root folder (mlkit-android) with all of the resources you will need. For this codelab, you will only need the sources in the object-detection subdirectory.

So, step by step:

enter image description here

This is the structure of the folder, and it says "ou will only need the sources in the object-detection subdirectory"

Try to open this folder(project) with Android Studio: Search for the folder "stater" and open it, do not open app then you will see something like that:

enter image description here

Now let’s code!

Azhagthott
  • 492
  • 5
  • 13
  • Thanks. Does it matter where I unzip the root folder? Currently all my other Android projects are in this folder: D:\Users\charl\AndroidStudioProjects. Am I supposed to unzip the downloaded source in that folder? – Ben Ethridge Apr 23 '20 at 13:49
  • Also, yes, I read that it only "needs" the sources in object-detection, but does that mean Android Studio will ignore all the other source folders, or am I supposed to delete those other folders? – Ben Ethridge Apr 23 '20 at 13:52
  • I had to import the starter directory, as opposed to just opening it, but that at least got it into Android Studio, i.e. my structure for it now looks like yours: – Ben Ethridge Apr 23 '20 at 16:12
  • I had to import the starter directory, as opposed to just opening it, but that at least got it into Android Studio, i.e. my structure for it now looks like yours. – Ben Ethridge Apr 23 '20 at 16:22
  • I would insert a screenshot here, but even after reading help (right of this comment) and reading the section on inserting images (using cntrl G), it's not inserting. Not sure which level is taking over (web page, JavaScript, Windows 10 or the hardware), but it starts reading every word on the page (starting with Products in upper left), highlighting the word and translating to voice. Probably for people who are blind. So how are you inserting your screenshots? – Ben Ethridge Apr 23 '20 at 16:23
  • I see now that pictures can only be added to "answers". – Ben Ethridge Apr 23 '20 at 17:40
  • I tried to upvote this but I don't have enough reputation on stackoverflow yet. – Ben Ethridge Apr 24 '20 at 18:23
0

Ok, so I got this to build without error (but not run yet, since I don't have my functionality fleshed out yet). Here's what I had to do high-level, and it's definitely NOT intuitive and NOT explained in the tutorial. (To be fair, there may be other ways to do this, of course.) : 1. It asked me to migrate my project to AndroidX at one point, so I did that. 1a. Downloaded and unzipped the source to my pc (You can put it anywhere within reason).

  1. In my pre-existing CameraX project (Your Whatever project where you normally put Android projects, like D:\Users\charl\AndroidStudioProjects - just NOT the unzipped starter directory), I imported the module "app", from the unzipped source (D:\AndroidRepository\mlkit-android-master\object-detection\starter\app) but renamed it "app1" as I imported it into my CameraX project, since I already had an "app" module in my project. When I first did the module import, it was there on my menu somewhere(?), but now I can't find it so if that happens to you, go to help->Find Action->Import Module... Fortunately, it's smart enough to know what a "module" (aka "app") structure is supposed to look like, so it will help you get to the right level by showing you an error until you do.

  2. Since the json file has to be at the module (aka app) level for each module which talks to the firebase server, and since I already had this running in my pre-existing "app" module, I simply copied the json file from app to app1 at the same dir level. You could also re-download the json from the firebase console. The key is that your two modules (apps) have to have the same package structure if they use the same json file. (Again, this builds ok but I'm not sure it's going to run with the json in two modules. I'm just not sure how the firebase ID validation works yet), but that's surely going to be a solvable problem later.

  3. Expect the usual annoying jar/library version incompatibilities. When, after the module import, Android Studio recommended that I up my gradle version to latest/greatest, I was dumb enough to say yes. This lastest version was not, however, compatible with the latest/greatest Android SDK (10.0 Q API Level 29), but at least the build log gave me hints that it wanted API Level 28, so I backed down to that in the SDK manager, and then my project build with no errors.

Clean Build

Ben Ethridge
  • 79
  • 1
  • 1
  • 4
0

Unless you are trying to add MLKit as an app in one of your existing projects, you can ignore my earlier answer. It only partially solves the problem, and now I understand why. There are dependencies at the starter project level, that are required to get the app (module) to run. The answer for most people will be closer to Azhagthottk's answer, with perhaps a few minor changes and/or explanations. I tried to upvote his answer now that, in hindsight, I understand it, but I'm not at that level in StackOverflow yet.

The Problem

"A rose by any other name would smell as sweet." - Romeo and Juliet

I quote Shakespeare here because that's the essence of the problem: terminology "overloading" - same term with different meanings. The instructions on the Introduction page say this:

"This codelab will walk you through simple steps to add Object Detection and Tracking(ODT) for a given image into your existing Android app. "

...and this is what confused me big time. I kept thinking that I should create an empty project (also called an "app" sometimes - go figure) in Android Studio and then add JUST the app (aka module - go figure) to that project. Due to the aforementioned "overloaded" use of concepts/terms in Firebase and Android Studio, I took this statement literally, which was what my other answer was all about. In hindsight, it is very simple to get this project downloaded, added into Android Studio and running. Here are the steps:

The Solution

  1. Download the source code to ANY reasonable location on your pc hard drive and unzip it.

  2. Open Android Studio.

  3. From the main menu: File-> New-> Import Project... Navigate down to "starter" directory. Click on it and then click the OK button. Side note for newbies: Don't think that Android Studio has enough common sense to ask you if you want to "import" the files to some logical location, say, underneath your AndroidStudioProjects directory. It's not. You have to do that yourself first. It's going to leave them and modify and run them from wherever you unzipped them. Go figure. So, be aware of future potential file security issues due to this, if you forget where you left them laying around.

  4. As shown on page 3 of the codelab instructions, set up your firebase. Note that it is vital that the package in firebase match the package in your app (module), or the communication between your app and the firebase server won't work.

  5. Per the firebase instructions page 4, download the generated json file from the firebase and install it just under the app folder level. Probably best to File-> Synch With File System now, to guarantee the json will be seen by Android Studio.

  6. As shown on page 4 of the codelab instructions, add the dependencies into build.gradle AT THE APP level, not at the project level. You should probably put the plugin statement up at the top with the others, not at the bottom as shown in the instructions. (Just good coding standard.) Since you already did the File->Import project, there no need to do the File->Open (shown in the CodeLab instructions) now. Just do the "Synch Project With Gradle Files", just in case they didn't auto-synch.

  7. From the main menu Build-> Make project. Note that this gives me an error on this line in the app-level build.gradle file:

implementation 'com.android.support:appcompat-v7:28.0.0'

...and yes, I have API level 28 already in the sdk.

I get a few other warning-level errors in the same file. All are about potential version incompatibilities. The CodeLab was probably written a while back and the versions have moved forward. Fair enough. I'll deal with that later since it's not stopping the build/run altogether at this point. I'm going to think positive and hope this doesn't turn into "jar hell".

  1. Select your emulated AVD and run "App". If it works, it should now look like this:

Running app

  1. I moved this forward to the latest/greatest versions, as shown in the below module-level build.gradle. Android Studio then complained that I needed to migrate to AndroidX, which I also did. This all caused several build errors, which I fixed by simply taking the default suggested fixes, and it all still runs as before, but with no more build errors. The updated module-level build.gradle file:

    apply plugin: 'com.android.application'

    apply plugin: 'kotlin-android'

    apply plugin: 'kotlin-android-extensions'

    apply plugin: 'com.google.gms.google-services'

    android { compileSdkVersion 29 defaultConfig { applicationId "com.google.firebase.mlkit.codelab.objectdetection" minSdkVersion 16 targetSdkVersion 29 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } }

    dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'com.google.android.material:material:1.1.0'

    // MLKit ODT libraries
    implementation 'com.google.firebase:firebase-ml-vision:24.0.2'
    implementation 'com.google.firebase:firebase-ml-vision-object-detection-model:19.0.4'
    

    }

  2. I got a runtime error on the FloatingActionButton. Apparently (according to a similar error in StackOverflow), when you move up to AndroidX, you have to change to com.google.android.material.floatingactionbutton.FloatingActionButton in layout/activity_main.xml, so it becomes:

    <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/captureImageFab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_camera" android:layout_gravity="center|bottom" android:layout_margin="16dp" />

Ben Ethridge
  • 79
  • 1
  • 1
  • 4