Search code examples
androidgradledependency-managementandroid-media3

Media3 dependencies for build.gradle.kts for Android Studio 2023.2.1 Patch 2


I am trying to build an app that uses Media3 by following this: https://developer.android.com/media/media3/exoplayer/hello-world#kts

When I try to add dependencies in my app for its build.gradle.kts Android Studio does not like the syntax given in the above link. Can someone help tell me what I am missing.

P.S. I am doing the kts way not the groovy way as it is the recommended way.

Currently, when I build default empty activity app from Android Studio, my build.gradle.kts looks like:

.....
dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)

}

According to android link https://developer.android.com/media/media3/exoplayer/hello-world#kts when I paste:

....
implementation("androidx.media3:media3-exoplayer:1.3.0")
implementation("androidx.media3:media3-exoplayer-dash:1.3.0")
implementation("androidx.media3:media3-ui:1.3.0")
....

It gives me following warning:

Use version catalog instead More... (⌘F1) Inspection info:If your project is using a libs.versions.toml file, you should place all Gradle dependencies in the TOML file. This lint check looks for version declarations outside of the TOML file and suggests moving them (and in the IDE, provides a quickfix to performing the operation automatically)

So:

  1. What is the correct way to add media3 dependencies for latest Android Studi - Iguana
  2. Why my default build.gradle.kts for hello world project dependencies format look so different than the one provided in https://developer.android.com/media/media3/exoplayer/hello-world#kts. Default one like something like implementation(libs.androidx.material3), starts with libs. Please help, I am confused.

Solution

  • You'd have to edit file gradle/libs.versions.toml and add in TOML format:

    [versions]
    androidx_compose_bom           = '2024.03.00'
    androidx_compose_uitest        = '1.6.4'
    androidx_media3                = '1.3.0'
    # ...
    
    [plugins]
    # ...
    
    [libraries]
    androidx_compose_bom           = { module = "androidx.compose:compose-bom",          version.ref = "androidx_compose_bom" }
    androidx_compose_uitest        = { module = "androidx.compose.ui:ui-test-junit4",    version.ref = "androidx_compose_uitest" }
    androidx_media3_exoplayer      = { module = "androidx.media3:media3-exoplayer",      version.ref = "androidx_media3" }
    androidx_media3_exoplayer_dash = { module = "androidx.media3:media3-exoplayer-dash", version.ref = "androidx_media3" }
    androidx_media3_ui             = { module = "androidx.media3:media3-ui",   version.ref = "androidx_media3" }
    # ...
    

    And one can even bundle these (optional):

    [bundles]
    androidx_media3                = ["androidx_media3_exoplayer", "androidx_media3_exoplayer_dash", "androidx_media3_ui"]
    

    Which means, you can't just copy & paste, but have to convert to TOML.
    Be aware that for BOM dependencies, this only works for the BOM itself.
    When there's no version number, one can use: //noinspection UseTomlInstead.

    The names of the definitions of the default empty activity app are kind of ambiguous, since they're not explicit enough. There androidx should better be called androidx_compose, where applicable... because eg. libs.androidx.ui does not provide any understandable meaning (low readability), compared to libs.androidx.compose.ui. Proper and consistent labeling is important there.


    Once added there, one can use them in build.gradle or build.gradle.kts:

    implementation(libs.androidx.media3.exoplayer)
    implementation(libs.androidx.media3.exoplayer.dash)
    implementation(libs.androidx.media3.ui)
    

    Or by the bundle:

    implementation(libs.bundles.androidx.media3)
    

    Further reading: