如何在 Android Studio 中使用我自己的 Android.mk 文件 [英] How to use my own Android.mk file with Android Studio
问题描述
我在 Android.mk
文件中定义了一些变量(我为编译器传递了一些标志),但是每次我构建我的项目时,Android.mk
被覆盖.我假设 Gradle
是负责任的,我应该去那里看看?如何使用我自己的 Android.mk 文件?
I am defining some variables within the Android.mk
file (I am passing some flags for the compiler), but every time I build my project, the Android.mk
is overwritten. I am assuming that Gradle
is responsible and that I should be looking there? How do I use my own Android.mk file?
背景信息:
Ubuntu
64 位,Android Studio
1.0.1,JDK7.我已经用 O-LLVM NDK,因此我正在编辑位于 app/build/的
(它是我项目目录中唯一的 Android.mk
文件middles/ndk/debugAndroid.mk
文件),与 O-LLVM
的文档给出示例的位置不同
Ubuntu
64bit, Android Studio
1.0.1, JDK7.
I have wrapped my NDK
version with O-LLVM NDK, and as such am editing the Android.mk
file located at app/build/intermediates/ndk/debug
(it's the only Android.mk
file within my project dir), different to the location that the doc for O-LLVM
gives examples of.
此外,没有 Application.mk
文件,所以我再次假设 Gradle
负责对编译器的调用?
Also, there is no Application.mk
file, so again I am assuming that Gradle
is responsible for the calls to the compiler?
更新信息
build.gradle - (app)
//The following code until the "----" line is the new build.gradle config file
// that disables automatic Android.mk file generation
import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.md.helloworld"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
ndk {
moduleName "MyLib"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets.main {
jniLibs.srcDir 'src/main/libs' //set libs as .so's location instead of jni
jni.srcDirs = [] //disable automatic ndk-build call with auto-generated Android.mk file
}
// Call regular ndk-build (.cmd) script from the app directory
task ndkBuild(type: Exec) {
commandLine 'ndk-build', '-C', file('src/main/').absolutePath
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
/*
//The following code is the original Android.mk file
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.md.helloworld"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
//The only modified line
ndk {
moduleName "MyLib"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
*/
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := helloWorld
LOCAL_SRC_FILES := main.c
LOCAL_LDLIBS := -static
include $(BUILD_EXECUTABLE)
Application.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
APP_ABI := armeabi
NDK_TOOLCHAIN_VERSION := clang3.4-obfuscator
include $(BUILD_EXECUTABLE)
请注意:我还没有传递任何 cflags,我想先让 Vanilla 构建工作
推荐答案
是的,默认情况下,gradle android 插件会重新生成并使用它自己的 Android.mk 文件来编译您的源代码.
yes, by default the gradle android plugin regenerates and uses its own Android.mk file to compile your sources.
您可以停用此功能并改用您自己的 Android.mk 文件,方法是在您的 build.gradle 配置文件中进行设置:
You can deactivate this and use your own Android.mk file instead, by setting this inside your build.gradle configuration file:
import org.apache.tools.ant.taskdefs.condition.Os
...
android {
...
sourceSets.main {
jniLibs.srcDir 'src/main/libs' //set libs as .so's location instead of jniLibs
jni.srcDirs = [] //disable automatic ndk-build call with auto-generated Android.mk
}
// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
} else {
commandLine 'ndk-build', '-C', file('src/main').absolutePath
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
请注意,如果您只需要将自己的 cflags 传递给自动生成的 Makefile,则可以在 cFlags ""
属性中设置这些以在 android { ndk {}} 中设置
Note that if you only need to pass your own cflags to the auto-generated Makefile, you can set these inside the cFlags ""
property to set inside android { ndk {}}
这篇关于如何在 Android Studio 中使用我自己的 Android.mk 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!