Simple JNI Programming for Android

Source: Internet
Author: User

1. First write the class library containing the native method

 Package com.miles.libs;  Public class Hello {        //  load local library    static  {        system.loadlibrary ("Hello" );    }     // Methods in the local library     Public native String hello ();}

2, using Javah to generate A/C + + header file

Javah-classpath Compiling directory class names

Javah-classpath ./bin/classes Com.miles.libs.Hello

Generated header file: com_miles_libs_hello.h

/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Com_miles_libs_hello*/#ifndef _included_com_miles_libs_hello#define_included_com_miles_libs_hello#ifdef __cplusplusextern "C" {#endif/** Class:com_miles_libs_hello * Method:hello * Signature: () ljava/lang/string;*/jniexport jstring jnicall Java_com_miles_libs_hello_hello (jnienv*, jobject); #ifdef __cplusplus}#endif#endif

PS: This file is in the directory where you call the Javah command

3. Create a new JNI file directory to copy the header files, and write C + + files in the directory

code file: com_miles_libs_hello.c

#include <jni.h>"com_miles_libs_hellolibs.h"*env, Jobject Thiz); {    return"Hello JNI! " );}

4. Writing android.mk files

Local_path      : = $ (call my-dir) include         $ (clear_vars) local_module    := hellolocal_src_files: = com_miles_libs_hello.cinclude         $ (build_shared_library)

For the preparation of the Mk file, please check it yourself.

5, compiled in the configuration of the NDK environment to compile

I am using android-ndk-r8c this version compiled, in the high version of the compilation will be error, no in-depth study!

Simple JNI Programming for Android

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.