首页 > 解决方案 > 如何使用从一个 Android 应用程序生成的 .so 文件,并在另一个应用程序中使用它?

问题描述

我是 NDK 开发的新手,我创建了支持 C++ 的项目。但是我刚刚添加了 1 个 cpp 类并生成了 .SO 文件。并尝试在另一个项目中访问该 so 文件。我参考了许多stackoverflow 链接、博客和 Google 开发者网站。但无法解决它。这是我的代码。

本地属性

ndk.dir=/home/android/Android/Sdk/ndk/20.1.5948944
sdk.dir=/home/android/Android/Sdk

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.4.1)

# 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.
        hello_world_lib

        # Sets the library as a shared library.
        SHARED

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

# 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.
        hello_world_lib

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

src/main/cpp/hello_world_lib.cpp

#include <jni.h>
#include <string>
#include <bitset>
#include "HelloWorld.h"

extern "C" JNIEXPORT jstring JNICALL
Java_com_dc_testapplication_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

extern "C" JNIEXPORT jint JNICALL
Java_com_dc_testapplication_MainActivity_getSumOfValues(JNIEnv *env,
                                                        jobject,jint a ,jint b,jstring sign){

    // working Solution
    const char *nativeString = env->GetStringUTFChars(sign, 0);


    HelloWorld helloWorld;
    helloWorld.setValues((int)a,(int)b);
    helloWorld.setOperationType(*nativeString);

    jint mData = (jint)helloWorld.getSumOf2Values();
    return mData;
}

在构建/运行应用程序后,我从这里得到了这些文件。app/build/intermediates/cmake/debug/obj/{abis}/{nameoflib}.so

现在我需要在另一个项目中访问这些 .so 文件。它的包名是com.dc.usendklib。现在这是我的新项目代码。

app/libs/{abi 文件夹名称}/.so 文件。src/main/jniLibs/{abi 文件夹名称}/.so

src/main/jniLibs/Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := hello_world_lib-prebuilt
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libhello_world_lib.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

src/main/jniLibs/Application.mk

APP_ABI := armeabi-v7a arm64-v8a x86

应用程序的gradle

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

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    sourceSets.main {
        jniLibs.srcDir 'src/main/jniLibs' //set .so files location to libs
        jni.srcDirs = [] //disable automatic ndk-build call
    }

    defaultConfig {
        applicationId "com.dc.usendklib"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

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

//    sourceSets {
//        main {
//            // let gradle pack the shared library into apk
//            jniLibs.srcDirs = ['src/main/jniLibs','src/main/CPP']
//        }
//    }

//    sourceSets.main {
//        jniLibs.srcDir 'src/main/jniLibs'
//    }

//    externalNativeBuild {
//        cmake {
//            path "src/main/cpp/CMakeLists.txt"
//            version "3.10.2"
//        }
//    }

}

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.1.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'
}

当我运行它时,我收到了这个错误。

2020-01-03 11:44:54.946 17235-17235/com.dc.usendklib E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.dc.usendklib, PID: 17235
    java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String com.dc.usendklib.MainActivity.stringFromJNI() (tried Java_com_dc_usendklib_MainActivity_stringFromJNI and Java_com_dc_usendklib_MainActivity_stringFromJNI__)
        at com.dc.usendklib.MainActivity.stringFromJNI(Native Method)
        at com.dc.usendklib.MainActivity.onCreate(MainActivity.kt:14)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

MainActivity.kt

package com.dc.usendklib

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Example of a call to a native method
        sample_text.text = stringFromJNI()
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    external fun stringFromJNI(): String

    companion object {

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

现在我的问题是,

  1. 我生成和选择 .so 文件的方式是正确的方式吗?

  2. JNI 类方法使用 Java_{package}_method,所以在生成 .so 之后,是否可以在另一个项目中使用该文件

  3. 如果是,请告诉我如何。任何帮助将不胜感激。提前致谢。

标签: javaandroidc++cmakeandroid-ndk

解决方案


是的,您可以将您的 so 文件访问到其他项目。但您的项目目录必须与旧项目相同。在您的情况下,项目目录必须是这样的:

com/dc/testapplication/MainActivity.classMainActivity.class必须有getSumOfValues方法。


推荐阅读