The method of compiling Java class Library and JNI Dynamic Library of Android system from source code _android

Source: Internet
Author: User

Using source code to compile the Java class Library of Android system
1. Writing Java projects and android.mk files

  ├──android.mk
  └──src
   └──com
     └──lhw
       └──framework
           └──led

Led.java file

Package com.lhw.framework.led; 
 
/** 
 * LED Operation Library 
 * @author Micky Liu 
/public class LEDs {public 
   
  Boolean turnon () {return 
    tru e; 
  }   
   
  public Boolean turnoff () {return 
    true; 
  }   

ANDROID.MK file

Local_path: = $ (call My-dir) 
 
local_src_files   : = $ (call All-java-files-under, SRC) 
local_module    : = framework_led 
local_module_tags  : = framework_led 
 
local_java_libraries    : = 
local_static _java_libraries: = 
 
include $ (build_java_library) 

2, compile

   Mmm/home/administrator/workspace/android_test/framework_led

Compilation results:

   Install:out/target/product/generic/system/framework/framework_led.jar    


using source code to compile the Android system JNI Dynamic Library
1, code and ANDROID.MK writing

├──jni
│  ├──android.mk
│  └──led.c


Led.c

#include <jni.h> 
#include <stdio.h> 
 
jboolean java_com_lhw_led_led_turnon (jnienv *env, Jclass Thiz) 
{return 
  jni_true; 
} 
 
Jboolean Java_com_lhw_led_led_trunoff (jnienv *env, Jclass thiz) 
{return 
  jni_true; 
} 

Note: The method name rule is: Java_ Package Name _ Class Name _ Method name, you can use the Javah command to generate the required. h header file According to the Java file. Of course, a better approach is to implement dynamic registration through Jni_onload and androidruntime::registernativemethods, so that you don't have to ignore the method name rules. Here is just a simple example, followed by an example to implement dynamic registration.

Android.mk

Local_path: = $ (call My-dir) 
 
local_src_files: = led.c 
local_module  : = Led_jni    local_module_tags : = Led_jni 
 
local_prelink_module  : = False 
local_shared_libraries: = libc 
 
include $ (build_shared_ LIBRARY) 

2, compile

  Mmm/home/administrator/workspace/android_test/jni

Compilation results:

  Install:out/target/product/generic/system/lib/led_jni.so

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.