【发布时间】:2015-01-14 15:22:29
【问题描述】:
我正在使用 Android NDK 将 C/C++ 代码库构建到共享库中。 NDK 成功构建代码并生成 .so 文件。然后我想把预建的共享库(.so 文件)放到我的 Android Studio 项目中。我使用的 Android Studio 版本是 1.0.1。当我按照this article 的建议将所有预构建的四个.so 文件放在src/main/jniLibs 中的jniLibs 文件夹下,然后我在Android Studio 中构建整个项目时,构建失败,因为它报告了一些“C++ STL header not found”错误。
我认为将 .so 文件放在 jniLibs 文件夹下以将 NDK 与 Android Studio 集成一样简单,但事实并非如此。显然,Android Studio gradle 构建系统正在重新编译我的 C/C++ 源代码,即使我已经使用命令行 NDK 构建了共享库。由于 NDK 可以成功构建 C/C++ 代码,Android Studio 不应重新编译代码,而应使用预先存在的 .so 文件。这是 NDK 与 Android Studio 集成中的一个已知问题吗?如何让 Android Studio 使用预构建的 .so 文件,而不是从其自身重建 C/C++ 代码?
这里是 $PROJECT/app/build.gradle 文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.myapp"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
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'
}
另外,这里是项目根目录$PROJECT/build.gradle的顶层build.gradle文件:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
如何解决这个问题,让 Android Studio 直接使用预构建的共享库,而不是重新构建 C/C++ 源代码?我认为这可能是由于带有 NDK 的 Android Studio 中的一个已知问题,或者是 Android Studio 使用的 gradle 脚本中有一个技巧来避免重新编译本机代码。
【问题讨论】:
-
它应该像...一样简单。哈哈哈 LMAO 对不起,我感觉到你的痛苦,我正在尝试做同样的事情。
标签: android android-studio android-ndk android-gradle-plugin