关于#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'
}

你自己看错误提示 编译需要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)

根据提供的信息,你在使用Android Studio引入第三方SO和头文件后运行项目时遇到了编译错误。错误信息提示缺少"/Movies/MTest/app/src/main/cpp/app/libs/arm64-v8a/libTest.so"文件,并且没有已知的规则来生成它。

为了解决这个问题,你可以尝试以下步骤:

  1. 确保你将SO库文件(libTest.so)放置在正确的路径:/Movies/MTest/app/libs/arm64-v8a/libTest.so。

  2. 检查CMakeLists.txt文件中的配置是否正确。在CMakeLists.txt文件中,你需要指定要引入的SO库文件的路径。请确保以下代码段中的路径是正确的:

set_target_properties(Test PROPERTIES IMPORTED_LOCATION
        ${PROJECT_SOURCE_DIR}/app/libs/${ANDROID_ABI}/libTest.so)
  1. 检查build.gradle文件中的配置是否正确。在build.gradle文件中,你需要将jniLibs.srcDirs设置为'libs',以确保正确引入SO库文件。请确认以下代码段是否正确:
sourceSets {
    main {
        jniLibs.srcDirs = ['libs']
    }
}
  1. 确保SO库文件的架构与ndk中指定的abiFilters匹配。在build.gradle文件中,你需要确保abiFilters包含的架构与你的SO库文件的架构相同。在你的情况下,应该包含'armeabi-v7a'、'arm64-v8a'和'x86'。请确认以下代码段是否正确:
ndk {
    abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86'
}

按照上述步骤检查并确认配置无误后,重新编译你的项目。希望这些信息对于解决你的问题有所帮助!

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