In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use android sdk and ndk under the linux command line". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Environment
First of all, make sure that some packages related to java,sdk,ndk have been installed and have a configuration similar to the following:
Create the project mkdir proj.androidcd proj.androidandroid create project-k wy.first-a helloandroid-n chinease-t android-17-p. / using export NDK_ROOT=/home/develop/android-ndk-r9cexport SDK_ROOT=/home/develop/adt-bundle-linux-x86_64-20131030/sdkPATH=$PATH:$SDK_ROOT/toolsPATH=$PATH:$NDK_ROOTexport JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64android SDK under the command line
Where-k specifies the package name (required)
-a specify activity name (required)
-t specify target (required), for example, here android-17 is for android4.2
-n specifies the project name (optional). If this option is not specified, the activity name is used as the project name by default.
-p specify project catalog (required)
After running, the src/wy/first/helloandroid.java source file is automatically generated:
Package wy.first;import android.app.Activity;import android.os.Bundle;public class helloandroid extends Activity {/ * * Called when the activity is first created. * / @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.main);} compile java and package it into apkant debug to install the compiled debug version of the apk package on the target device ant installd
The above two commands can also be completed at one time by ant debug install. The command section of ant is not discussed in detail. You can type ant help to view it:
Kimo@debian-desktop:~/proj.android$ ant helpBuildfile: / home/kimo/proj.android/build.xmlhelp: [echo] Android Ant Build. Available targets: [echo] help: Displays this help. [echo] clean: Removes output files created by other targets. [echo] This calls the same target on all dependent projects. [echo] Use 'ant nodeps clean' to only clean the local project [echo] debug: Builds the application and signs it with a debug key. [echo] The 'nodeps' target can be used to only build the [echo] current project and ignore the libraries using: [echo]' ant nodeps debug' [echo] release: Builds the application. The generated apk file must be [echo] signed before it is published. [echo] The 'nodeps' target can be used to only build the [echo] current project and ignore the libraries using: [echo]' ant nodeps release' [echo] instrument:Builds an instrumented package and signs it with a [echo] debug key. [echo] test: Runs the tests. Project must be a test project and [echo] must have been built. Typical usage would be: [echo] ant [emma] debug install test [echo] emma: Transiently enables code coverage for subsequent [echo] targets. [echo] install: Installs the newly build package. Must either be used [echo] in conjunction with a build target (debug/release/ [echo] instrument) or with the proper suffix indicating [echo] which package to install (see below). [echo] If the application was previously installed, the [echo] application is reinstalled if the signature matches. [echo] installd: Installs (only) the debug package. [echo] installr: Installs (only) the release package. [echo] installi: Installs (only) the instrumented package. Installt: Installs (only) the test and tested packages (unless [echo] nodeps is used as well. [echo] uninstall: Uninstalls the application from a running emulator or [echo] device. Also uninstall tested package if applicable [echo] unless' nodeps' is used as well.
The running effect on the mobile phone:
Wow, actually bring hello world, although do not understand but think this is very powerful, it seems that in the future we can only write hello world people can not mix, no, as a mindless programmer focused on hello world for 30 years, we must take the initiative back! After searching for android create project, we found out that a default xml interface layout file, res/layout/main.xml, was created. The default code is as follows.
Default ui layout for a new project
So in order to control the TextView control in the code, we add a line of id attribute to it in the TextView node
Android:id= "@ + id/myTextView"
Then modify the src/wy/first/helloandroid.java file
Package wy.first;import android.app.Activity;import android.os.Bundle;import android.widget.TextView; / / addpublic class helloandroid extends Activity {/ * * Called when the activity is first created. * / @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.main); TextView myTextView = (TextView) findViewById (R.id.myTextView); / / add myTextView.setText ("Don't write"); / / add}}
Ant debug install again, the running effect is as follows, well, this is the real hello world.
The use of android NDK declares local methods in the class
To add a local jni call to a java code, modify the src/wy/first/helloandroid.java again
Package wy.first;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class helloandroid extends Activity {/ * * Called when the activity is first created. * / @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.main); TextView myTextView = (TextView) findViewById (R.id.myTextView); myTextView.setText (stringFromJNI ()); / / modify} public native String stringFromJNI (); / / add static {System.loadLibrary ("testso"); / / add}} compile the java source file
Type ant debug to automatically compile and generate the jni header file and then use the javah tool to automatically generate the c-generation wharf file called by jni javah-classpath bin/classes-d jni wy.first.helloandroid error: unable to access the class file where android.app.Activity cannot find android.app.Activity
After half an hour of trouble, I finally got something. In short, the android.app.Activity package could not be found. You need to specify a parameter manually. Here, I take target as an example:
Javah-classpath bin/classes-bootclasspath / home/develop/adt-bundle-linux-x86_64-20131030/sdk/platforms/android-17/android.jar-d jni wy.first.helloandroid
The decisive execution is successful, and a jni directory and a jni/wy_first_helloandroid.h header file are automatically generated under our current directory.
Implement the c function called by jni
Now all you have to do is use c to implement the function declared in the header file, so, and create the jni/wy_first_helloandroid.c file.
# include # include # include "wy_first_helloandroid.h" JNIEXPORT jstring JNICALL Java_wy_first_helloandroid_stringFromJNI (JNIEnv * env, jobject obj) {return (* env)-> NewStringUTF (env, "hello, not writable");} ndk-build is compiled to generate so
Create a jni/Android.mk file where the value of LOCAL_MODULE should be the same as in the java code
LOCAL_PATH: = $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE: = testsoLOCAL_SRC_FILES: = wy_first_helloandroid.cinclude $(BUILD_SHARED_LIBRARY)
Then you can compile so. Type ndk-build and enter.
Kimo@debian-desktop:~/proj.android$ ndk-build [armeabi] Install: libtestso.so = > libs/armeabi/libtestso.so
Decisively so will be generated. Ant debug install again, and the running effect is as follows
This is the end of the introduction of "how to use android sdk and ndk under the linux command line". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.