3

So far into my foray with using NDK with Android Studio, I've written the Java Wrapper, generated the header file, and filled in some functions in the corresponding c++ file in JNI folder.

What I'm trying to do right now is to get a simple build going so that I can verify things work. My project relies on some c++ source files located outside of my entire android project. Do I build those source files somehow from within Android? How do I access them? Is there anything I need to do from Gradle?

I'm incredibly new to building projects with across multiple sources, so I have no idea what to do. Apologies if the questions don't make sense. Any help is greatly appreciated (:

4
  • If you're developing in Windows, you'll have to build the project separately using NDK and place the .so files in the project underneath a folder for each architecture, otherwise you can use Gradle to do everything. That should help direct your research efforts. In any case, you'll have to create a Android.mk and a Application.mk for NDK; the face-detection example in the OpenCV-android-sdk should help understand how to prepare for building with NDK. Commented Oct 4, 2014 at 0:52
  • I will definitely take a look at the OpenCV-android-sdk example; thanks!
    – bhbbby
    Commented Oct 5, 2014 at 6:59
  • I'm actually developing on a Mac, does that make a giant different in what you described?
    – bhbbby
    Commented Oct 5, 2014 at 7:00
  • You should be able to use Gradle for building jni sources with NDK when using a Mac. Commented Oct 6, 2014 at 2:30

1 Answer 1

4

http://ph0b.com/android-studio-gradle-and-ndk-integration/

user ph0b has many SO posts on NDK.

read this person's various posts on the subj ( AS + NDK )

IMO - You can follow strategy 'import NDK proj' from src dirs used for eclipse/NDK android project and AS 0.8.+ will get you almost all the way there with normal "File/Import project" dialog.

After the AS import is done, the NDK stuff will be at:

./root/module/src/main/jni

Java packages will be at

./root/module/src/main/java

Verify that the import to AS did NOT do auto-update on the "Android.mk" file that you input to the import process because you will need it and not any auto gen'd file from AS.

In AS gradle.build file ...

make sure

buildToolsVersion "19.1.0"

and add following as per the earlier links:

   ndk {
        moduleName "audioboo-ogg"
    }
}
flavorDimensions "abi"
productFlavors {
    x86 {
        ndk {
            abiFilter "x86"
        }
    }
    armv7 {
        ndk {
            abiFilter "armeabi-v7a"
        }
    }
}
sourceSets {
    main {
        jni.srcDirs = [] /*disable automatic ndk-build call */
    }

}

task ndkBuild(type: Exec) {
    commandLine '$NDK_HOME/android-ndk-r9[a-z]/ndk-build', '-C', file('src/main/jni').absolutePath
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}

You then will have option of doing CLI NDK build in the jni folder, OR just using gradle integrated build that will use the "ndkBuild" task from gradle file.

3
  • wow, awesome @Robert! thanks for the link (: I'm actually trying to use a bunch cpp files that weren't written for the ndk, but as a library for cross-platform development. How would i go about accessing these from using the ndk?
    – bhbbby
    Commented Oct 5, 2014 at 7:15
  • look for the NDK samples in the ndk download , review the docs and build them. that is how to learn the jni interfaces Commented Oct 5, 2014 at 15:12
  • @RobertRowntree I don't think you understand the OP's question. If I say I want external native sources, it means I don't want them imported into the Android project, because by doing so, those sources will need to be reimported every single time they change. For example, I'm developing a cross-platform, native codebase that needs to be deployed for both iOS/XCode and Android/Studio, and my native code is constantly changing. So I think you can see how an import does not help in the OP's situation.
    – Engineer
    Commented Jul 22, 2015 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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