本文介绍了如何将JNI与AAR库一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Android库(.aar文件),我需要使用JNI. (我很清楚Google尽量不要使用JNI/NDK,但在这种情况下是不可能的).

I am creating an Android library (.aar file) and I need to use JNI. (I am very well aware of Google's discouragement of using JNI/NDK if possible, but in this case, it's not possible).

我从一个独立的hello-jni示例APP开始(首先学习JNI),其中包含以下文件:

I started with a standalone hello-jni example APP (to first learn JNI), with the following files:

HelloJni.java

public class HelloJni extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        TextView  tv = new TextView(this);
        tv.setText( stringFromJNI() );
        setContentView(tv);
    }

    public native String  stringFromJNI();

    static {
        System.loadLibrary("hello-jni");
    }
}

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.c

include $(BUILD_SHARED_LIBRARY)

Application.mk

APP_ABI := all

hello-jni.c

#include <string.h>
#include <jni.h>

jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
    return (*env)->NewStringUTF(env, "Hello from JNI!");
}

以下内容可作为应用程序(apk)很好地构建,并且可以在我的设备上运行它,并显示"JNI的Hello!".如预期的那样.

The following builds fine as an app (apk) and I am able to run it on my device, which prints "Hello from JNI!" as expected.

现在,我做了同样的事情,但是我创建了一个库项目来生成aar,而不是apk.除了HelloJni.java,我将所有文件都保持不变,将其更改为以下内容:

Now, I did the same thing, but instead of an apk, I built a library project to produce an aar. I kept all the files the same except HelloJni.java, which I changed to the following:

HelloJni.java

public class HelloJni {
    public native String stringFromJNI();

    static {
        System.loadLibrary("hello-jni");
    }
}

aar构建良好,但是当我将aar导入一个单独的应用程序项目中并尝试在我的设备上运行它时,它在应用程序启动时崩溃,并且我收到以下错误logcat消息:

The aar builds fine, but when I import the aar into a separate app project, and try to run it on my device, it crashes on app start and I get the following error logcat message:

这个"libhello-jni.so"文件到底是什么?为什么我需要它?我能够以apk的形式完美运行.但是,谁能解释为什么当我将它放入aar并将其导入到应用程序项目中以使用它时为什么它不起作用?我是否缺少将其放入库(和使用库)所需的一些其他步骤?谢谢!

What in the world is this "libhello-jni.so" file? And why do I need it? I was able to run this perfectly fine as an apk. But can anyone explain why it doesn't work when I make it into an aar, and import it to an app project to use it? Am I missing some additional steps needed to make it into a library (and use it)? Thanks!

这就是我将aar导入到我的应用程序中的方式.1.单击文件"->新建"->新模块".2.选择导入.JAR或.AAR包"作为模块类型.3.将我的aar文件设置为新模块4.然后打开文件"->项目结构"5.在依赖项"选项卡中,添加模块依赖项"并选择我的aar文件如果这不是将aar导入到我的应用程序的好方法,请也告诉我.谢谢!

This is how I imported my aar to my app.1. Click "File" -> "New" -> "New Module".2. Select "Import .JAR or .AAR Package" as module type.3. Set my aar file as new module4. And then open "File" -> "Project Structure"5. In the "Dependencies" tab, add "Module dependency" and select my aar fileIf that's not a good way to import aar to my app, then also please let me know. Thank you!

推荐答案

结果是,我不得不在AAR模块目录中的build.gradle文件中添加一些NDK配置.

Turns out I had to add some NDK config in my build.gradle file inside my AAR module directory.

build.gradle:

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"

        // This determines the .so filename
        ndk {
            moduleName "hello-jni"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

在build.gradle中不添加该文件将生成一个.so文件,该文件默认为您的项目名称,在我的情况下为"libsample-aar.so".使用上面的配置,然后生成的.so文件将是"libhello-jni.so".

Not having that added in build.gradle will produce an .so file that's defaulted to your project's name, in my case "libsample-aar.so". With that config above, the generated .so file will then be "libhello-jni.so".

这篇关于如何将JNI与AAR库一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 11:42
查看更多