Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ndk build and .so file creation android

i am new in android ndk.

I am working on a app which needs java code as well c/c++ code

So, for that i need android ndk.

But i stuck at this point that i cannot run ndk-build which makes the connection between java and c/c++.

So. please someone help me out to solve this problem.

I tried on both windows and linux but got same error.

I got this error when i use ndk-build .

/home/kamal/android-ndk-r8e/build/core/add-application.mk:128: Android NDK:      
Compile thumb : ndk <= native.c
jni/native.c: In function 'Java_com_example_demo_MainActivity_hello':  
jni/native.c:4:3: error: parameter name omitted
jni/native.c:4:3: error: parameter name omitted
jni/native.c:5:10: error: 'env' undeclared (first use in this function)
jni/native.c:5:10: note: each undeclared identifier is reported only once for each              function it appears in
jni/native.c: In function 'Java_com_example_demo_MainActivity_add':
jni/native.c:9:3: error: parameter name omitted
jni/native.c:9:3: error: parameter name omitted
jni/native.c:9:3: error: parameter name omitted
jni/native.c:9:3: error: parameter name omitted
jni/native.c:10:9: error: 'value1' undeclared (first use in this function) 
jni/native.c:10:18: error: 'value2' undeclared (first use in this function)
make: *** [obj/local/armeabi/objs/myjni/native.o] Error 1 
like image 777
Kamal Bhardwaj Avatar asked May 27 '13 10:05

Kamal Bhardwaj


People also ask

What is NDK build?

The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android and provides platform libraries you can use to manage native activities. STEP 1: To use the ndk-tools with android, you need to download and install “NDK tools” from the SDK-manager.

What is difference between Android NDK and SDK?

Android provides Native Development Kit (NDK) to support native development in C/C++, besides the Android Software Development Kit (Android SDK) which supports Java. [TODO] more. NDK is a complex and advanced topics.

What is .so file in Android?

The SO file stands for Shared Library. You compile all C++ code into the.SO file when you write it in C or C++. The SO file is a shared object library that may be dynamically loaded during Android runtime. Library files are larger, often ranging from 2MB to 10MB in size.

What is .MK file in Android?

Overview. The Android.mk file resides in a subdirectory of your project's jni/ directory, and describes your sources and shared libraries to the build system. It is really a tiny GNU makefile fragment that the build system parses once or more.


1 Answers

First of all you are getting this error because you are not declaring parameter in is must to create conection between java and c/c++.

SO, i am sending you my code for your problem

1.First of all create android project in eclipse.

  1. create folder under project click -> click new -> then folder and name it jni.

  2. create one more folder under jni nameing include.

  3. create java class.

  4. code for java class nameing-(MainActivity.java)->

     package com.example.ndk;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    
     public class MainActivity extends Activity {
    
     static {
         System.loadLibrary("myjni");
        }
    
    /**
    * Adds two integers, returning their sum
    */
    public native int add( int v1, int v2 );
    
    /**
    * Returns Hello World string
    */
    public native String hello();
    
    
    @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;
    }
    
        }
    
  5. open command prompt or press window+R.

  6. move to directory- (workspace-> project name -> jni -> include).

  7. run command under this directory.

        javah -classpath <project-name>/bin/classes;<ANDROID_SDK_HOME>\platforms\android-<xx>\android.jar -o HelloJNI.h com.example.test.MainActivity
    
  8. after this we can see "HelloJNI.h" file under include folder.

  9. check "HelloJNI.h" have this lines in it

    JNIEXPORT jint JNICALL Java_com_example_ndk_MainActivity_add(JNIEnv *, jobject, jint, jint);
    
    JNIEXPORT jstring JNICALL Java_com_example_ndk_MainActivity_hello (JNIEnv *, jobject);
    
  10. create new file under jni naming test.c (use this 2 points in pont 10 in this file test.c)

       #include <jni.h>
       #include "include/HelloJNI.h"
    
    JNIEXPORT jstring JNICALL Java_com_example_ndk_MainActivity_hello
        (JNIEnv *env, jobject javaThis) {
        return (*env)->NewStringUTF(env, "Hello");
    }
    
      JNIEXPORT jint JNICALL Java_com_example_ndk_MainActivity_add
          (JNIEnv *env, jobject javaThis, jint value1, jint value2){
    return (value1 + value2);
        }
    
  11. create new file under jni naming Android.mk

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE    := myjni       // from point 5 
     LOCAL_SRC_FILES := test.c     //from point 10 that we creare test.c
    
     include $(BUILD_SHARED_LIBRARY)
    
  12. create new file NDKActivity.java

      package com.example.ndk;
    
      import android.app.Activity;
      import android.view.View.OnClickListener;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.TextView;
    
      public class NDKActivity extends Activity{
    
      Button buttonCalc;
      TextView result;
      EditText value1,value2;
      /** Called when the activity is first created. */
      MainActivity nativeLib;
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
     nativeLib = new MainActivity();
      String helloText = nativeLib.hello();
    
    result = (TextView) findViewById(R.id.result);
    value1 = (EditText) findViewById(R.id.value1);
    value2 = (EditText) findViewById(R.id.value2);
    
    // Update the UI
    TextView outText = (TextView) findViewById(R.id.textOut);
    outText.setText(helloText);
    
     // Setup the UI
    buttonCalc = (Button)this.findViewById(R.id.buttonCalc);
    
    buttonCalc.setOnClickListener(new OnClickListener() {
    
    
    public void onClick(View v) {
     int v1, v2, res = -1;
     v1 = Integer.parseInt(value1.getText().toString().trim());
     v2 = Integer.parseInt(value2.getText().toString().trim());
    
     res = nativeLib.add(v1, v2);
     result.setText(new Integer(res).toString());
     }
    
    
    
     });
     }
         }
    
  13. run ndk-build in command promt

go to project directory-> then, write this command <android-ndk-directory>\ndk-build.cmd and hit enter

after this we can check .so file under obj folder

  1. xml file for NDKActivity.

     <TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Vikram"
    android:textSize="22sp"/>
    <TextView android:id="@+id/textOut"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Output"/>
    
    <EditText
    android:id="@+id/value1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Value 1"
    android:inputType="numberDecimal" />
    
     <TextView android:id="@+id/TextView01"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="+"
         android:textSize="36sp" />
    
          <EditText
       android:id="@+id/value2"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:hint="Value 2"
       android:inputType="numberDecimal" />
    
    <Button android:id="@+id/buttonCalc"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="=" />
    <TextView android:id="@+id/result"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="result"
       android:textSize="36sp" />
    
    
       </LinearLayout>
    
like image 123
Vikram Mahal Avatar answered Nov 01 '22 10:11

Vikram Mahal