Android NDK Initial dabble

Source: Internet
Author: User

The approximate points are these steps:

(1) New Android project:


Hellojni.java Code

Package Com.panpass.main;public class Hellojni {static{system.loadlibrary ("Demo-jni");} public void SayHello () {}public native String Jnisay ();}

Mainactivity.java Code

Package Com.panpass.main;import Android.app.activity;import Android.os.bundle;import Android.widget.TextView; Import Com.example.demo.r;public class Mainactivity extends Activity {private TextView mtextview; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main_ activity); Mtextview = (TextView) Findviewbyid (R.id.text_view); Hellojni Demojin = new Hellojni (); Mtextview.settext (Demojin.jnisay ());}}
(2) Generate header file.

In cmd, enter the appropriate directory:

For example, my project is here:



The end is to enter here:


That is, after you open cmd, enter the command:

CD C:\android\workone\Demo\bin\classes


Then enter: Javah-jni com.panpass.main.HelloJni


The. h file is generated.


(3) Create a new JNI file.


DEMO-JNI.C is the concrete implementation of the C-layer function. As follows:

/* Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License") ; * You are not a use 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 appli Cable law or agreed into 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. * */#include <string.h> #include <jni.h>/* This was a trivial JNI example where we use a native method * to Retu RN a new VM String. See the corresponding Java source * file located at: * * APPS/SAMPLES/HELLO-JNI/PROJECT/SRC/COM/EXAMPLE/HELLOJNI/HELLOJN I.java */jstringjava_com_panpass_main_hellojni_jnisay (jnienv* env, Jobje  CT thiz) {  Return (*ENV)->newstringutf (env, "Hello Jni");} 

One of the

Java_com_panpass_main_hellojni_jnisay (jnienv* env,                                                  jobject thiz)
is from the generated. h file. Look at the generated. h file:

/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Com_panpass_main_hellojni * /#ifndef _included_com_panpass_main_hellojni#define _included_com_panpass_main_hellojni#ifdef __cplusplusextern "C "{#endif/* * Class:     com_panpass_main_hellojni * Method:    havereturnfromjni * Signature: (ljava/lang/string;) ljava/lang/string; */jniexport jstring jnicall java_com_panpass_main_hellojni_havereturnfromjni  (jnienv *, Jobject, jstring);/* * Class:     com_panpass_main_hellojni * Method:    Jnisay * Signature: () ljava/lang/string; */jniexport jstring Jnicall Java_com_panpass_main_hellojni_jnisay  (jnienv *, jobject); #ifdef __cplusplus} #endif #endif

The ANDROID.MK code is as follows:

# 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    : = demo-jnilocal_src_files: = Demo-jni.cinclude $ (build_shared_library)
The specific meaning can be Baidu.

(4) Download the NDK build. So library using the NDK.

NDK download to Baidu.

To configure the NDK environment variable:



Which NDK:



In cmd, enter the project root directory:



Then execute:

Ndk-build


Refresh the project and you'll find:


More libs and obj.

Last Run, OK.








Android NDK Initial dabble

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.