Windows Android JNI usage and environment setup

Source: Internet
Author: User

Read a lot of articles on the Internet, some too old, some do not say is not very clear, this step by step start using the Android JNI method

Reference: http://bbs.51cto.com/thread-948244-1.html###

Http://www.cnblogs.com/kissazi2/p/3298884.html

Using GNUStep as the C + + compiler, the reason to choose this is that the compiler is small and will be downloaded shortly.


(i) Download and install the compilation environment

1. Open the site http://www.gnustep.org/

After entering, download the installation, from top to bottom in order to install the line


Start shell after successful installation, method


Open Gnustep->shell, enter the make-v and GCC-V commands to verify that the installation was successful


Download DNK The address i downloaded is: https://dl.google.com/android/ndk/android-ndk64-r10-windows-x86.zip

See the article http://www.cnblogs.com/yaotong/archive/2011/01/25/1943615.html there are many native addresses

Google's address is not open, but can download directly, thank you Ah! Oh!

Note: Download down after decompression encountered said file repeat whether overwrite, I choose is overwrite, lowercase name overwrite uppercase name; (not in-depth study here, only a look at two files)

To configure the NDK environment variable, GNUSTEP is an environment that simulates Linux, open the D: \gnustep\gnustep\gnustep.conf file under the GNUstep installation directory and add the following:

copy content to clipboardNdk=/d/android-ndk64-r10-windows-x86/android-ndk-r10
Export=ndk

The NDK's location is where you unzip and place it after downloading:



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.

To this environment deployment complete

(ii) Establishment of engineering, writing example code

1. Establish engineering COM.EXAMPLE.TEST4;

Add Class Jnitest

Package Com.example.test4;public class Jnitest {public native final String init (), public native final void init2 (int a,i NT b);}    The code in main in the public class Mainactivity extends Activity {static{system.loadlibrary ("Jnitest");        } @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main);        Jnitest jnitest = new Jnitest ();         String s = jnitest.init (); }}



2. Start-run-cmd, to the project catalog

Using Cmd:f:\workspace2\test4>javah-classpath./src-d./jni-jni com.example.test4.JniTest

-classpath < paths > Paths for loading Classes-D < directory > output directory-jni generate JNI-styled header files (default)

Add com_example_test4_jnitest.c android.mk files manually

# Copyright (C) The Android Open Source project## Licensed under the Apache License, Version 2.0 (the "License"); # yo U may not use this file except in compliance with the license.# your may obtain a copy of the License at## http://www.a      pache.org/licenses/license-2.0## unless required by applicable law or agreed to in writing, software# distributed under th E License is distributed on a "as is" basis,# without warranties OR CONDITIONS of any KIND, either express or implied.# S EE the License for the specific language governing permissions and# limitations under the License. #LOCAL_PATH: = $ (Call my -DIR) include $ (clear_vars) local_module    : = jnitestlocal_src_files: = Com_example_test4_jnitest.cinclude $ (BUILD_ Shared_library)

Only one interface is implemented here.

#include <string.h> #include <jni.h> #include "com_example_test4_jnitest.h" Jstringjava_com_example_ Test4_jnitest_init  (jnienv* env, Jobject thiz) {          return (*env)->newstringutf (env, "My Test Jin Interface");}


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.

F5 Refresh Project

F11debug Eclipse test4 Project, you can see the call in Mainactivity in String s = jnitest.init (); Is the string written in the C file.

(3) Automatic compilation

Straight from the http://bbs.51cto.com/thread-948244-1.html### CP.

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



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Windows Android JNI usage and environment setup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.