Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNI folder in Android Studio

Tags:

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

TestJni2Project

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

like image 295
AndreyNik Avatar asked Jun 27 '13 10:06

AndreyNik


People also ask

What is the use of JNI in Android?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).

What is NDK and JNI?

JNI is just the way that Java handles calling into native/C++ code, and calling back into Java from there. It has nothing to say about Android - it is a Java language feature. The Android NDK is a way to write Android applications using code called by JNI.

What is JNI libs?

jni/libs folder is where your shared library files are built from the C/C++ sources. Your native code gets compiled and depending on the value you had set in your application.mk file for the parameter APP_ABI: = <all | x86 | armv7a | armeabi-v7 | mips>


2 Answers

Nowadays there is an easier method than the accepted answer.

All you have to do is create a folder called "jniLibs" under your /src/main directory (ie, /app/src/main/jniLibs) and place your .so files there. It will look like this:

app
   |
   src
      |
     main
        |
        jniLibs
            |
            armeabi
            |   |
            |   your_lib_compiled_for_armeabi.so
            |
            armeabi-v7a
            |   |
            |   your_lib_compiled_for_v7a.so
            |
            x86
            |   |
            |   your_lib_compiled_for_x86.so

Your .so files should be picked up now.

like image 144
Bitcoin Cash - ADA enthusiast Avatar answered Oct 02 '22 22:10

Bitcoin Cash - ADA enthusiast


There is other way to import so to the android project.

  1. create the lib dir and armeabi dir like this, and copy so to it.
    |-project
    |--lib
    |----armeabi
    |------libhello-jni.so
  2. zip the dir lib.
  3. put the zip to the libs dir of project, and change .zip to .jar.
  4. add code to the dependencies part of build.gradle.
    compile fileTree(dir:'libs', include:'lib.jar')
like image 37
spmno Avatar answered Oct 02 '22 22:10

spmno