New Android Studio Versions
Newer Android Studio users may need to use updated versions for Android Gradle Plugin (AGP) and Gradle.
How to Change AGP and Gradle Versions
You can change the AGP version in the build.gradle file (buildscript —> dependencies —> classpath) by replacing with the current version number. Then, change the Gradle version under Project Structure.
The current minimum supported version is 8.6 for Android Studio Jellyfish | 2023.3.1 with AGP 8.4.1.
Common Errors
After syncing and building, you may run into other errors.
Namespace Missing
Namespace is required for AGP 8.0.0. If the namespace is missing, it can be added under android in the build.gradle file for Module: app. Simply type namespace followed by the same string used by the applicationId under defaultConfig. This is also the same as the package name in the AndroidManifest.xml file under manifests. If the applicationId differs from the package name, use the package name.
Some demos may not have an app folder, or the app folder is empty. This is due to the demo being converted from eclipse. In these cases, the two build.gradle files are combined into one file. Similarly, add the namespace under android in the build.gradle file, but it should be for Project: name instead of Module: app.
Package Does Not Exist (aidl files not found)
If the demo uses aidl, add the following lines of code within android {} (also in build.gradle) as some new versions have default value false:
Constant Expression Required
If the demo uses buttons with a switch-case function, you may get an error message "constant expression required" for a line such as case R.id.example. This is due to AGP 8.0.0 having default resources that are no longer declared final for optimized build speed.
You can add android.nonFinalResIds=false to gradle.properties or change the switch-case function to if-else statements in the file where the error occurs (generally MainActivity.java).
gradle.properties
Simply copy the following line to the bottom of the file.
Replacement with if-else
Mouse over the switch keyword and Android Studio should give you the option to replace. Alternatively, Ctrl + 1 or Alt + Enter will also bring up the option. The edited function should look like this:
Cannot Find Symbol for AndroidX
Copy the following line under dependencies in build.gradle.
You can also manually change the import statements following this mapping. For example:
Alternatively, you can copy these statements below into gradle.properties, but following the mapping is recommended as the enableJetifier flag can lead to slower build times.
Could not find method compile()
Some configurations have been deprecated since Gradle 4.10 and removed since Gradle 7.0. These include compile, runtime, testCompile, and testRuntime and should be replaced with implementation, runtimeOnly, testImplementation, and testRuntimeOnly respectively in the build.gradle file.
Manifest merger failed
If you receive the following error:
Manifest merger failed : android:exported needs to be explicitly specified for element <activity#example element>. Apps targeting Android 12 and higher are required to specify an explicit value for android:exported when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
In the AndroidManifest.xml file, copy the following line into the activity element where the error occurs.
An example AndroidManifest.xml file is below.
Example build.gradle Files
After applying the changes above, your files should look similar to these examples.
build.gradle (Project: Name)
build.gradle (Module: app)
gradle.properties
Last updated