Here are some useful tips

This article is about the problems faced while importing Android project with C++ or NDK code.
First and foremost, download NDK and CMake from SDK Tools as shown in the screenshot below.

As you can see, NDK (Side by side) and CMake is already installed in my Android Studio.
Issue 1:
After importing an Android project (not only C++ or NDK code), you may get the following error.
Error:
Your build is currently configured to use Gradle 8.0. Cannot sync the project. We recommend upgrading to Gradle version 8.9. The minimum compatible Gradle version is 8.5. We recommend upgrading to Gradle version 8.9.
The minimum compatible Gradle version is 8.5.
Possible solutions:
— Upgrade to Gradle 8.9 and re-sync
— Upgrade to Gradle 8.5 and re-sync
Solution:
For the above issue, solution is simple.
Upgrade Android Gradle Plugin (AGP) to the required version (8.5 or 8.9)
Once the solution is applied, you should see the updated version in gradle-wrapper.properties file like below
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
Issue 2:
While building Android project with C++ or NDK code, you may get the following error.
Error:
:app debug:arm64-v8a failed to configure C/C++
Solution:
To solve the above issue, we need to check and update the CMake version in app’s build.gradle like below.
externalNativeBuild { cmake { path "src/main/cpp/Android/CMakeLists.txt" version "3.31.5" } }
In my case, CMake version was 3.10.2. I upgraded it to 3.31.5 and that solved the issue.
Issue 3: (Runtime issue)
After solving Issue 1 and Issue 2, we may get runtime issues
Error:
org.gradle.workers.WorkerExecutionException: There were multiple failures while executing work items
Caused by: org.gradle.tooling.BuildException: Failed to process:
Caused by: com.android.builder.dexing.DexArchiveBuilderException: Failed to process:
After seeing DexArchiveBuilderException, one thing that might come in mind is about enabling MultiDex but that is not the issue here.
Solution:
Upgrading JavaVersion
From
compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }
To
compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 }
worked for me.
Issue 4:
After solving Issue 3, we may get the following error.
Error:
Execution failed for task ‘:app:compileDebugJavaWithJavac’.
> Could not resolve all files for configuration ‘:app:androidJdkImage’.
Solution:
After searching for the solution in StackOverFlow, got the below solution.
Update plugin version of our app in project’s build.gradle.
In my case, updating
From
plugins { id 'com.android.application' version '8.0.0' apply false }
To
plugins { id 'com.android.application' version '8.3.2' apply false }
solved the issue.
Job Offers
Another tip:
Directly applying solution of Issue 4 i.e updating plugin of our app in project’s build.gradle solved Issue 3(which means we can skip solution of upgrading JavaVersion). This was found out later.
. . .
Thanks for reading this article. If you like this post, Please give a clap (👏).
Also, if you like to support me through
https://buymeacoffee.com/dilipchandar, please do.
Let’s connect on LinkedIn https://www.linkedin.com/in/dilip-chandar-97570158?
This article is previously published on proandroiddev.com.