问题
In the documentation for Android NDK, the following statement is present:
The Android.mk file resides in a subdirectory of your project's jni/ directory [...] http://developer.android.com/ndk/guides/android_mk.html
I can interpret from this that an Android.mk file should be placed in [project_path]/jni/[module_name]/Android.mk, each module having its own specific Android.mk file since this is what differentiates it from the application wide Application.mk file, but when I execute ndk-build I get the following error message:
Android NDK: There is no Android.mk under ./jni
Android NDK: If this is intentional please define APP_BUILD_SCRIPT to point
Android NDK: to a valid NDK build script.
I gather from that I am supposed to create a single Android.mk file alongside my Application.mk file or define APP_BUILD_SCRIPT in Application.mk to point to a single Android.mk file. This contradicts the documentation and leaves me wondering why there is a need for multiple makefiles when Android.mk will contain the definitions for all modules anyway-that could just as well be placed in Application.mk.
Reading a couple of NDK sample projects I found out that, indeed, the Android.mk file is in the same directory as Application.mk and executing ndk-build on them seems to work.
What is missing?
回答1:
I can answer at least some of your questions. You are right about the documentation being a little confusing. If you are using a single native module, indeed the Application.mk may seem redundant - but, there are a few things that can only be set by the Application.mk (you can look here: Application.mk). The Application.mk is meant for settings that apply for all modules, whereas the Android.mk is for specific module settings. Indeed, usually simple projects have a single Android.mk and it resides on the same folder as the Application.mk.
You can define where to put them, it also depends how you build your code - for example, if you are using a task for building ndk using 'ndk-build' and commandLine command, you pass the folder path as an argument. Usually, according to my experience, they reside under the jni folder.
回答2:
Expected project structure
<project>/
|
+-- jni/
| |
| +-- Android.mk
| +-- [Application.mk] (optionally)
| +-- main.c
|
+-- AndroidManifest.xml
According to a short guide from here: https://developer.android.com/ndk/guides/concepts
- Create an Android.mk file in the jni/ directory of your project
- ... compile your native code using the ndk-build
$ cd <path>/<to>/<project> $ <ndk>/ndk-build
However, there is a way to define custom NDK project structures using ndk-build with arguments.
Plain structure
<mylib>/
|
+-- Android.mk
+-- [Application.mk]
+-- main.c
$ cd <mylib>
$ ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=Android.mk
Android.mk vs Application.mk
why there is a need for multiple makefiles when Android.mk will contain the definitions for all modules anyway-that could just as well be placed in Application.mk.
Android.mkis mandatory andApplication.mkis not.Android.mkcontains module definitions andApplication.mkdescribes architecture, compiler options, and other "global" options.Android.mkis orthogonal toApplication.mkby design. IfAndroid.mkcontains 3 modules, andApplication.mk2 ABIs (e.g. arm and x86), then NDK will build (3 * 2) artifacts.- Variables from
Application.mkappears inAndroid.mk, so you can use, for instance,APP_ABIinAndroid.mkto link architecture dependent libraries. - There are complex projects with a lot of makefiles and keeping common options in
Application.mkis a clean way. - But still
Application.mkis just a design decision, it could be done differently.
来源:https://stackoverflow.com/questions/34372092/where-is-android-mk-supposed-to-be