首先保證已經安裝了java,sdk,ndk 相關的一些包,並有相似以下的配置:
html
export NDK_ROOT=/home/develop/android-ndk-r9c export SDK_ROOT=/home/develop/adt-bundle-linux-x86_64-20131030/sdk PATH=$PATH:$SDK_ROOT/tools PATH=$PATH:$NDK_ROOT export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
mkdir proj.android cd proj.android android create project -k wy.first -a helloandroid -n chinease -t android-17 -p ./
其中 -k 指定包名(必選)
java
-a 指定activity名(必選)
node
-t指定target(必選),例如此處android-17對於android4.2
linux
-n指定工程名(可選) ,若不指定該選項,則默認使用activity名字做爲工程名字。
android
-p 指定工程目錄(必選)
shell
運行完畢會自動生成src/wy/first/helloandroid.java源文件:app
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); } }
ant debug
ant installd
上面兩個命令也能夠ant debug install一次完成,ant的命令部分不在贅述,可自行鍵入ant help查看:less
kimo@debian-desktop:~/proj.android$ ant help Buildfile: /home/kimo/proj.android/build.xml help: [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. [echo] 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.
手機上的運行效果:jvm
哇塞,竟然自帶hello world,不明覺厲有木有,看來之後咱們這種只會寫hello world的人都無法混了,不行,做爲一個專一hello world 30年的無腦碼農,必定要把這個主動權奪回來!!!搜索了一番才知道實際上是剛剛android create project的時候,建立了一個默認的xml界面佈局文件res/layout/main.xml,默認代碼以下ide
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, helloandroid" /> </LinearLayout>
因此爲了能在代碼中控制那個TextView控件,咱們在TextView節點中給它加上一行id屬性
android:id="@+id/myTextView"
而後修改src/wy/first/helloandroid.java文件
package wy.first; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; //add 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); //add myTextView.setText("不寫能夠麼"); //add } }
再次ant debug install,運行效果以下,恩,這纔是真正的hello world嘛
爲了個java代碼增長本地jni調用,再次修改src/wy/first/helloandroid.java
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 } }
javah -classpath bin/classes -d jni wy.first.helloandroid 錯誤: 沒法訪問android.app.Activity 找不到android.app.Activity的類文件
神馬狀況,竟然又給我報錯,折騰半小時後終於有了點眉目,總之就是找不到android.app.Activity包,須要手動指定一個參數,我這裏以target爲android-17爲例:
javah -classpath bin/classes -bootclasspath /home/develop/adt-bundle-linux-x86_64-20131030/sdk/platforms/android-17/android.jar -d jni wy.first.helloandroid
果斷執行成功,此時咱們的當前目錄下自動生成了一個jni目錄,以及jni/wy_first_helloandroid.h頭文件。
如今要作的就是用c實現該頭文件中申明的函數,so,建立 jni/wy_first_helloandroid.c文件
#include <string.h> #include <jni.h> #include "wy_first_helloandroid.h" JNIEXPORT jstring JNICALL Java_wy_first_helloandroid_stringFromJNI(JNIEnv *env, jobject obj) { return (*env)->NewStringUTF(env, "hello ,麼以可寫不"); }
建立jni/Android.mk文件,其中LOCAL_MODULE的值應與java代碼中的一致
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := testso LOCAL_SRC_FILES := wy_first_helloandroid.c include $(BUILD_SHARED_LIBRARY)
而後就能夠編譯so了,鍵入ndk-build回車
kimo@debian-desktop:~/proj.android$ ndk-build [armeabi] Install : libtestso.so => libs/armeabi/libtestso.so
果斷so就生成好了,再次ant debug install,運行效果以下
恩,正是咱們期待的效果,so,收工吃飯
http://www.cnblogs.com/eddy-he/archive/2012/08/08/2628676.html
http://blog.csdn.net/furongkang/article/details/6857610
http://www.cnblogs.com/hoys/archive/2010/10/28/1863612.html