本篇內(nèi)容介紹了“l(fā)inux 命令行下怎么使用android sdk 以及ndk”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了舞陽免費(fèi)建站歡迎大家使用!
首先保證已經(jīng)安裝了java,sdk,ndk 相關(guān)的一些包,并有類似如下的配置:
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 指定包名(必選)
-a 指定activity名(必選)
-t指定target(必選),例如此處android-17對于android4.2
-n指定工程名(可選) ,若不指定該選項,則默認(rèn)使用activity名字作為工程名字。
-p 指定工程目錄(必選)
運(yùn)行完畢會自動生成src/wy/first/helloandroid.java源文件:
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查看:
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.
手機(jī)上的運(yùn)行效果:
哇塞,居然自帶hello world,不明覺厲有木有,看來以后我們這種只會寫hello world的人都沒法混了,不行,作為一個專注hello world 30年的無腦碼農(nóng),一定要把這個主動權(quán)奪回來?。。∷阉髁艘环胖榔鋵嵤莿倓俛ndroid create project的時候,創(chuàng)建了一個默認(rèn)的xml界面布局文件res/layout/main.xml,默認(rèn)代碼如下
所以為了能在代碼中控制那個TextView控件,我們在TextView節(jié)點(diǎn)中給它加上一行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,運(yùn)行效果如下,恩,這才是真正的hello world嘛
為了個java代碼增加本地jni調(diào)用,再次修改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的類文件
神馬情況,居然又給我報錯,折騰半小時后終于有了點(diǎn)眉目,總之就是找不到android.app.Activity包,需要手動指定一個參數(shù),我這里以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
果斷執(zhí)行成功,此時我們的當(dāng)前目錄下自動生成了一個jni目錄,以及jni/wy_first_helloandroid.h頭文件。
現(xiàn)在要做的就是用c實現(xiàn)該頭文件中申明的函數(shù),so,創(chuàng)建 jni/wy_first_helloandroid.c文件
#include#include #include "wy_first_helloandroid.h" JNIEXPORT jstring JNICALL Java_wy_first_helloandroid_stringFromJNI(JNIEnv *env, jobject obj) { return (*env)->NewStringUTF(env, "hello ,么以可寫不"); }
創(chuàng)建jni/Android.mk文件,其中LOCAL_MODULE的值應(yīng)與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,運(yùn)行效果如下
“l(fā)inux 命令行下怎么使用android sdk 以及ndk”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!