简体   繁体   English

如何在Android Studio和/或gradle中设置NDK_MODULE_PATH?

[英]How to set NDK_MODULE_PATH in Android Studio and/or gradle?

I'm in the process of migrating an Android Library from Eclipse to Android Studio. 我正在将Android库从Eclipse迁移到Android Studio。 In Eclipse I could set NDK_MODULE_PATH via Project > Properties > Resource > Linked Resources > Path Variables. 在Eclipse中,我可以通过Project> Properties> Resource> Linked Resources> Path Variables设置NDK_MODULE_PATH。 But how do I achieve the same thing in Android Studio respectively with gradle? 但是如何使用gradle分别在Android Studio中实现相同的功能呢?

My library builds until it tries to find the headers for a module under NDK_MODULE_PATH. 我的库构建,直到它试图在NDK_MODULE_PATH下找到模块的头。

Here's the error message: 这是错误消息:

Executing tasks: [:libAndroid:compileDebugSources, :physicaloidLibrary:compileDebugSources]

Configuration on demand is an incubating feature.
:libAndroid:preBuild UP-TO-DATE
:libAndroid:preDebugBuild UP-TO-DATE
:libAndroid:checkDebugManifest
:libAndroid:preDebugAndroidTestBuild UP-TO-DATE
:libAndroid:preReleaseBuild UP-TO-DATE
:physicaloidLibrary:compileLint
:physicaloidLibrary:copyReleaseLint UP-TO-DATE
:physicaloidLibrary:mergeReleaseProguardFiles UP-TO-DATE
:physicaloidLibrary:preBuild UP-TO-DATE
:physicaloidLibrary:preReleaseBuild UP-TO-DATE
:physicaloidLibrary:checkReleaseManifest
:physicaloidLibrary:prepareReleaseDependencies
:physicaloidLibrary:compileReleaseAidl UP-TO-DATE
:physicaloidLibrary:compileReleaseRenderscript UP-TO-DATE
:physicaloidLibrary:generateReleaseBuildConfig UP-TO-DATE
:physicaloidLibrary:generateReleaseAssets UP-TO-DATE
:physicaloidLibrary:mergeReleaseAssets UP-TO-DATE
:physicaloidLibrary:generateReleaseResValues UP-TO-DATE
:physicaloidLibrary:generateReleaseResources UP-TO-DATE
:physicaloidLibrary:packageReleaseResources UP-TO-DATE
:physicaloidLibrary:processReleaseManifest UP-TO-DATE
:physicaloidLibrary:processReleaseResources UP-TO-DATE
:physicaloidLibrary:generateReleaseSources UP-TO-DATE
:physicaloidLibrary:compileReleaseJava UP-TO-DATE
:physicaloidLibrary:processReleaseJavaRes UP-TO-DATE
:physicaloidLibrary:packageReleaseJar UP-TO-DATE
:physicaloidLibrary:compileReleaseNdk UP-TO-DATE
:physicaloidLibrary:packageReleaseJniLibs UP-TO-DATE
:physicaloidLibrary:packageReleaseLocalJar UP-TO-DATE
:physicaloidLibrary:packageReleaseRenderscript UP-TO-DATE
:physicaloidLibrary:bundleRelease UP-TO-DATE
:libAndroid:prepareLibAndroidGradlePhysicaloidLibraryUnspecifiedLibrary UP-TO-DATE
:libAndroid:prepareDebugDependencies
:libAndroid:compileDebugAidl UP-TO-DATE
:libAndroid:compileDebugRenderscript UP-TO-DATE
:libAndroid:generateDebugBuildConfig UP-TO-DATE
:libAndroid:generateDebugAssets UP-TO-DATE
:libAndroid:mergeDebugAssets UP-TO-DATE
:libAndroid:generateDebugResValues UP-TO-DATE
:libAndroid:generateDebugResources UP-TO-DATE
:libAndroid:mergeDebugResources UP-TO-DATE
:libAndroid:processDebugManifest UP-TO-DATE
:libAndroid:processDebugResources UP-TO-DATE
:libAndroid:generateDebugSources UP-TO-DATE
:libAndroid:compileDebugJava UP-TO-DATE
:libAndroid:compileDebugNdk
AGPBI: {"kind":"SIMPLE","text":"/home/rhodo/dev/android/studio/LibAndroidGradle/libAndroid/src/main/jni/imageprocessing/dmtxutil.c:30:18: fatal error: dmtx.h: No such file or directory","position":{},"original":"/home/rhodo/dev/android/studio/LibAndroidGradle/libAndroid/src/main/jni/imageprocessing/dmtxutil.c:30:18: fatal error: dmtx.h: No such file or directory"}
AGPBI: {"kind":"SIMPLE","text":" #include \u003cdmtx.h\u003e","position":{},"original":" #include \u003cdmtx.h\u003e"}
AGPBI: {"kind":"SIMPLE","text":"                  ^","position":{},"original":"                  ^"}
AGPBI: {"kind":"SIMPLE","text":"compilation terminated.","position":{},"original":"compilation terminated."}
AGPBI: {"kind":"SIMPLE","text":"make: *** [/home/rhodo/dev/android/studio/LibAndroidGradle/libAndroid/build/intermediates/ndk/debug/obj/local/arm64-v8a/objs/Rhodo//home/rhodo/dev/android/studio/LibAndroidGradle/libAndroid/src/main/jni/imageprocessing/dmtxutil.o] Error 1","position":{},"original":"make: *** [/home/rhodo/dev/android/studio/LibAndroidGradle/libAndroid/build/intermediates/ndk/debug/obj/local/arm64-v8a/objs/Rhodo//home/rhodo/dev/android/studio/LibAndroidGradle/libAndroid/src/main/jni/imageprocessing/dmtxutil.o] Error 1"}


 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':libAndroid:compileDebugNdk'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/opt/android-ndk/ndk-build'' finished with non-zero exit value 2

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.763 secs

