Install the latest Android Studio, conan and NDK. Start the studio, chose "Start a new Android Studio project". Fill the form. Don't forget to check "Include C++ support".
Choose minimum SDK.
Choose minimum SDK, empty Activity, fill in activity parameters. In Customize C++ support form chose C++ standard (C++11 is highly recommended) and check the box "Exceptions support".
Go to File->Project Structure
and fill in "Android NDK location".
Add my repository to conan
conan remote add hoxnox https://api.bintray.com/conan/hoxnox/conan
In the app
directory add two files: conanfile.txt
[requires]
boost/1.64.0@hoxnox/stable
[options]
boost:fPIC=True
boost:without_locale=True
boost:without_math=True
boost:without_context=True
[generators]
cmake
and conan_android_profile
[settings]
os=Android
compiler=clang
compiler.version=3.8
compiler.libcxx=libstdc++
[options]
android-toolchain:ndk_path=/opt/android-ndk
[build_requires]
*:android-toolchain/r13b@lasote/testing
Change app/build.gradle
: add into android::defaultConfig:
ndk { abiFilters 'x86', 'armeabi' }
and in the root of the file:
task conanInstall {
def CONAN_ARCHS_MAP = ["x86":'x86', 'armeabi':'armv6']
def api_level = android.defaultConfig.minSdkVersion.mApiLevel
android.defaultConfig.ndk.abiFilters.each {
def arch = CONAN_ARCHS_MAP.get(it, it)
def build_dir = new File("app/conan/$it")
build_dir.mkdirs()
def cmd = "conan install --file ../../conanfile.txt " +
"--profile ../../conan_android_profile " +
"-s arch=${arch} -s os=Android -s os.api_level=${api_level} " +
" --build missing "
print(">> ${cmd} \n")
def sout = new StringBuilder(), serr = new StringBuilder()
def proc = cmd.execute(null, build_dir)
proc.consumeProcessOutput(sout, serr)
proc.waitFor()
println "$sout $serr"
if(proc.exitValue() != 0){
throw new Exception("out> $sout err> $serr" + "\nCommand: ${cmd}")
}
}
}
Now you can use libraries, defined in conanfile.txt
in your native cpp
code:
#include <jni.h>
#include <string>
#include <boost/filesystem.hpp>
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_hoxnox_test_1android_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Fail";
try
{
if (boost::filesystem::exists("/default.prop"))
std::string hello = "Hello from C++";
}
catch (std::exception& e)
{
hello = e.what();
}
return env->NewStringUTF(hello.c_str());
}
Comments
comments powered by Disqus