2

I'm using Android NDK to build a C/C++ code base into a shared library. The NDK built the code successfully and generated the .so files. Then I want to put the prebuilt shared library (.so file) to my Android Studio project. The Android Studio version I'm using is 1.0.1. When I placed all prebuilt four .so files under the jniLibs folder in src/main/jniLibs as suggested by this article, and then I built the whole project in Android Studio, the build failed because it reported some "C++ STL header not found" error.

I thought it is just as easy as putting .so files under jniLibs folder to integrate NDK with Android Studio but it's not. Obviously the Android Studio gradle build system is recompiling my C/C++ source code even though I already built the shared library using command line NDK. Since NDK can successfully built the C/C++ code, Android Studio shouldn't recompile the code but should use the pre-existing .so files. Is this a known issue in NDK integration with Android Studio? How do I let Android Studio use prebuilt .so files instead of rebuilding C/C++ code from its own?

Here is the $PROJECT/app/build.gradle file:

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'
}

Additionally, here is the top-level build.gradle file in project root directory $PROJECT/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()
    }
}

How to solve this issue to let Android Studio use the prebuilt shared library directly instead of rebuilding the C/C++ source code again? I believe this may be either due to a known issue in Android Studio with NDK or there is a trick in the gradle script used by Android Studio to avoid recompiling native code.

1
  • It is supposed to be as easy as.... hhahahah LMAO sorry, I feel your pain , I am trying to do the same thing.
    – Josh
    Commented Dec 14, 2015 at 8:29

1 Answer 1

3

Android Studio does not officially support NDK yet and there might be some glitches in adding precompiled shared .so files. You can force Android Studio to disable building your shared lib files by adding this on build.gradle file

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

You can also include your Android.mk and Application.mk files. Here is a detailed video on how to build an NDK application on Android Studio https://www.youtube.com/watch?v=0fEtrekNcOo

1
  • This is exactly what I did to use prebuilt .so files in Android Studio. This is not officially documented anywhere since AS doesn't support NDK right now. So making a build with two separate steps is completely fine with me since Makefile is solely used to build C/C++ source files and Gradle is solely used to build Android app code.
    – tonga
    Commented Jan 14, 2015 at 22:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.