When i am working with Augmeted Reality for Android Device,i found a great implementation of C++ in java.Because C++ is best for some work like image processing,image detecting etc.However i suffered a lot when worked with AR because i have no knowledge about integration of C/C++ with java.
Now i am sharing this topic
Firstly We must set up our environment for do the work.As a android developer one must install following feature.
- JDK
- Eclipse IDE
- Android SDK Downloader
- Android ADT
- Android SDK platform support
But if we work with C/C++ we must set following two
- Cygwin Environment
- Android NDK
Note that set environment carefully because without proper installtion it's doesn't work.
Cygwin Environment Setup:
For Cygwin Environment Go to http://www.cygwin.com/setup.exe and select "
Install from
the Internet!
" when prompted at "Choose A Download Source
"
in the installer. I recommend not changing the Root Directory in the next
window, and leaving it at "C:\cygwin
". The "Local Package
Directory
" holds the downloaded packages. You may want to keep them with
the downloaded Setup.exe
in the same directory so as to have a
Cygwin installer directory. Choose a download site with a known fast connection
near you.
When the package information is downloaded you will see a hierarchical
browser to select packages. Select the following package from the hierarchy for
download:
All -> Devel -> "make: The GNU version of the
'make' utility"
Select the word "
skip
" to change it to the actual version
number, which is currently 3.81-2. Finish the installation by clicking next.
Your Cygwin environment is fully set-up to work with the avobe topic. Now we set windows path
variable to point to "
C:\cygwin\bin
". by Computer(Righ Click) -> Properties -> Advance System Setting -> Environment Variable -> Edit .Then Copy "C:\cygwin\bin;
" and paste it.NDK Setup:
The Android NDK is an extension to the Android SDK that lets Android
developers build performance-critical parts of their applications in native
code. Download the NDK package from
Unzip the archive and copy the contents into a directory. To be consistent with
our your previous setup i recommend that you put the contents in
the same drive where you put Android SDK and Eclipse. Thus Android SDK and
Android NDK share the same parent directory.
Now set the Environment variable as the same as above with path"C:\Development\Android\android-ndk-r7\"
Now our Environment is set for work.
- Lets start with creating a new project in eclipse.There is two important thing for next work and they are Activity Name and Package Name.
- We create a new folder in project root directory named jni.
- Create a file in jni folder with name hello.c
- Copy the following code and paste to it.#include<string.h>#include<jni.h>JNIEXPORTjstring JNICALLJava_com_integratingstuff_jni_Hello_getString(JNIEnv* env, jobject thiz){return (*env)->NewStringUTF(env, "String Come From C");}
- Now Look at the code.Red mark text indicate your package name and change it to your project package name.Green mark text indicate the Activity name and change it to your activity name and blue color text is function name that will be called later.Orange color text is the return type of this function.
- Create another file in jni folder with name Android.mk.Copy the following code and paste to it.LOCAL_PATH := $(call my-dir)include$(CLEAR_VARS)# Here we give our module name and source file(s)LOCAL_MODULE := helloLOCAL_SRC_FILES := hello.cinclude$(BUILD_SHARED_LIBRARY)
- Create another file in jni folder with name Application.mk.Copy the following code and paste to it.APP_ABI := armeabi armeabi-v7a
- Open your layout from res folder and paste following code to it.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/text1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:text="" /></RelativeLayout>
- Now open your Activity.java file in src folder.Copy the following code and paste to it.package com.integratingstuff.jni;importa ndroid.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Menu ;import android.widget.TextView;import android.widget.Toast;publicclass Hello extends Activity {TextView t;public native String getString();static {System.loadLibrary("hello-jni");}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
activity_hello);
String s = getString();
Log.e("Text Asce-------------------", s);
t = (TextView) findViewById(R.id.text1);
t.setText(s);
Toast.makeText(Hello.
this, s, Toast.LENGTH_LONG).show();
}
}
Now Run Cygwin.exe.And goto the directory where the project reside.If your project reside in "D:\Sample"
then write following command on Cygwin window and execute
cd D:/Sample/YOUR_PROJECT_NAME
Then execute another command "ndk-build" on cygwin window.
After that Build your project in eclipse and run it on device or emulator.
Hope output will Come
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন