【问题标题】:Problem in android studio prebuilt libraryandroid studio预建库中的问题
【发布时间】:2020-03-24 11:04:33
【问题描述】:

我正在使用带有 android studio 的预构建 c++ 库,该库没有被 studio 接收。 我在相同版本的操作系统和工作室中使用 ndk-build 构建了这个 so 文件,但在不同的机器上。 然后在新机器上复制so文件 Studio 正在为 lib 中的新 c++ 函数生成未定义的参考错误。 用 nm 命令检查,so 文件中存在符号。它正在从某个地方挑选旧的库,旧的函数不会产生这样的错误。 试过了:

  1. 缓存无效并重新启动。
  2. 构建干净的项目。
  3. ndk-build 再次只使用这个 so 文件(无 cpp)

但没有成功。

我将 .so 文件放入 myproject/app/libs/x86/libalgos.somyproject/app/jni/libs/x86/libalgos.so

cmakelists:

.
.
.
add_library(algos SHARED IMPORTED)
    set_target_properties(algos PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../../libs/x86/libalgos.so)
    target_include_directories(native-lib PRIVATE ${CMAKE_SOURCE_DIR}/ )
    target_link_libraries( # Specifies the target library.
            native-lib

    # Links the target library to the log library
    # included in the NDK.
            algos ${log-lib})

Android.mk [位置:myproject/app/jni]

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := algos
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libalgos.so
LOCAL_EXPORT_C_INCLUDES := include
include $(PREBUILT_SHARED_LIBRARY)

Application.mk[位置:myproject/app/jni]

APP_CFLAGS += -Wno-error=format-security
APP_ABI := x86
APP_MODULES := algos
APP_OPTIM := debug

build.gradle

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

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "com.example.cryptotrading"
        //minSdkVersion 15
        minSdkVersion 26 //---changed for rhino implementation (invoke customs)
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }

        ndk {
            abiFilters 'x86'
        }
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }

    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    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"
        }
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['jni/', 'libs/', 'app/libs/', '${CMAKE_SOURCE_DIR}/../../..//libs/']
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.preference:preference:1.1.0-alpha05'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.google.android.material:material:1.0.0'
    //implementation files('./libs/js.jar')
    implementation 'org.mozilla:rhino:1.7.12'
    //implementation 'org.apache.commons:commons-text:1.7'
    //compile group: 'commons-codec', name: 'commons-codec', version: '1.7'
    //compile 'com.squareup.okhttp3:okhttp:3.4.1'
    implementation("com.squareup.okhttp3:okhttp:4.4.0")
    implementation 'com.googlecode.json-simple:json-simple:1.1'
    //compile 'com.alibaba.fastjson:1.2.47'
    def room_version = "2.2.4"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    // optional - Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:$room_version"
    // optional - RxJava support for Room
    implementation "androidx.room:room-rxjava2:$room_version"
    implementation 'androidx.annotation:annotation:1.1.0'
    compile "com.androidplot:androidplot-core:1.5.7"
}

【问题讨论】:

  • 未定义的引用意味着VS无法获取函数描述。在“在新机器上复制 so 文件”之后如何将“库”添加到 VS Studio 配置(附加 lib 目录)?

标签: android c++ c android-ndk


【解决方案1】:

此问题是由于项目的不同文件夹中存在旧的 so 文件。 解决者:

  1. 运行 find 命令获取 so 文件的所有位置。删除它们。
  2. 使用 ndk-build 重新生成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    相关资源
    最近更新 更多