Android JNI sample code

Source: Internet
Author: User

In this article, we will introduce how to use Android JNI through several steps, hoping to help you.

You can modify the Android mobile phone operating system in the android modulo simulator to add various functions to meet your needs. So today we will bring you the android JNI-related usage methods to facilitate your interpretation of this system. In Android applications (Dalvik VM), JNI (Java Native Interface) is used to call the shared libraries developed by C/C ++.

  • Overview of various functions of the android Java package
  • Tips for using the android call Platform
  • Overview of the basic content of the android program architecture
  • How Android judges the network status
  • Android unit test source code

Development Environment

  1. (Eclipse + Android ADT)
  2. Windows XP SP3
  3. JDK 1.6.0.12
  4. VMware + Ubuntu Linux 8.04

Steps for using Android JNI (1) Create an android project in eclipse

  1. Project name: jnitest
  2. Package name com. Ura. Test
  3. Activity name jnitest
  4. Application name: jnitest

Steps for using Android JNI (2) EDIT main. xml

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android =
    Http://schemas.Android.com/apk/res/Android"
  3. Android: Orientation = "vertical"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent"
  6. >
  7. <Textview
  8. Android: Id = "@ + ID/jnitest"
  9. Android: layout_width = "fill_parent"
  10. Android: layout_height = "wrap_content"
  11. Android: text = "@ string/jnitest"
  12. />
  13. </Linearlayout>

Steps for using Android JNI (3) edit a Java File

  1. Package com. Ura. test;
  2. Import Android. App. activity;
  3. Import Android. OS. Bundle;
  4. Import Android. widget. textview;
  5. Public class jnitest extends activity {
  6. /** Called when the activity is first created .*/
  7. Static {
  8. System. loadlibrary ("jnitest ");
  9. }
  10. Public native string gettest ();
  11. @ Override
  12. Public void oncreate (bundle savedinstancestate ){
  13. Super. oncreate (savedinstancestate );
  14. Setcontentview (R. layout. Main );
  15. String STR = gettest ();
  16. Textview jnitest = (textview) findviewbyid (R. Id. jnitest );
  17. Jnitest. settext (STR );
  18. }
  19. }

Steps for using Android JNI (4) to generate a head File 

Compile the above project and claim the class file, and then use javah to generate the C/C ++ header file.

  1. Javah-classpath bin-d jni com. Ura. Test. jnitest

The generated header file is as follows:

  1. /* Do not edit this file-it is machine generated */
  2. # Include <JNI. h>
  3. /* Header for class com_ura_test_jnitest */
  4. # Ifndef _ included_com_ura_test_jnitest
  5. # DEFINE _ included_com_ura_test_jnitest
  6. # Ifdef _ cplusplus
  7. Extern "C "{
  8. # Endif
  9. /*
  10. * Class: com_ura_test_jnitest
  11. * Method: gettest
  12. * Signature: () ljava/lang/string;
  13. */
  14. Jniexport jstring jnicall java_com_ura_test_jnitest_gettest
  15. (Jnienv *, jobject );
  16. # Ifdef _ cplusplus
  17. }
  18. # Endif
  19. # Endif

Step 5: Compile the C/C ++ file as follows:

  1. # Include "com_ura_test_jnitest.h"
  2. # Define log_tag "jnitest"
  3. # UNDEF log
  4. # Include <utils/log. h>
  5. Jniexport jstring jnicall java_com_ura_test_jnitest_gettest
  6. (Jnienv * ENV, jobject OBJ)
  7. {
  8. Return (* env)-> newstringutf (ENV, (char *)
    "Jnitest native string ");
  9. Logd ("Hello Lib! \ N ");
  10. }

Step 6: Compile the Android. mk File

  1. Local_path: = $ (call my-DIR)
  2. Include $ (clear_vars)
  3. Local_src_files: = \
  4. Com_ura_test_jnitest.c
  5. Local_c_includes: = \
  6. $ (Jni_h_include)
  7. Local_shared_libraries: = libutils
  8. Local_prelink_module: = false
  9. Local_module: = libjnitest
  10. Include $ (build_shared_library)

Step 7: compile and generate a dynamic library for Android JNI

Create folder

  1. ~ /Mydroid/external/libjnitest

Copy the header file, C/C ++ source file, and make file to the preceding directory.

* Set prelink_moudule to false.

Otherwise, you need to re-create an imgfile and then burn it in.

Run

  1. CD
  2. CD mydroid/build/
  3. Envsetup. Sh
  4. Cd ~ /Mydroid
  5. CD external/libjnitest/
  6. Mm

After compilation is successful, the libjnitest. So file will be generated in the following directory.

  1. ~ Mydroid/out/target/product/generic/system/lib/

Steps for using Android JNI (8) execute a program in the simulator

First, copy the dynamic library to/system/lib.

Start the simulator

  1. ADB Shell
  2. ADB remount
  3. ADB push libjnitest. So/system/lib

Confirm that the copy is successful.

  1. CD/system/lib
  2. Ls

Then do not close the simulator (turn it off and enable the dynamic library, because the simulator Rom is read-only)

Execute Java program jnitest

The screen is displayed.

Jnitest native string

The specific usage of Android JNI is described here.

[Responsible editor: Cao Kai Tel :( 010) 68476606]

 

Original article: Android JNI sample code
Tag: Android JNI
Related Article

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.