終端切換到 <sdk>/tools/ 目錄下執行 Android 命令就可以把AVD Manager 打開了。 可是,如果你沒有添加 <sdk>/tools 到你的環境變量中時,輸入 android 回車後終端只會提示你
android:找不到命令
而只要在android前加上 ./ 就可以解決問題:
./android
更簡單的辦法是將 <sdk>/tools 路徑添加進 PATH 環境變量。可以添加進用戶級環境變量,也可以添加到系統環境變量中。通過命令或編輯文件均可,在這裡我只提供一個辦法,打開終端,輸入:
sudo gedit /etc/environment
回車,在PATH=”………………….”的雙引號中追加上:
:<sdk>/tools:<sdk>/platform-tools
比如:
:/opt/android-sdk/tools:/opt/android-sdk/platform-tools
注意,:是分隔符。
重啟一下或者 source /etc/environment(立即生效) 在終端輸入 android 回車就會有反應了。
下載Android NDK : http://developer.android.com/sdk/ndk/index.html
echo 解壓縮得到android-ndk-r6b目錄,即可。 tar -jxvf android-ndk-r6b-linux-x86.tar.bz2
也將其路徑加入到source路徑中
經過了上述步驟,在命令行下敲:
ndk-bulid
彈出如下的錯誤,而不是說ndk-build not found,就說明ndk環境已經安裝成功了。
Android NDK: Could not find application project directory !
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
/home/braincol/workspace/android/android-ndk-r5/build/core/build-local.mk:85: *** Android NDK: Aborting . Stop.
二、代碼的編寫
1.首先是寫java代碼
建立一個Android應用工程HelloJni,創建HelloJni.java文件:
HelloJni.java :
/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.hellojni; import android.app.Activity; import android.widget.TextView; import android.os.Bundle; public class HelloJni extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); /* Create a TextView and set its content. * the text is retrieved by calling a native * function. */ TextView tv = new TextView(this ); tv.setText( stringFromJNI() ); setContentView(tv); } /* A native method that is implemented by the * 'hello-jni' native library, which is packaged * with this application. */ public native String stringFromJNI(); /* This is another native method declaration that is *not* * implemented by 'hello-jni'. This is simply to show that * you can declare as many native methods in your Java code * as you want, their implementation is searched in the * currently loaded native libraries only the first time * you call them. * * Trying to call this function will result in a * java.lang.UnsatisfiedLinkError exception ! */ public native String unimplementedStringFromJNI(); /* this is used to load the 'hello-jni' library on application * startup. The library has already been unpacked into * /data/data/com.example.HelloJni/lib/libhello-jni.so at * installation time by the package manager. */ static { System.loadLibrary("hello-jni" ); } }
這段代碼很簡單,注釋也很清晰,這裡只提兩點::
static{
System.loadLibrary("hello-jni" );
}
表明程序開始運行的時候會加載hello-jni, static區聲明的代碼會先於onCreate方法執行。如果你的程序中有多個類,而且如果HelloJni這個類不是你應用程序的入口,那麼 hello-jni(完整的名字是libhello-jni.so)這個庫會在第一次使用HelloJni這個類的時候加載。
public native String stringFromJNI();
public native String unimplementedStringFromJNI();
可以看到這兩個方法的聲明中有 native 關鍵字, 這個關鍵字表示這兩個方法是本地方法,也就是說這兩個方法是通過本地代碼(C/C++)實現的,在java代碼中僅僅是聲明。
用eclipse編譯該工程,生成相應的.class文件,這步必須在下一步之前完成,因為生成.h文件需要用到相應的.class文件。