Android calls JNI and prints log

Source: Internet
Author: User

1. CreateNative Method

Public final static native int startservice ();

2. Select Project Right click on Android Tools->add native support will generate JNI file



The 3.CD user/alarmtest/bin/command enters the project's classes execution Javah Com.li.JNI package name. Class name can generate Com_yfz_jni.h file and then copy the com_yfz_jni.h file to the JNI file


4 Modify Android.mk File

Local_path: = $ (call My-dir)


Include $ (clear_vars)


Local_module: = Test This is the name of the generated library file

Local_src_files: = com_li_jni.cpp Note Here The default is the generated project name. cpp Here we need to change to the package name _ Class name. cpp because there is no code inside the build, you can use the. h file

Local_ldlibs + =-l$ (sysroot)/usr/lib-llog This is the library file we want to print with the Android log information.


Include $ (build_shared_library)

5. Modify Com.li.JNI.cpp

Introduction of two required files equivalent to Java Import

#include <jni.h>

#include <android/log.h>

Then add in. cpp because the. h file has already generated an interface inside the CPP to implement {

__android_log_print (Android_log_error, "test-jni", "%s", "ERROR");

Android_log_error This is the LOG level inside Android here is the ERROR

Test-jni is the tag inside Android

'%s ' means that the input is a string

Error is what we print


}

6. Click Project->build Project to generate the so library file.

7. Load Library file System.loadlibrary ("Test");

8. Call the JNI method Jni.startservice ();

9. Here's what we've shown from the Android call



OK, it's here today!! Everyone has any questions can leave a message to study together!!


Android calls JNI and prints log

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.