Any ideas on this? 有什么想法吗?

Using: 使用:

  • Gradle version: 3.3 Gradle版本:3.3
  • Android plugin version: com.android.tools.build:gradle:2.3.1 Android插件版本:com.android.tools.build:grad:2.3.1

This works for me: 这对我有用:

android {

    ...

    defaultConfig {

        ...

        ndk {
            abiFilters 'armeabi', 'armeabi-v7a'
        }

        externalNativeBuild {
            ndkBuild {
                // TODO replace jniDependencies folder with the path to your modules
                arguments "NDK_MODULE_PATH:=${rootProject.projectDir}/jniDependencies"
            }
        }
    }

    externalNativeBuild {
        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
    }


}

This way I don't need to add NDK_MODULE_PATH to my PATH in order to build the project. 这样我就不需要将NDK_MODULE_PATH添加到我的PATH中来构建项目。 However, the clean task fails, because that won't pick up the argument from here, so I needed to add another hack to fix that: 但是,clean任务失败了,因为这不会从这里获取参数,所以我需要添加另一个hack来解决这个问题:

tasks.withType(com.android.build.gradle.tasks.ExternalNativeCleanTask) {
    it.actions.clear()

    doLast {
        project.delete("${projectDir}/.externalNativeBuild")
    }
}

At the root of the Android Studio project, there is a file called "local.properties". 在Android Studio项目的根目录下,有一个名为“local.properties”的文件。 Add a property named "ndk.dir" and set the value to point to the location of the ndk. 添加名为“ndk.dir”的属性并将值设置为指向ndk的位置。 Something like the following: 类似于以下内容:

ndk.dir=/Users/username/sdk

I may not be understanding your question. 我可能不理解你的问题。 If you've already set the location of the ndk and just want to make sure a module gets built then add the module to the "settings.gradle" file. 如果您已经设置了ndk的位置并且只想确保构建模块,那么将模块添加到“settings.gradle”文件中。

include ":<your module name>"

Then make sure you put the C code in "src/main/jni" in the module. 然后确保将C代码放在模块中的“src / main / jni”中。 Gradle will find it automatically based on the folder name. Gradle将根据文件夹名称自动找到它。 There is no longer a need to set the NDK_MODULE_PATH. 不再需要设置NDK_MODULE_PATH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM