【问题标题】:boost::filesystem::current_path() return "/" only in Native Androidboost::filesystem::current_path() 仅在原生 Android 中返回“/”
【发布时间】:2020-04-22 04:17:20
【问题描述】:

我为 Android 构建了 boost c++ 库。 我已经测试了 boost::chrono,它工作得很好。

但我在使用 boost::filesystem::current_path(); 时遇到问题; 它只返回“/”

有什么我想念的吗? 我使用 boost 1.72

CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)


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


#-----------------------------------------
add_library (boost_system SHARED IMPORTED)

set_target_properties (boost_system PROPERTIES IMPORTED_LOCATION
        ${MY_BOOST_LIBS_DIR}/${ANDROID_ABI}/libboost_system.so
        )

#-----------------------------------------
add_library (boost_chrono SHARED IMPORTED)

set_target_properties (boost_chrono PROPERTIES IMPORTED_LOCATION
        ${MY_BOOST_LIBS_DIR}/${ANDROID_ABI}/libboost_chrono.so
        )

#-----------------------------------------
add_library (boost_filesystem SHARED IMPORTED)

set_target_properties (boost_filesystem PROPERTIES IMPORTED_LOCATION
        ${MY_BOOST_LIBS_DIR}/${ANDROID_ABI}/libboost_filesystem.so
        )

#-----------------------------------------
include_directories (${MY_BOOST_INC_DIR}
        )

#-----------------------------------------
find_library (log-lib log)

#-----------------------------------------
target_link_libraries( # Specifies the target library.
        native-lib

        ${log-lib}

        boost_system
        boost_chrono
        boost_filesystem
        )

build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

def my_boost_dir =  jniLibs("boost.dir")
def my_boost_dir_libs = my_boost_dir + "/libs"
def my_boost_dir_inc  = my_boost_dir + "/include"

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.example.radu"
        minSdkVersion 24
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        //=================================================================//
        externalNativeBuild {
            cmake {

                 cppFlags "-std=c++17"

                // this causes libc++_shared.so to get packaged into .apk
                arguments '-DANDROID_STL=c++_shared'

                // This is used in CMakeLists.txt so our native code can find/use (prebuilt) boost
                arguments '-DMY_BOOST_LIBS_DIR=' + my_boost_dir_libs
                arguments '-DMY_BOOST_INC_DIR='  + my_boost_dir_inc

            }
        }

        ndk {

' abiFilters 'x86' } //================================================= =================// }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    externalNativeBuild {
        cmake {
            path file('CMakeLists.txt')
        }
    }

    sourceSets {
        main {

            jniLibs.srcDirs = [
                    my_boost_dir_libs
            ]
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

def jniLibs(key) {
    def val = null
    if (key == "boost.dir")
        val ="${projectDir}/src/main/jniLibs/ndk_21_boost_1.72.0"
    else if(val == null)
        throw new GradleException("""\
                cant find value '${key}' in file 'local.properties'.  """)
    return val
}

【问题讨论】:

  • 也许这是Android应用程序的默认工作目录?您是否在任何地方修改路径?您是否尝试过查看(详尽的)Android 文档?
  • @Someprogrammerdude 我不修改路径。我可以从托管源代码中很好地获取目录。
  • Java 代码中的 CWD 和 C++ 代码有什么区别?请edit您的问题告诉我们。我们不仅需要实际输出,还需要预期输出。请花一些时间刷新How to Askthis question checklist

标签: c++ android-studio boost android-ndk


【解决方案1】:

这是适用于 Android 应用的 CWD。如果您想要应用程序的目录,则需要使用 Java API,例如 https://developer.android.com/reference/android/content/pm/ApplicationInfo#dataDir

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多