I've been studying how to compile. So dynamic library, feel in the Linux Android source code compiled under too much trouble, so find online find there is no convenient way.
Now finally realized, and now together to summarize the next.
1. Developing JNI in a Windows environment requires the support of the C + + compiler, using GNUstep,http://www.gnustep.org/windows/installer.html.
2. After downloading the installation, verify that it is successful. Open Gnustep->shell, enter the make-v and GCC-V commands.
3. Download NDK, address http://developer.android.com/tools/sdk/ndk/index.html. After downloading, the decompression can be done.
To Configure the NDK environment variable, GNUSTEP is an environment that simulates Linux, open the G:\softinstall\GNUstep\GNUstep\GNUstep.conf file under the GNUstep installation directory and add the following:
copy content to clipboardCode:NDK=/g/softinstall/Android/android-ndk-r8b
export=NDK
Note If you do not know where the NDK directory should be under Linux, you can open the GNUstep command window and enter mount to find the corresponding drive letter.
Verify environment variables, such as.
The above configuration is successful.
4.
Open Eclipse and the new project is named Testjni. Add the following code to the activitycopy content to clipboardCode:Package com.xzw.jni;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.TextView;
Import Android.support.v4.app.NavUtils;
/**
*
* @author Xuzhiwei ([email protected])
* sina:http://weibo.com/xzw1989
*
* Create at 2012-8-30 10:49:45
*/
public class Testjni extends Activity {
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
}
Natvie must declare that it is used to generate C + + code
Public native String hello ();
static{
System.loadlibrary ("Testjni");
}
}The compiled file is generated in the bin directory using the Javah command to generate a C/C + + file header. Such as
The jni/com_xzw_jni_testjni.h is generated under the project directory.
The header file code is as follows:copy content to clipboardCode:/* Don't EDIT this file-it are machine generated */
#include <jni.h>
/* Header for class COM_XZW_JNI_TESTJNI */
#ifndef _included_com_xzw_jni_testjni
#define _included_com_xzw_jni_testjni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:com_xzw_jni_testjni
* Method:hello
* Signature: () ljava/lang/string;
*/
Jniexport jstring Jnicall Java_com_xzw_jni_testjni_hello
(JNIENV *, jobject);
#ifdef __cplusplus
}
#endif
#endifHere we can write C code based on the header filecopy content to clipboardCode:#include <string.h>
#include <jni.h>
jstring
Java_com_xzw_jni_TestJni_hello
(JNIEnv* env, jobject thiz){
return (*env)->NewStringUTF(env, "哈哈完成自动化编译 !");
}Next write Android.mk, the file can be directly from the NDK samples under the Hello-jni JNI file directly from the change on it. Also paste the code ha.copy content to clipboardCode:# Copyright (C) The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You are not a use of this file except in compliance with the License.
# Obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# unless required by applicable or agreed to writing, software
# Distributed under the License is distributed on a "as is" BASIS,
# without warranties or CONDITIONS of any KIND, either express or implied.
# See the License for the specific language governing permissions and
# Limitations under the License.
#
Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_module: = Testjni
Local_src_files: = Testjni.c
Include $ (build_shared_library)All you need is the Local_module and the local_src_files.
Description: Local_module is the module name that describes the module, which is used to call Java, generating the corresponding libtestjni.so
Local_src_files is the source file, multiple file spaces can be separated.
Next, we're going to start compiling and generating so files.
Open the GNUstep Command window, enter the project, enter the $ndk/ndk-build command, you can automatically generate libs/armeabi/libtestjni.so files.
The next step is the Java call. Directly on the codecopy content to clipboardCode:Package com.xzw.jni;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.TextView;
Import Android.support.v4.app.NavUtils;
/**
*
* @author Xuzhiwei ([email protected])
* sina:http://weibo.com/xzw1989
*
* Create at 2012-8-30 10:49:45
*/
public class Testjni extends Activity {
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
TextView TV = new TextView (this);
Tv.settext (hello ()); Here's Hello () is called C
Setcontentview (TV);
}
Public native String hello ();
static{
System.loadlibrary ("Testjni");
}
}
These are the development steps for JNI.
5.JNI Automatic Compilation settings
In the course of our development, we will have to manually compile a file in C + + for a bit of trouble. Here we can use let Eclipse help us compile ourselves.
Right-click on the Properties-->builders-->new-->; of the JNI project Program can see the following content:
argument:--login-c "Cd/e/myworkspace/android/helljni && $NDK/ndk-build"
switch to the Refresh tab page
switch to the Build Options tab
This completes the configuration, click OK to see the console automatically compile the program
Construction of the JNI development environment for Android