Using boost for Android
Top level 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-experimental:0.9.3'
// classpath 'com.android.tools.build:gradle:2.3.0'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
//-----------------------------------
// def my_abis() { return ["x86", "x86_64", "armeabi-v7a", "mips", "armeabi", "arm64-v8a", "mips64"] } // some abis (eg "x86_64") require android platform_ver: 21 or newer
def my_abis() { return ["armeabi-v7a"] }
def my_compileSdkVersion(){ return 26 }
def my_buildToolsVersion() { return '26.0.0'}
def my_minSdkVersion(){ return 16}
def my_targetSdkVersion() { return my_compileSdkVersion().asType(Integer)}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
def want_gcc(){ return true}
def tc_family() { return want_gcc()? "gnu" : "llvm"} // "gnu" (for gcc) or "llvm" (for clang)
def tc_name() { return want_gcc()? "gcc" : "clang" } // "gcc" or "clang"
def tc_ver() {return want_gcc()? "4.9" : "3.5"}// "3.6", "3.7" (for clang) or "4.9" , "5" (for gcc)
def std_lib () { return want_gcc()? "gnustl_shared" : "c++_shared"} // "gnustl_shared" or "c++_shared"
//---------------------
//-----------------------
def platform_ver(){return 9}
def platform_dir() { return "${ndk_dir()}/platforms/android-${platform_ver()}" }
def boost_dir() { return local_props("boost.dir") }
def boost_inc_dir() { return "${boost_dir()}/include" }
def boost_lib_dir() { return "${boost_dir()}/libs" }
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
//-------------------------------------------------
def local_props(key) { // version = "ndk.dir" or "ndk.dir.nightly"
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def val = properties.getProperty(key, null)
if (val == null)
throw new GradleException("""\
cant find value '${key}' in file 'local.properties'. """)
return val
}
local.properties_example
ndk.dir=/home/declan/Documents/zone/mid/lib/android/sdk/ndk-bundle
sdk.dir=/home/declan/Documents/zone/mid/lib/android/sdk
boost.dir=../../build/boost/1.64.0
build.grade for app
apply plugin: 'com.android.model.application'
//-------------------------------
def my_inc = [
boost_inc_dir()
]
//-------------------------------------------------
def my_incs = my_inc.collect{ "-I" + file(it)}
//---------------------------------------------------
model {
android {
compileSdkVersion = my_compileSdkVersion()
buildToolsVersion = my_buildToolsVersion()
defaultConfig {
minSdkVersion.apiLevel = my_minSdkVersion()
targetSdkVersion.apiLevel = my_targetSdkVersion()
applicationId = 'com.testboost'
versionCode = 1
versionName = '1.0'
}
ndk {
moduleName = 'test_boost'
//------------------------------
platformVersion = platform_ver() // defaultConfig.minSdkVersion.apiLevel.asType(Integer) // keep equal to minSdk
toolchain = tc_name()
toolchainVersion = tc_ver()
stl = std_lib()
//--------------------------------
cppFlags.addAll(['-std=c++14', '-frtti', '-fexceptions'])
cppFlags.addAll(my_incs)
ldLibs.addAll(['log'])
abiFilters.addAll(my_abis())
}
sources {
main {
jni {
exportedHeaders
{
srcDirs "src/main/jni"
}
dependencies {
library 'boost_chrono' linkage 'shared'
library 'boost_system' linkage 'shared'
}
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles.add(file('proguard-android.txt'))
}
}
}
repositories {
libs(PrebuiltLibraries) {
boost_chrono { binaries.withType(SharedLibraryBinary) { sharedLibraryFile = file("${boost_lib_dir()}/${targetPlatform.getName()}/${tc_family()}-${tc_ver()}/libboost_chrono.so") } }
boost_system { binaries.withType(SharedLibraryBinary) { sharedLibraryFile = file("${boost_lib_dir()}/${targetPlatform.getName()}/${tc_family()}-${tc_ver()}/libboost_system.so") } }
}
}
android.lintOptions { // was getting lint error that apparently was a problemm with lint itself ... maybe doesnt know syntax of new gradle experimental properly yet?
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.0.1'
}
//----------------------------------------------------------------------------------------
Original build.grade for app
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "au.com.cedanet.myapplication"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.1'
testCompile 'junit:junit:4.12'
}