tzutalin / dlib-android-app

:dragon: Android app to demo dlib-android(https://github.com/tzutalin/dlib-android). Use the prebuilt shared-lib built from dlib-android
Apache License 2.0
683 stars 246 forks source link

Compiling dlib for android #58

Closed ghost closed 3 years ago

ghost commented 4 years ago

Hi @tzutalin It would be of great help if you can let me know how to compile the .so files for android.

Settings; Android SDK: 26.1.1 Gradle: 3.5.2 NDK: 20.x (latest stable version)

Using CMakeLists.txt with the following settings on a Native C/C++ Prj

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.dummy.oloidtestapp"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -fexceptions -O3"
                arguments '-DBUILD_TESTING=OFF', '-DANDROID_STL=c++_shared', '-DBUILD_SHARED_LIBS=ON'
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
    flavorDimensions "1.0.8"
    productFlavors {
        demo {
            externalNativeBuild {
                dimension "1.0.8"
                applicationIdSuffix ".demo"
                versionNameSuffix "-demo"
                cmake {

                    // Specifies which native libraries or executables to build and package
                    // for this product flavor. The following tells Gradle to build only the
                    // "native-lib-demo" and "my-executible-demo" outputs from the linked
                    // CMake project. If you don't configure this property, Gradle builds all
                    // executables and shared object libraries that you define in your CMake
                    // (or ndk-build) project. However, by default, Gradle packages only the
                    // shared libraries in your APK.
                    targets "dlib"
                            // You need to specify this executable and its sources in your CMakeLists.txt
                            // using the add_executable() command. However, building executables from your
                            // native sources is optional, and building native libraries to package into
                            // your APK satisfies most project requirements.
                            //"dlib"
                }
            }
        }
    }
}

I am trying to compile dlib for android and am getting build erros as follows:

