Arman_ 2023-07-03 18:31 采纳率: 28.6%
浏览 64
已结题

关于#AndroidJNI#的问题,如何解决?

问题:使用Android Studio 引入第三方SO和头文件后运行项目时编译报错。这个问题我在ChatGPT上问过 搞了好半天也都进展

希望帮忙看看 感激不尽 感激不尽,顺利编译通过后 费用给你

项目地址:https://github.com/52932/TestWork  分支:main

SO库文件路径是:/Movies/MTest/app/libs/arm64-v8a/libTest.so。

  1. 错误信息是:
Build command failed.
Error while executing process /Library/Android/sdk/cmake/3.22.1/bin/ninja with arguments {-C /Movies/MTest/app/.cxx/Debug/4v675r6m/arm64-v8a mtest}
ninja: Entering directory `/Movies/MTest/app/.cxx/Debug/4v675r6m/arm64-v8a'

ninja: error: '/Movies/MTest/app/src/main/cpp/app/libs/arm64-v8a/libTest.so', needed by '/Movies/MTest/app/build/intermediates/cxx/Debug/4v675r6m/obj/arm64-v8a/libmtest.so', missing and no known rule to make it
  1. cmake文件路径是:/Movies/MTest/app/src/main/cpp/CMakeLists.txt。内容如下:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.22.1)
# Declares and names the project.

project("mtest")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        mtest

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        CallPluginLib.cpp)

#配置加载头文件
include_directories(../cpp/include)

#动态方式加载
add_library(Test SHARED IMPORTED )
#引入第三方.so库
set_target_properties(Test PROPERTIES IMPORTED_LOCATION
        ${PROJECT_SOURCE_DIR}/app/libs/${ANDROID_ABI}/libTest.so)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        mtest
        Test

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})
  1. build.gradle文件内容如下:
plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.mtest'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.mtest"
        minSdk 21
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags '-std=c++17'
            }
        }
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86'
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }


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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
}
  • 写回答

3条回答 默认 最新

  • 2301_76247172 2023-07-03 19:26
    关注

    你自己看错误提示 编译需要libTest.so,而编译器找的是这个路径 /Movies/MTest/app/src/main/cpp/app/libs/arm64-v8a/libTest.so
    这是你配置的

    set_target_properties(Test PROPERTIES IMPORTED_LOCATION
            ${PROJECT_SOURCE_DIR}/app/libs/${ANDROID_ABI}/libTest.so)
    

    实际上 路径在 /Movies/MTest/app/libs/arm64-v8a/libTest.so

    那么修改成

    set_target_properties(Test PROPERTIES IMPORTED_LOCATION
            ${PROJECT_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/libTest.so )
    

    或者使用绝对路径

    set_target_properties(Test PROPERTIES IMPORTED_LOCATION
            /Movies/MTest/app/libs/arm64-v8a/libTest.so)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 7月11日
  • 已采纳回答 7月3日
  • 修改了问题 7月3日
  • 创建了问题 7月3日

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line