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
Download the source code to ANY reasonable location on your pc hard drive and unzip it.
Open Android Studio.
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.
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.
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.
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.
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".
- Select your emulated AVD and run "App". If it works, it should now look like this:
Running app
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'
}
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" />