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

7

我正在将一个Android库从Eclipse迁移到Android Studio。在Eclipse中,我可以通过Project > Properties > Resource > Linked Resources > Path Variables设置NDK_MODULE_PATH。但是,在Android Studio中,我该如何使用gradle实现同样的功能?

我的库构建过程一直持续到它尝试查找NDK_MODULE_PATH下模块的头文件。

以下是错误信息:

执行任务:[:libAndroid:compileDebugSources, :physicaloidLibrary:compileDebugSources]
按需配置是一项孵化功能。 :libAndroid:preBuild已更新 :libAndroid:preDebugBuild已更新 :libAndroid:checkDebugManifest :libAndroid:preDebugAndroidTestBuild已更新 :libAndroid:preReleaseBuild已更新 :physicaloidLibrary:compileLint :physicaloidLibrary:copyReleaseLint已更新 :physicaloidLibrary:mergeReleaseProguardFiles已更新 :physicaloidLibrary:preBuild已更新 :physicaloidLibrary:preReleaseBuild已更新 :physicaloidLibrary:checkReleaseManifest :physicaloidLibrary:prepareReleaseDependencies :physicaloidLibrary:compileReleaseAidl已更新 :physicaloidLibrary:compileReleaseRenderscript已更新 :physicaloidLibrary:generateReleaseBuildConfig已更新 :physicaloidLibrary:generateReleaseAssets已更新 :physicaloidLibrary:mergeReleaseAssets已更新 :physicaloidLibrary:generateReleaseResValues已更新 :physicaloidLibrary:generateReleaseResources已更新 :physicaloidLibrary:packageReleaseResources已更新 :physicaloidLibrary:processReleaseManifest已更新 :physicaloidLibrary:processReleaseResources已更新 :physicaloidLibrary:generateReleaseSources已更新 :physicaloidLibrary:compileReleaseJava已更新 :physicaloidLibrary:processReleaseJavaRes已更新 :physicaloidLibrary:packageReleaseJar已更新 :physicaloidLibrary:compileReleaseNdk已更新 :physicaloidLibrary:packageReleaseJniLibs已更新 :physicaloidLibrary:packageReleaseLocalJar已更新 :physicaloidLibrary:packageReleaseRenderscript已更新 :physicaloidLibrary:bundleRelease已更新 :libAndroid:prepareLibAndroidGradlePhysicaloidLibraryUnspecifiedLibrary已更新 :libAndroid:prepareDebugDependencies :libAndroid:compileDebugAidl已更新 :libAndroid:compileDebugRenderscript已更新 :libAndroid:generateDebugBuildConfig已更新 :libAndroid:generateDebugAssets已更新 :libAndroid:mergeDebugAssets已更新 :libAndroid:generateDebugResValues已更新 :libAndroid:generateDebugResources已更新 :libAndroid:mergeDebugResources已更新 :libAndroid:processDebugManifest已更新 :libAndroid:processDebugResources已更新 :libAndroid:generateDebugSources已更新 :libAndroid:compileDebugJava已更新 :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"}
失败
构建失败,出现异常。
* 出错内容: 执行任务':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
* 尝试: 使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项获取更多日志输出
有关此事有什么想法吗?
2个回答

1

使用:

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

这对我有效:

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'
        }
    }


}

这样我就不需要将NDK_MODULE_PATH添加到我的路径中以构建项目。然而,清理任务失败了,因为它不会从这里获取参数,所以我需要添加另一个hack来解决这个问题:

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

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

-2
在Android Studio项目的根目录下,有一个名为“local.properties”的文件。添加一个名为“ndk.dir”的属性,并将其值设置为指向ndk的位置。类似以下内容:
ndk.dir=/Users/username/sdk

我可能没有理解你的问题。如果你已经设置了ndk的位置,只想确保一个模块被构建,那么将该模块添加到“settings.gradle”文件中即可。

include ":<your module name>"

然后确保将C代码放在模块的“src/main/jni”中。Gradle会根据文件夹名称自动找到它。不再需要设置NDK_MODULE_PATH。


我认为你理解得没错,但是为了澄清一下:术语“模块”有不同的用法,$NDK_MODULE_PATH下的模块是C/C++模块/库。我在我的库项目中创建了一个gradle模块。但是,“Android.mk” makefile被忽略了。网络上有几篇与此主题相关的文章。但是,如果依赖于这些makefile,则C/C++库的构建可能会失败。例如,“libdmtx”:我只需要将“dmtx.c”添加到makefile中,它就可以成功构建。如果没有makefile,ndk-build会尝试构建该文件夹中的每个文件,构建将失败。 - rhodo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接