【问题标题】:Android Studio and NDK integration issueAndroid Studio 和 NDK 集成问题
【发布时间】: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


【解决方案1】:

Android Studio 尚未正式支持 NDK,在添加预编译的共享 .so 文件时可能会出现一些问题。您可以强制 Android Studio 通过在 build.gradle 文件中添加它来禁用构建您的共享库文件

jni.srcDirs = [] //disable automatic ndk-build call

您还可以包含您的 Android.mk 和 Application.mk 文件。这是一个关于如何在 Android Studio 上构建 NDK 应用程序的详细视频https://www.youtube.com/watch?v=0fEtrekNcOo

【讨论】:

  • 这正是我在 Android Studio 中使用预构建的 .so 文件所做的。这在任何地方都没有正式记录,因为 AS 目前不支持 NDK。因此,使用两个单独的步骤进行构建对我来说完全没问题,因为 Makefile 仅用于构建 C/C++ 源文件,而 Gradle 仅用于构建 Android 应用程序代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2015-11-27
相关资源
最近更新 更多