How to compile the JNI module on the Android platform (1)

Source: Internet
Author: User

This article mainly records my research experience on the JNI technology on the Android platform. It is a memo, and it also saves some time to explore for later users.

I have been busy with projects and have heard a friend mention that my blog has not been updated for a long time. So today I will take some time to write something. No way. With the increasingly fierce competition in the mobile phone industry, and the rise of companies like Google, apple, and palm, there is less and less free time for practitioners in the mobile phone field, and competition is becoming increasingly fierce.

Start with the topic:
1. Introduction to the command line Development Method
Because C/C ++ is used to it, I am not used to using eclipse. I decided to use the command line (similar to makefile) compilation and development (even more obsessed with Emacs ).
If it is a Java platform, ant will be used. I will not go into details about the installation of Android SDK and the setting of environment variables in Linux,

The selected work environment is as follows:
Software environment: android-sdk-linux_x86_1.5_r1, JDK 1.6.0 _ 12, slackware 12.1G,
Hardware environment: lenvo sl400 (Office version) and Android G1 (cracked and updated to cupcake 1.5)

(1) create a project
Wayne @ WAYNE :~ /Works/workspace $ Android Create project -- Target 2 -- package com. TWM -- activity jnitest -- path/home/Wayne/Works/workspace/jnitest

Here, Android is a script tool provided by SDK 1.5. Note that the parameter -- target (Thank god, google can finally support both Android 1.1 and Android 1.5 software development in an SDK.) -- when the value of target is 1, the generated project is an android 1.1 Project, -- when the value of target is 2, the generated project is an android 1.5 project. In the preceding command line, we created an Android Application project of for 1.5 with a value of 2.

(2) compile the project
CD jnitest
Ant debug
The following output is displayed:
Buildfile: Build. xml
[Setup] project target: Android 1.5
[Setup] API level: 3

Dirs:
[Echo] creating output directories if needed...

Resource-Src:
[Echo] generating R. Java/manifest. Java from the resources...

Aidl:
[Echo] compiling aidl files into Java classes...

Compile:
[Javac] compiling 1 source file to/home/Wayne/Works/workspace/jnitest/bin/classes

DEX:
[Echo] converting compiled files and external libraries into bin/classes. Dex...

Package-resources:
[Echo] packaging Resources
[Aaptexec] creating full resource package...

Debug:
[Apkbuilder] creating .jnitest-debug.apk and signing it with a debug key...
[Apkbuilder] Using keystore:/home/Wayne/. Android/debug. keystore

Build successful
Total time: 6 seconds

OK. If you see a similar image, it means that your command line compiling environment is correct.
The following is the verification:
CD Bin
Ls
If you see jnitest-debug.apk, it means that your installation package has been generated (note that this is the debug version of the installation package, you can start the simulator and then use ADB install, then go to the simulator to see the effect of Hello world. Khan, isn't it easy ?!)

(3) generate the release version of the installation package
We just generated the debug version of the installation package. What should we do to generate the release version? It is easy to go back to the jnitest directory and run the following command:
Ant release
The following output is displayed:
Buildfile: Build. xml
[Setup] project target: Android 1.5
[Setup] API level: 3

Dirs:
[Echo] creating output directories if needed...

Resource-Src:
[Echo] generating R. Java/manifest. Java from the resources...

Aidl:
[Echo] compiling aidl files into Java classes...

Compile:
[Javac] compiling 1 source file to/home/Wayne/Works/workspace/jnitest/bin/classes

DEX:
[Echo] converting compiled files and external libraries into bin/classes. Dex...

Package-resources:
[Echo] packaging Resources
[Aaptexec] creating full resource package...

Release:
[Apkbuilder] creating .jnitest-unsigned.apk for release...
[Echo] All generated packages need to be signed with jarsigner before they are published.

Build successful
Total time: 4 seconds

OK. Please note that there is such a sentence:
[Apkbuilder] creating .jnitest-unsigned.apk for release...
[Echo] All generated packages need to be signed with jarsigner before they are published.
It seems that a release version of the installation package named jnitest-unsigned.apk is generated, and "intimate" tells us that this package needs to be signed by jarsigner (nnd, all blame Nokia for having a bad header, now, you need to sign a program that is a little useful to your mobile phone no matter what platform you want... )

(4) create a signature for the release APK
Keytool-genkey-v-keystore waynechen. keystore-alias Wayne-keyalg RSA-validity 10000
Here, a Linux tool is used to generate a key used to identify the identity (anyway, even the mobile phone test is naturally cracked and refreshed many times, but one thing is certain, the unsigned APK cannot be installed even if the android G1 is cracked ).
After the preceding command is run, a signature file called waynechen. keystore is generated to prepare jarsinger for the next step.

(5) sign the unsigned APK installation package
This jarsigner is not a tool provided by the android SDK, but a tool provided by the JDK. Therefore, if you cannot find this tool, you can check whether the bin path of the JDK has been added to the current path.
Jarsigner-verbose-keystore waynechen. keystore JniTest-unsigned.apk Wayne
The file behind the signature is still called jnitest-unsigned.apk. However, the APK file has already been signed and can be installed on the real machine.

Now, a lot of methods for compiling Android code through command lines have been added. you can skip the above steps directly by using Eclipse IDE, beat you to death ...)

2. basic working principle of JNI
(1) essence of Java
To understand the essence of JNI, we must start with the essence of Java. In essence, the Java language is a scripting language (this is my personal understanding, and I hope Java heroes will not use bricks to pat me ), its operation relies entirely on the script engine to explain and execute Java code (of course, modern Java is much more advanced and can be compiled from source code. class and other binary files in the intermediate format, this processing will greatly accelerate the Java Script running speed, but the basic execution method remains unchanged, and is executed by the script engine (we call it JVM, compared with pure scripts such as Python and Perl, it only converts the script into a binary format. In addition, JAVA supports the object-oriented concept well and has a complete functional library for calling. This script engine is transplanted to all platforms, this script will naturally implement the so-called "cross-platform ). The vast majority of script engines support a significant feature, that is, they can use C/C ++ to write modules and call these modules in the script to compare them with Java, java must provide a mechanism to call modules written in C/C ++ in scripts, so that it can be called a relatively complete script reference.

(2) Java in Android
The Android platform is essentially composed of an arm-Linux operating system and a Java virtual machine called Dalvik. All the gorgeous interfaces seen on the android simulator are written in Java (see the framework/base Directory of the Android platform source code ). Currently, Dalvik only provides a standard Java virtual machine environment that supports JNI calls. All hardware-related operations on the Android platform are encapsulated using the JNI technology. Java calls the JNI module, the JNI module uses C/C ++ to call the underlying arm-Linux driver of Android.

For example, there is a file named "egldisplaysurface. cpp" under the frameworks/base/libs/UI Directory, which contains:
Status_t egldisplaysurface: In the mapframebuffer () function, there is an initialization code for framebuffer in arm-Linux of Android.

This also confirms that android is actually built on Java + JNI. Hoho, as a result, highlights the importance of JNI in Android Development (of course, some simple small programs can be done with only Java ).

I wanted to write an article to record the compiling and debugging methods of JNI...
As a result, I accidentally ran the question and introduced so many introductory knowledge about the surrounding area, so I had to continue with the next article.

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.