Giter Site home page Giter Site logo

open3d-core-android's Introduction

Building Open3D core for Android

CMake scripts for cross-compiling the core library of Open3D for Android and linking to it with Android Studio.

Tested with:

  • Windows 10 1809
  • CMake 3.14.1
  • Android Studio 3.4.1 with Gradle plugin 3.4.1
  • Bundled Android NDK r20
  • Open3D 0.7.0

Should work on Linux hosts, too, but it's untested.

Patches

A lot of Open3D revolves around visualisation. Since that code relies on OpenGL and X11 (both of which don't exist on Android) I removed it from being compiled and included. The patches to its CMakeLists.txt can be found in open3d-android.patch. Essentially it:

  • prevents GLEW and GLFW from being built and linked
  • removes the Visualization and Tools modules

The patch file itself was created with git diff > open3d-android.patch.

Compiling

You will need Ninja on your system (and in the PATH variable).

To cross-compile Open3D, simply:

mkdir build & cd build
cmake -G Ninja -DCMAKE_INSTALL_PREFIX=../install .. && cmake --build .

This downloads a specific revision of Open3D and compiles it for all supported NDK ABIs.

  • To change the version of Open3D change the GIT_TAG in the call to ExternalProject_Add for open3d-fetch. You may have to change Open3D's CMakeLists.txt's and create an updated patch file.
  • To specify an NDK path, call CMake with -DANDROID_NDK=path/to/ndk. By default it locates the NDK bundled with Android Studio.

When it's finished, you'll find compiled versions for each Android ABI in the install folder.

Linking

To use the library in Android Studio, take a look at the template CMakeLists.txt. It defines a sample library that finds and links to Open3D. It requires two additional steps:

  1. Set OPEN3D_PATH to the Open3D install. This can be done by specifying an extra parameter to CMake in your build.gradle:
android {
  ...
  defaultConfig {
    ...
    externalNativeBuild {
      cmake {
        ...
        arguments "-DOPEN3D_PATH=path/to/Open3D/install"
      }
    }
  }
}
  1. Add the shared libraries. The template CMakeLists.txt copies the appropriate shared libraries to their own ABI folders under libs/ in the main source directory. To make Gradle find and package them, add them to the source set:
android {
  ...
  sourceSets {
    main {
      jniLibs.srcDirs = ['libs']
    }
  }
}

open3d-core-android's People

Contributors

pezcode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

open3d-core-android's Issues

Failed to checkout tag: 'v0.5.0'

Hi I am trying to build on ubuntu 16.04. i get error Failed to checkout tag: 'v0.5.0'. How can i fix it?
cmake version 3.5.1
git version 2.20.1

cmake --build .
[2/40] Performing download step (git clone) for 'open3d-fetch'
FAILED: cd /home/shome/temp_codes/open3d_android/open3d-core-android/build/CMakeExternals/Source && /usr/bin/cmake -P /home/shome/temp_codes/open3d_android/open3d-core-android/build/CMakeExternals/tmp/open3d-fetch/open3d-fetch-gitclone.cmake && /usr/bin/cmake -E touch /home/shome/temp_codes/open3d_android/open3d-core-android/build/CMakeExternals/Stamp/open3d-fetch/open3d-fetch-download
Cloning into 'origin'...
CMake Error at /home/shome/temp_codes/open3d_android/open3d-core-android/build/CMakeExternals/tmp/open3d-fetch/open3d-fetch-gitclone.cmake:49 (message):
Failed to checkout tag: 'v0.5.0'

ninja: build stopped: subcommand failed.

Need for latest upate

Hi, Can you please update the repository with the latest changes. I am trying to build the Shared library, but every time I end up with different error after making changes to the cmake.

perfect tools

Hello, I am now experiencing the same problem, that is, compiled open3D into a .so library that can be used by Android. I am very happy to see your program, I hope to have further communication with you.

C/C++: ld: error: undefined symbol: open3d::io::CreateImageFromFile(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&)

Whenever i try to call CreateImageFromFile (or any other method from this library that receives an std:string as an argument), during compilation i get hit with the following error:

C/C++: ld: error: undefined symbol: open3d::io::CreateImageFromFile(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&)
I understand that it may be a compilation/ndk problem, but not much more than that.

Here I'll explain how i compiled the project and setup the Android Studio environment:

I am trying to build on Windows 10
I have version 3.14 of CMake installed

I have cloned the project with git clone https://github.com/InstytutXR/open3d-core-android.
I have edited the CMakeLists.txt file to download version 0.9.0 instead of 0.10.0 (keeping 0.10 did not work)
Inside the downloaded project folder, I have run the commands:

mkdir build & cd build
cmake -G Ninja -DANDROID_NDK=C:\Users\vittorio.veloccia\AppData\Local\Android\Sdk\ndk\20.1.5948944 -DCMAKE_INSTALL_PREFIX=../install .. && cmake --build .

This part correclty builds (aka no errors thrown)

Now i created an Android Studio Native C++ project.

My app's gradle.build file looks like this:

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.testo3d'
    compileSdk 33
    ndkVersion "20.1.5948944"

    defaultConfig {
        applicationId "com.example.testo3d"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        externalNativeBuild {
            cmake {
                version '3.22.1'
                arguments "-DOPEN3D_PATH=C:\\Development\\open3d-core-android\\install"
            }
        }
        ndk {
            abiFilters = []
            abiFilters.addAll(ABI_FILTERS.split(';').collect{it as String})
        }
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            version '3.22.1'
        }
    }
    buildFeatures {
        viewBinding true
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

In gradle.properties I have set the following property ABI_FILTERS=armeabi-v7a;arm64-v8a (these 2 are the only ones i have compiled)

My CMakeLists.txt file (inside Android Studio's cpp folder) looks like this:

cmake_minimum_required(VERSION 3.22.1)

project("testo3d")

set(SOURCE_FILES native-lib.cpp)

add_library( ${PROJECT_NAME} SHARED ${SOURCE_FILES})

find_library(log-lib log)

if(NOT OPEN3D_PATH)
    message(FATAL_ERROR "Open3D path not specified")
endif()

set(open3d-abi-path ${OPEN3D_PATH}/open3d-${ANDROID_ABI})

find_package(Open3D 0.9.0 # exact match required (major, minor, patch)
        REQUIRED
        PATHS ${open3d-abi-path}
        # don't look anywhere except in the path(s) we specify
        NO_DEFAULT_PATH
        # ignore the NDK CMAKE_FIND_ROOT_PATH, otherwise all search paths get prefixed with it
        NO_CMAKE_FIND_ROOT_PATH)

find_library(open3d-lib
        Open3D
        PATHS ${Open3D_LIBRARY_DIRS}
        NO_DEFAULT_PATH
        NO_CMAKE_FIND_ROOT_PATH)

file(COPY ${open3d-lib} DESTINATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI})

# Open3D gives us non-existing directories
# CMake doesn't allow adding those as interface include directories
foreach(dir ${Open3D_INCLUDE_DIRS})
    if(NOT EXISTS ${dir})
        list(REMOVE_ITEM Open3D_INCLUDE_DIRS ${dir})
    endif()
endforeach()


add_library(Open3D SHARED IMPORTED)
set_target_properties(Open3D PROPERTIES
        IMPORTED_LOCATION ${open3d-lib})
set_property(TARGET Open3D APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${Open3D_INCLUDE_DIRS})

target_link_libraries(# Specifies the target library.
        ${PROJECT_NAME} PRIVATE
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}

        Open3D)

