Project Setup Steps

This section explains the steps required to prepare your Android Studio project to use the LeiaNativeSDK.

To start, open your existing project or start a new NDK project in Android Studio.

Next connect to the Java module that gives access to the Leia Display and the hardware values associated with that.

In Android Studio, every project contains modules, and both the project and modules contain build.gradle files. The modules are where the local code exists, so any module that will be interacting with the Leia Display needs to be adjusted. You can add the Java module to your own application module:

  • Note: These changes can be found in ​Appendix A​ or in the TeapotsWithLeia sample at: <LeiaNativeSDK dir>/samples/TeapotsWithLeia/classic-teapot/build.gradle

  • In your module’s folder, open the build.gradle file

  • Add the following to the build.gradle file:

repositories {  
    maven {  
        url 'https://leiainc.jfrog.io/leiainc/gradle-release'  
    }  
}
  1. Add a dependency to the Leia library:

dependencies​ {
    ​implementation​ ​'com.leia:leiasdk:4.2.1'
}
  • NOTE: On older projects, “implementation” will need to be changed to “compile”

  • TEST: You can now include Leia specific imports if everything went well. If adding imports from

    the Leia module works, then you have successfully pulled it into your application. Rebuilding

    your project should work without error. Errors like “error: package com.leia.android.lights does

    not exist” show the above did not work.

    Try adding the following code to your application module and if the rebuild works you can move

    on to the next step:

    import​ ​com​.leia.android.lights.LeiaSDK​;

Now that the build.gradle knows how to find the Android module, you want to update your application module to build for the correct target. In this case, the build target supported by Leia currently is “arm64-v8a”. The way we can add this to the module is by doing the following: Note: These changes can be found in ​Appendix A​ or in the TeapotsWithLeia sample at:

<LeiaNativeSDK dir>/samples/TeapotsWithLeia/classic-teapot/build.gradle

  1. Open the build.gradle file which you opened for the previous step to include the Leia Android module.

  2. Add the following to the build.gradle file:

android {
    ...
    defaultConfig {
        ...
        ndk {
            abiFilters ​'arm64-v8a'
        }
        ...
    }
    ...
}

Once the Java module added, it is necessary to link to the native portion. Android Studio supports CMakeLists.txt for building native applications, this is the approach used to add the native LeiaNativeSDK library. First add the header files and libraries into the project by creating a common directory. Then update the CMakeLists.txt file in the source code directory. The steps below show how to do this:

1) In the application root directory, create a folder for common libraries Note: These changes can be found in: <LeiaNativeSDK dir>/samples/TeapotsWithLeia/

A. referenced in future steps as <application root>/<distribution folder>

<application root>/distribution/

2) Inside the <distribution folder>, create a folder named “leia_sdk” Note: These changes can be found in: <LeiaNativeSDK dir>/samples/TeapotsWithLeia/distribution/

A. referenced in future steps as <application root>/<distribution folder>/leia_sdk

<application root>/distribution/lei​a_sdk

3) Inside the leia folder just created, add the folders found in the LeiaNativeSDK folder. Note: These changes can be found in: <LeiaNativeSDK dir>/samples/TeapotsWithLeia/distribution/leia_sdk

<application root>/<distribution folder>/leia_sdk/include 
<application root>/<distribution folder>/leia_sdk/lib

With the directory created, you have added the Leia native libraries to your project. Now you need to tell the project how to find them and include them in the application. This can be done accomplished by the following steps:

Note: These changes can be found in ​Appendix A​ or in the TeapotsWithLeia sample at: <LeiaNativeSDK dir>/samples/TeapotsWithLeia/classic-teapot/build.gradle

1) Update the build.gradle for the application’s module which will be using the Leia library by adding jniLibs.srcDirs with the distribution directory from the previous step. If the build.gradle already contains jniLibs.srcDirs, and the distribution directory from the previous step to the list instead.

android {
    sourceSets {
        main {
            jniLibs.srcDirs = ['./../distribution/leia_sdk/lib']
        }
    }
}

Your application now understands how to interact with the Leia Java module, and it knows how to find the Leia library, and include it in the package. The next step is to make the library visible to the native code, so you can make LeiaNativeSDK library calls. To do this, follow the instructions below:

  1. Include the library in the modules CMakeLists.txt

    Note: These changes can be found in ​Appendix B​ or in the TeapotsWithLeia sample at:

    `<LeiaNativeSDK dir>/samples/TeapotsWithLeia/classic-teapot/src/main/cpp/CMakeLists.txt`

A. Add a Cmake variable for the distribution directory (and change <distribution dir> to your directory) by calling the “set” function, near the top of the file.

set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../../<distribution dir>)

B. Add a library variable to the project by adding a call to “add_library”

add_library(lib_lei​a_sdk​ STATIC IMPORTED)

C. Set the library variable properties by calling “set_target_properties” directly below the “add library” call in (B)

set_target_properties(lib_leia_sdk PROPERTIES IMPORTED_LOCATION
​${distribution_DIR}​/leia_sdk/lib/​${ANDROID_ABI}​/libleiasdk.​so​)

D. Add the include directory for the headers (and change ACTIVITY NAME to the name of activity running your module) by adding a call to “target_include_directories” function.

target_include_directories( <ACTIVITY NAME> ​PRIVATE
${distribution_DIR}​/leia_sdk/i​nclude)

E. Link to the library (and change ACTIVITY NAME to the name of activity running your module) by adding “lib_leia_sdk” to the “target_link_libraries” function

target_link_libraries(<ACTIVITY NAME>
lib_lei​a_sdk)

TEST: Add one of the header files included in <application root>/<distribution folder>/leia_sdk/include and from the Build menu select Rebuild Project to verify the application can now see the include directory

Last updated

Copyright © 2023 Leia Inc. All Rights Reserved