2019-12-11 16:27:48.159 938-1893/? E/cutils: Failed to open(/data/misc/profiles/cur/150/com.dummy.oloidtestapp.demo/primary.prof): No such file or directory
2019-12-11 16:27:48.159 938-1893/? E/installed: Failed to prepare /data/misc/profiles/cur/150/com.dummy.oloidtestapp.demo/primary.prof: No such file or directory
2019-12-11 16:27:48.159 1408-1604/? E/ArtManagerService: Failed to prepare profile for com.dummy.oloidtestapp.demo:/data/app/com.dummy.oloidtestapp.demo-ljbEeOH94LvogZTxbyZy2g==/base.apk
2019-12-11 16:27:48.853 4318-29029/? E/ThirdPartyChatAppSvc: ThirdPartyChatAppService: Intent { act=android.intent.action.PACKAGE_REMOVED dat=package:com.dummy.oloidtestapp.demo flg=0x4000010 cmp=com.google.android.wearable.app/com.google.android.clockwork.companion.messaging.ThirdPartyChatAppService (has extras) }
2019-12-11 16:27:48.894 1408-1408/? E/SDAgentPackageStateReceiver: Not going to handle 'com.dummy.oloidtestapp.demo'!
2019-12-11 16:27:49.453 4318-29029/? E/ThirdPartyChatAppSvc: ThirdPartyChatAppService: Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.dummy.oloidtestapp.demo flg=0x4000010 cmp=com.google.android.wearable.app/com.google.android.clockwork.companion.messaging.ThirdPartyChatAppService (has extras) }
2019-12-11 16:27:50.089 32451-32451/? E/SPPClientService: [PackageInfoChangeReceiver] [handlePkgRemovedEvent] PackageName : com.dummy.oloidtestapp.demo, true, false
2019-12-11 16:27:50.191 1408-1408/? E/SDAgentPackageStateReceiver: Not going to handle 'com.dummy.oloidtestapp.demo'!
2019-12-11 16:27:50.292 29064-29064/com.dummy.oloidtestapp.demo A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xffffffffffffffe8 in tid 29064 (oidtestapp.demo), pid 29064 (oidtestapp.demo)
2019-12-11 16:27:50.340 3641-26108/? E/id.app.launche: Failed to open APK '/data/app/com.dummy.oloidtestapp.demo-7KIhcikoV_6sr9aMA_1k-Q==/base.apk' I/O error
2019-12-11 16:27:50.340 3641-26108/? E/ResourcesManager: failed to add asset path /data/app/com.dummy.oloidtestapp.demo-7KIhcikoV_6sr9aMA_1k-Q==/base.apk
2019-12-11 16:27:50.341 3641-26108/? E/id.app.launche: Failed to open APK '/data/app/com.dummy.oloidtestapp.demo-7KIhcikoV_6sr9aMA_1k-Q==/base.apk' I/O error
2019-12-11 16:27:50.341 3641-26108/? E/ResourcesManager: failed to add asset path /data/app/com.dummy.oloidtestapp.demo-7KIhcikoV_6sr9aMA_1k-Q==/base.apk
2019-12-11 16:27:50.341 3641-26108/? E/id.app.launche: Failed to open APK '/data/app/com.dummy.oloidtestapp.demo-7KIhcikoV_6sr9aMA_1k-Q==/base.apk' I/O error
2019-12-11 16:27:50.341 3641-26108/? E/ResourcesManager: failed to add asset path /data/app/com.dummy.oloidtestapp.demo-7KIhcikoV_6sr9aMA_1k-Q==/base.apk
2019-12-11 16:27:50.510 29188-29188/? A/DEBUG: pid: 29064, tid: 29064, name: oidtestapp.demo  >>> com.dummy.oloidtestapp.demo <<<
2019-12-11 16:27:51.000 29188-29188/? A/DEBUG:     #00 pc 000000000039f368  /data/app/com.dummy.oloidtestapp.demo-ljbEeOH94LvogZTxbyZy2g==/lib/arm64/libdlib.so (dlib::logger::global_data::global_data()+372)
2019-12-11 16:27:51.000 29188-29188/? A/DEBUG:     #01 pc 000000000039e31c  /data/app/com.dummy.oloidtestapp.demo-ljbEeOH94LvogZTxbyZy2g==/lib/arm64/libdlib.so (dlib::logger::get_global_data()+88)
2019-12-11 16:27:51.000 29188-29188/? A/DEBUG:     #02 pc 00000000003a1ad4  /data/app/com.dummy.oloidtestapp.demo-ljbEeOH94LvogZTxbyZy2g==/lib/arm64/libdlib.so (dlib::logger::logger(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char>> const&)+64)
2019-12-11 16:27:51.000 29188-29188/? A/DEBUG:     #03 pc 000000000029d428  /data/app/com.dummy.oloidtestapp.demo-ljbEeOH94LvogZTxbyZy2g==/lib/arm64/libdlib.so
2019-12-11 16:27:51.000 29188-29188/? A/DEBUG:     #04 pc 000000000029d4c4  /data/app/com.dummy.oloidtestapp.demo-ljbEeOH94LvogZTxbyZy2g==/lib/arm64/libdlib.so
2019-12-11 16:27:51.001 29188-29188/? A/DEBUG:     #32 pc 0000000000012420  /dev/ashmem/dalvik-classes2.dex extracted in memory from /data/app/com.dummy.oloidtestapp.demo-ljbEeOH94LvogZTxbyZy2g==/base.apk!classes2.dex_29064_29064 (deleted) (com.dummy.oloidtestapp.MainActivity.<clinit>+20)
2019-12-11 16:27:51.002 29188-29188/? A/DEBUG:     #54 pc 00000000000e3030  /dev/ashmem/dalvik-classes.dex extracted in memory from /data/app/com.dummy.oloidtestapp.demo-ljbEeOH94LvogZTxbyZy2g==/base.apk_29064_29064 (deleted) (androidx.core.app.CoreComponentFactory.instantiateActivity)

Using this to load the library

companion object {

        // Used to load the 'native-lib' library on application startup.
        init {
            //System.loadLibrary("dlib")
        }
    }

Thank you again for your help. Best Chandra

aboozaid commented 4 years ago

to build the library from scratch 1- clone it from https://github.com/tzutalin/dlib-android 2- run envsetup first by python envsetup 3- ensure you have an appropriate NDK version 15c works well 4- set ndk path as global reference (bash_profile/environment) 5- run python build.py 6- copy libs folders to your android project

don't have to call loadLibrary it's already handled by dlib