Lastly, my native-lib.cpp file (inside Android Studio's cpp folder) looks like this:

#include <jni.h>
#include "string"
#include "Open3D/Geometry/RGBDImage.h"
#include "Open3D/IO/ClassIO/ImageIO.h"

using namespace open3d::geometry;

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_o3dnative_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string path = "/emulated/storagew/0/Android/data/com.example.testo3d/files/image.jpg";
    Image colImg = *open3d::io::CreateImageFromFile(path);
    return env->NewStringUTF(path.c_str());
}

Can anyone help me out on understanding what that error might mean and how to resolve it?

Ninja stopped: subcommand failed.

Hi, I am tried following your steps to cross compile open3d core for Android, but I am getting this error.

D:\Experiments\open3d-core-android\build>cmake -G Ninja -DCMAKE_INSTALL_PREFIX=../install .. && cmake --build .
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Experiments/open3d-core-android/build
[1/37] Performing patch step for 'open3d-fetch'
FAILED: CMakeExternals/Stamp/open3d-fetch/open3d-fetch-patch D:/Experiments/open3d-core-android/build/CMakeExternals/Stamp/open3d-fetch/open3d-fetch-patch
cmd.exe /C "cd /D D:\Experiments\open3d-core-android\build\CMakeExternals\Source\open3d-fetch && "C:\Program Files\Git\cmd\git.exe" apply D:/Experiments/open3d-core-android/open3d-android.patch && "C:\Program Files\CMake\bin\cmake.e
xe" -E touch D:/Experiments/open3d-core-android/build/CMakeExternals/Stamp/open3d-fetch/open3d-fetch-patch"
error: No valid patches in input (allow with "--allow-empty")
ninja: build stopped: subcommand failed.

step of Download Open3D failed

i can download the code of open3d without using your scripts.

but i do not how to change the cmakeList file and how to process Open3D-0.7.0.zip

can you help me ,

thanks

image

build failed need help

[4/40] Performing patch step for 'open3d-fetch'
FAILED: CMakeExternals/Stamp/open3d-fetch/open3d-fetch-patch /Users/muxiangchen/Desktop/open3d-core-android-master/build/CMakeExternals/Stamp/open3d-fetch/open3d-fetch-patch
cd /Users/muxiangchen/Desktop/open3d-core-android-master/build/CMakeExternals/Source/open3d-fetch && /usr/bin/git apply /Users/muxiangchen/Desktop/open3d-core-android-master/open3d-android.patch && /usr/local/Cellar/cmake/3.23.2/bin/cmake -E touch /Users/muxiangchen/Desktop/open3d-core-android-master/build/CMakeExternals/Stamp/open3d-fetch/open3d-fetch-patch

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.