Forums - Math libraries with Android Studio 2.2.3

4 posts / 0 new
Last post
Math libraries with Android Studio 2.2.3
ozi
Join Date: 3 Jan 17
Posts: 1
Posted: Thu, 2017-01-05 07:20

I'm trying to integrate the Math libraries with Android Studio 2.2. Since the example applications are built for Eclipse I am having a hard time to port them to Android Studio. I would appreciate any help with this. 

  • Up1
  • Down1
swinston
Join Date: 8 May 15
Posts: 9
Posted: Fri, 2017-01-06 15:25

I'd be very happy to help.  Working with Android Studio is a very easy process.  Lemme quickly walk you through some basics.

The goal you have is to tell your compiler where to find the header files and the library files.  Then you need to tell the packaging process where to find the libraries so the apk has them at the end.  In Android Studio, there's 4 different ways to get the ndk to build something.  Thankfully, only 3 of them are officially supported by Google, so I'll detail what to do for each of the three.

First the stable method:

In your gradle, edit your sourcesets closure to resemble this:

android {

sourceSets.main.jniLibs.srcDirs = [ 'RelativePathToLibs' ] 

}

Note that above you need to ensure that the RelativePathToLibs goes from build.gradle's execute directory.  If you have this as your directory structure, you'll be happier, as things will just work:

<RelativePathToLibs>/arm64/lib*.so
<RelativePathToLibs>/armeabi-v7a/lib*.so

Now, use the externalNativeBuild closure:

CMakeLists.txt or Android.mk <-- these are standard, just point them to the right header and libs you will compile and link with.

Done for first two methods.

Now for the experimental-gradle method: <-- note that this is receiving less support from Google and I doubt it will survive externalNativeBuild working so well.

replicate this:

model {
repositories {
libs(PrebuiltLibraries) {
QSML {
headers.srcDirs = [ 'relativePathtoHeaders/include']
}
binaries.withType(SharedLibraryBinary) {
sharedLibraryFile = file("relativePathToLibs/${targetPlatform.getName()}/libQSML-0.15.2.so")
}
... (add one separate library closure per lib you use, headers only need to be included once).
}
}
android {
ndk {
... normal stuff here ...
}
sources.main {
jni {
source { 
... normal stuff here ...
}
dependencies {
library "QSML" linkage "shared"
}
}

 

  • Up0
  • Down0
khcheung
Join Date: 5 Jul 17
Posts: 1
Posted: Thu, 2017-07-06 16:22

Hi,

     I am very new to android, and I have encountered some problems when importing QSML in android studio. I followed the ndk build example, and I got "error:  #error "QSML could not detect or does not support the requested architecture" from qsml_architecture.h. I am not quite sure what's going on here. Is it because I do not copy the libQSML.so file directly to the /system/vendor/lib of the android device as mentioned in the QSML documentation?

Best,

Ed 

  • Up0
  • Down0
swinston
Join Date: 8 May 15
Posts: 9
Posted: Fri, 2017-07-07 00:44

I'm working on a blog post that shows how to use math lib in an AI app using VR.  I also got a recent follow up question in the private messages.  I thought it'd be nicer of me to reply here so any one that has similar problems can get the same help!  Anyway, as I got a followup I thought I'd preview the project here showing how I got integration to work.

My reply above is the generic how to get everything going.  What I'm going to show here, is the actual code.

First the directory structure:

<ProjectDir>/AI-Sampler/build.gradle
<ProjectDir>/AI-Sampler/app/build.gradle
<ProjectDir>/AI-Sampler/app/src/<snip> rest of project

If you observe the install directory for QSML (on OSX they live in /opt/Qualcomm/qsml-0.15.2/android/) You'll notice that the include directories are all nearly the same, and  the main differences are in the libraries.  However, the way that most android libraries' directory structure are setup, it's closer to the setup below:

<ProjectDir>/3rdParty/qsml/libs/armeabi-v7a/libQSML-0.15.2.so
<ProjectDir>/3rdParty/qsml/libs/armeabi-v7a/libQSML-sequential-0.15.2.so
<ProjectDir>/3rdParty/qsml/libs/arm64-v8a/libQSML-0.15.2.so
<ProjectDir>/3rdParty/qsml/libs/arm64-v8a/libQSML-sequential-0.15.2.so

With the libraries copied to there where arm32/lp64/ndk-r11/* is copied to <ProjectDir>/3rdParty/qsml/libs/armeabi-v7a/* and arm64/lp64/ndk-r11/* is copied to <ProjectDir>/3rdParty/qsml/libs/arm64-v8a/*
Then copy any of the include directories to <ProjectDir>/3rdParty/qsml/includes <-- they all appear to be the same other than ilp64's qsml_types.h doesn't define QBLAS_STRICT_BLAS_SPEC.  So if you use the ilp64 libs, then ensure you set a define in your cflags.

Now here's my CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.1)

add_library( native-lib SHARED src/main/cpp/native-lib.cpp )

set( TOP_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../.. )

include_directories( ${TOP_PATH}/3rdParty/qsml/include )

add_library( QSML SHARED IMPORTED )
set_target_properties(
            QSML
            PROPERTIES
            IMPORTED_LOCATION
            ${TOP_PATH}/3rdParty/qsml/libs/${ANDROID_ABI}/libQSML-0.15.2.so
        )


add_library( QSML_sequential SHARED IMPORTED )
set_target_properties(
            QSML_sequential
            PROPERTIES
            IMPORTED_LOCATION
            ${TOP_PATH}/3rdParty/qsml/libs/${ANDROID_ABI}/libQSML-sequential-0.15.2.so
        )


find_library( log-lib log )

target_link_libraries( native-lib ${log-lib} QSML )

And here's my project's build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.gpxblog.ai_sampler"
        minSdkVersion 25
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -frtti -fexceptions"
                targets "native-lib"
                abiFilters "armeabi-v7a", "arm64-v8a"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    splits {
        abi {
            enable true
            reset()
            include 'armeabi-v7a', 'arm64-v8a'
            universalApk false
        }
    }
    sourceSets {
        main {
            jniLibs {
                srcDirs = ['../../3rdParty/qsml/libs']
            }
        }
    }
}

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:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:25.3.1'
    testCompile 'junit:junit:4.12'
}

You'll notice that all I did was take a "new project" created from Android Studio's 2.3.3's wizard with C++ support.  Setup the project the same way i did and you'll have it working.

  • Up3
  • Down0
or Register

Opinions expressed in the content posted here are the personal opinions of the original authors, and do not necessarily reflect those of Qualcomm Incorporated or its subsidiaries (“Qualcomm”). The content is provided for informational purposes only and is not meant to be an endorsement or representation by Qualcomm or any other party. This site may also provide links or references to non-Qualcomm sites and resources. Qualcomm makes no representations, warranties, or other commitments whatsoever about any non-Qualcomm sites or third-party resources that may be referenced, accessible from, or linked to this site.