How to set NDK path in Gradle build scripts when using CMake?
Asked Answered
C

2

6

So all the reading I've done online says to set ndk.dir in the ANT build properties file to fix the "NDK not configured" error I'm getting from Gradle, but obviously I can't do that because I'm not using ant, I'm using Gradle+CMake.

Below is my gradle script. I'm still very new to this, I'm just trying to figure things out as I go. I got the java piece building fine in Gradle but at this point I am not able to integrate CMake.

apply plugin: 'com.android.library'

android {
    compileSdkVersion 17
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 17

        ndk {
            moduleName "UI"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    lintOptions {
        abortOnError false
    }

    externalNativeBuild {
        cmake {
            path '../../CMakeLists.txt'
        }
    }
}

Where can I specify the NDK path? I have an environment variable defined named ANDROID_NDK. CMake 3.7 uses this to hunt down the location of the NDK already. Why isn't the android gradle script using it? What is the proper way since I'm not using ant?

Note: I'm trying to get things building on the command line first outside of Android Studio via gradle build command. The error I get:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':Core:UI'.
> NDK not configured.
Download it with SDK manager.)
Concord answered 21/10, 2016 at 20:22 Comment(0)
T
4

You can specify the path to the NDK in Android Studio by selecting the File->Project Structure menu item.

You should then see the Project Structure settings dialog where you can specify the NDK location.

The value you enter here will modify the ndk.dir value in the local.properties file, which you can see in the background in the image below:

enter image description here

Talya answered 21/10, 2016 at 21:11 Comment(1)
Did I falsely assume that local.properties is an Ant-specific build file? Because now it seems like it isn't, and instead a general settings file for AS in general. Also how can I set this up in version control using environment variables? Does each person have to set this up manually?Concord
O
0

You can do this by passing arguments to CMake, like this, for example:

...
  defaultConfig {
    minSdkVersion 15
    targetSdkVersion 17

    externalNativeBuild {
      cmake {
        arguments.add(0, "-DANDROID_NDK:PATH=/path/to/ndk")
    }

    ndk {
        moduleName "UI"
    }
  }
...

Note that there are two separate externalNativeBuild blocks, the one inside defaultConfig is where you pass the arguments and the one at the same level as defaultConfig is where you set the path to CMakeLists.txt.

The exact parameters you pass to CMake will depend on how the CMake build is configured. Some CMake build scripts will read the required NDK path from and environment variable so you need to consult the documentation for your situation.

Ophthalmitis answered 5/3 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.