Cause: compileSdkVersion is not specified. Please add it to build.gradle

I created a new project and tried to compile it after a while. An error message appeared:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'MyApp'.
> compileSdkVersion is not specified. Please add it to build.gradle

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

I’m very surprised. I haven’t changed anything, so why does it suddenly report an error?

So I checked one by one and finally found the problem. In my project APP, the dependencies are as follows:

dependencies {

    ...
    implementation "androidx.core:core-ktx:+"
    ...
}

The key point is this +, which means always using the latest version, and the latest version may have various requirements on compileSdk and tools versions, resulting in the final compilation failure.

This dependent library is automatically created when the project is created. If it is not used, just delete it. If it is used, it is recommended to use a fixed version.

In addition, the version must be declared in the project's build.gradle file as follows:

plugins {
    id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
}

Similar errors include the following, etc., all caused by this problem

1.  Dependency 'androidx.core:core:1.9.0-beta01' requires libraries and applications that
      depend on it to compile against version 33 or later of the
      Android APIs.

      :app is currently compiled against android-32.

      Also, the maximum recommended compile SDK version for Android Gradle
      plugin 7.2.1 is 32.

      Recommended action: Update this project's version of the Android Gradle
      plugin to one that supports 33, then update this project to use
      compileSdkVerion of at least 33.

      Note that updating a library or application's compileSdkVersion (which
      allows newer APIs to be used) can be done separately from updating
      targetSdkVersion (which opts the app in to new runtime behavior) and
      minSdkVersion (which determines which devices the app can be installed
      on).
2.  Dependency 'androidx.core:core-ktx:1.9.0-beta01' requires libraries and applications that
      depend on it to compile against version 33 or later of the
      Android APIs.

      :app is currently compiled against android-32.

      Also, the maximum recommended compile SDK version for Android Gradle
      plugin 7.2.1 is 32.

      Recommended action: Update this project's version of the Android Gradle
      plugin to one that supports 33, then update this project to use
      compileSdkVerion of at least 33.

      Note that updating a library or application's compileSdkVersion (which
      allows newer APIs to be used) can be done separately from updating
      targetSdkVersion (which opts the app in to new runtime behavior) and
      minSdkVersion (which determines which devices the app can be installed
      on).

Guess you like

Origin blog.csdn.net/AA5279AA/article/details/126280942