在Android Studio中是否可以使用.so作为输出构建本机模块 [英] Is it possible in Android Studio to build a native module with .so as output
问题描述
我有几个c ++项目,目前正在使用Android.mk文件和ndk-build构建.由于调试起来并不是很好,所以我想将此c ++项目作为模块包含在我的android studio项目中.这个android studio项目目前是一个android java库,它使用ndk-build中的.so.
I have several c++ projects which I currently build using Android.mk files and ndk-build. Since this is not really great to debug I want to include this c++ projects as modules inside my android studio project. This android studio project is currently an android java library which uses the .so from the ndk-build.
我通过创建一个新的android库模块并导入c ++代码,设置标头和库路径来调试工作.该模块如下所示:
I got debugging to work by creating a new android library module and importing the c++ code, setting header and library paths. This module looks like this:
apply plugin: 'com.android.model.library'
model {
android {
compileSdkVersion = 23
buildToolsVersion = '23.0.2'
defaultConfig {
minSdkVersion.apiLevel = 4
targetSdkVersion.apiLevel = 23
}
}
android.ndk {
moduleName = 'module_name'
toolchain = 'gcc'
stl "gnustl_static"
ldFlags.addAll(["-L/LIB_PATH"])
ldLibs.addAll(["android", "log", "whatever_lib"])
cppFlags.addAll(['-std=c++11',
"-I" + "/SEARCH_PATHS"])
}
android.productFlavors {
create("arm7") {
ndk.abiFilters.add("armeabi-v7a")
}
}
android.sources {
main {
java.source.srcDirs += []
res.source.srcDirs += []
assets.source.srcDirs += []
jniLibs.source.srcDirs += []
jni.source{
srcDirs += [
NATIVE_PROJECT_PATH + "include/",
NATIVE_PROJECT_PATH + "src/",
]
}
}
}
}
这工作正常,但我在模块/build/output/aar/libname_release.aar/lib/abi/lib.so中得到了.so,这不是一个好地方,因为我需要直接使用.so而不解压缩aar .
This works fine but i get the .so in module/build/output/aar/libname_release.aar/lib/abi/lib.so which is not a good spot since i would need the .so directly without unzipping the aar.
有没有办法让此模块仅在给定路径下创建.so并让另一个模块构建并使用它?
Is there any possible way to let this module only create the .so at a given path and have another module build and use it?
推荐答案
当然,我在询问后不久便找到了一种方法. http://tools.android.com/tech-docs/new-build-system/gradle-experimental#TOC-NDK-Dependencies
Of course I find a way shortly after asking. http://tools.android.com/tech-docs/new-build-system/gradle-experimental#TOC-NDK-Dependencies
您可以使用"apply plugin:'com.android.model.native"代替"apply plugin:'com.android.model.library'",并且在具有依赖性的模块中可以使用:
Instead of "apply plugin: 'com.android.model.library'" you can use "apply plugin: 'com.android.model.native'" and in the module having the dependency you can use:
sources {
main {
jni {
dependencies {
project ":dependency_module"
}
}
}
}
这篇关于在Android Studio中是否可以使用.so作为输出构建本机模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!