Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the case implementation of adding applications to Android SDK?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about the case implementation of adding applications to Android SDK. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

In the process of developing Android applications, there are usually two ways: NDK and eclipse, or add directly to Android SDK. Here is how to add an application to the Android SDK environment. It involves the call of JNI,JAVA to JNI, where the JNI code file is C++.

The first step is to establish the JNI layer code. Note that the path of the JNI code has a lot to do with the code path of the JAVA, so it must be consistent.

Android SDK creates the JNI C++ file android_test.cpp under the frameworks/base/core/jni directory, which contains the following:

# define LOG_TAG "FMC" # include "jni.h" # include "android_runtime/AndroidRuntime.h" # include # include "utils/Log.h" extern "C" {int test (void); / / this function can come from c code or c library}; namespace android / / Note that namespaces are closely related to all paths of JAVA calling JNI and cannot be named spaces casually. / / the original author's sentence is not correct / / it's just that the namespace of C++ has nothing to do with JNI, so if you don't use android namespace, you should declare it outside android namespace in / / AndroidRuntime.cpp. {static jint android_test (JNIEnv * env, jobject clazz) {return test ();} static JNINativeMethod method_table [] = {{"my_test", "() I", (void*) android_test}, / / * arguments are strings, which is the function name seen in the JAVA layer / / the second parameter is the formal parameter description of the function. / / the JAVA code knows the formal parameter of the called function by this parameter; / / the third parameter is the C function actually called in the JNI code. } int register_android_test (JNIEnv * env) / / this is the JNI registration function, and android.test is the package path seen by the java call layer (step 3). / / if this is register_android_hardware_test, / / then the JAVA call layer needs to import the android.hardware.test package {return AndroidRuntime::registerNativeMethods (env, "android/test/Test", method_table,NELEM (method_table));}}

The second step is to add the JNI code registration function to the AndroidRuntime runtime library. Edit the frameworks/base/core/jni/AndroidRuntime.cpp file:

Android SDK adds the following two lines according to the method on the file:

. Extern int register_android_test (JNIEnv* env); / / this is the registration function in the android_test.cpp file.. Static const RegJNIRec gRegJNI [] = {. . REG_JNI (register_android_test), . }

The third step is to establish the JAVA call layer and create the file Test.java under the frameworks/base/core/java/android/test directory, as follows:

The last thing that can be called in the code of package android.test; public class Test {private native int my_test (); / / Android application is the code public Test () / / constructor {/ / Add Your Code Here. }}

Step 4: create your android application under the packages/apps/test directory (not described here). Assuming that test_app.java is the main file of the application, the content is as follows:

Package com.app; import android.test;// imports its own package and is used in the code like this: test test; test.my_test (); / / final call

Android SDK JNI implementation, Android SDK JNI C++ code, Android calls Cellular codes, establish the complete steps of JNI and JAVA applications under Android SDK, and Android JAVA calls C++ code.

This is how the editor shares the case of adding applications to Android SDK. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report