Android Studio 中的 JNI 文件夹 [英] JNI folder in Android Studio
问题描述
我正在尝试在 Android Studio 中制作 helloy-jni 应用程序,但出现异常
06-27 13:17:19.099 12714-12714/com.example.testjni2 E/AndroidRuntime:致命异常:mainjava.lang.ExceptionInInitializerError在 java.lang.Class.newInstanceImpl(Native Method)在 java.lang.Class.newInstance(Class.java:1319)在 android.app.Instrumentation.newActivity(Instrumentation.java:1071)在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)在 android.app.ActivityThread.access$700(ActivityThread.java:154)在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)在 android.os.Handler.dispatchMessage(Handler.java:99)在 android.os.Looper.loop(Looper.java:137)在 android.app.ActivityThread.main(ActivityThread.java:5306)在 java.lang.reflect.Method.invokeNative(Native Method)在 java.lang.reflect.Method.invoke(Method.java:511)在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)在 dalvik.system.NativeStart.main(本机方法)引起:java.lang.UnsatisfiedLinkError:无法从加载器dalvik.system.PathClassLoader加载hello-jni[dexPath=/data/app/com.example.testjni2-1.apk,libraryPath=/data/app-lib/com.example.testjni2-1]:findLibrary 返回空值在 java.lang.Runtime.loadLibrary(Runtime.java:365)在 java.lang.System.loadLibrary(System.java:535)在 com.example.testjni2.MainActivity.<clinit>(MainActivity.java:9)……还有 15 个
我的结构项目是这样的
我添加了我的 build.gradle 行:
编译文件('libs/armeabi/libhello-jni.so')
但这无济于事.我阅读了 Gradle 和 Android Gradle 插件,但我没有找到有关使用 jni 文件夹的信息 >
我在想什么文件依赖,但它不起作用
我的 hello-jni.c 文件包括
jstring Java_com_example_testjni2_MainActivity_stringFromJNI (JNIEnv *env, jobject thiz){return (*env)->NewStringUTF(env, "Hello from JNI !");}
我的 MainActivity 文件包含
public class MainActivity extends Activity {静止的 {System.loadLibrary("hello-jni");}公共本机字符串 stringFromJNI();@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);字符串文本 = stringFromJNI();Log.i("MainActivity", text);}@覆盖公共布尔 onCreateOptionsMenu(菜单菜单){//给菜单充气;如果它存在,这会将项目添加到操作栏.getMenuInflater().inflate(R.menu.main, menu);返回真;}}
我的 Android.mk 文件包含
LOCAL_PATH := $(call my-dir)包括 $(CLEAR_VARS)LOCAL_MODULE := hello-jniLOCAL_SRC_FILES := hello-jni.c包括 $(BUILD_SHARED_LIBRARY)
请告诉我,我哪里会出错.
Android Studio 0.1.7操作系统 - Windows 7 x64
gradle 构建系统目前不支持捆绑本地库.检查 https://groups.google.com/d/msg/adt-dev/nQobKd2Gl_8/Z5yWAvCh4h4J 对于一些需要添加到 build.gradle 的自定义任务.
请注意,这是 gradle 支持构建原生项目之前的临时解决方法.
I am trying make helloy-jni app in Android Studio and I have exception
06-27 13:17:19.099 12714-12714/com.example.testjni2 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.access$700(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5306)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load hello-jni from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.example.testjni2-1.apk,libraryPath=/data/app-lib/com.example.testjni2-1]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:365)
at java.lang.System.loadLibrary(System.java:535)
at com.example.testjni2.MainActivity.<clinit>(MainActivity.java:9)
... 15 more
My structure project looks like this
I added my build.gradle line:
compile files('libs/armeabi/libhello-jni.so')
But this don't helped. I read Gradle and Android Gradle plugin, but I don't find information about working with jni folder
I am thinking what it file dependencies, but it isn't working
My hello-jni.c file includ
jstring Java_com_example_testjni2_MainActivity_stringFromJNI (JNIEnv *env, jobject thiz){
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
My MainActivity file include
public class MainActivity extends Activity {
static {
System.loadLibrary("hello-jni");
}
public native String stringFromJNI();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String text = stringFromJNI();
Log.i("MainActivity", text);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
My Android.mk file containts
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)
Tell me, please, where I could make a mistake.
Android Studio 0.1.7 OS - Windows 7 x64
The gradle build system does not support bundling native libraries at the moment. Check https://groups.google.com/d/msg/adt-dev/nQobKd2Gl_8/Z5yWAvCh4h4J for some custom tasks you'd need to add to your build.gradle.
Note that this is a temporary workaround until gradle supports building native projects.
这篇关于Android Studio 中的 JNI 文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!