編輯:關於Android編程
public class MainActivity extends Activity { static { System.loadLibrary("JniTest"); } public native String getStringFromNative(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView txt = (TextView) findViewById(R.id.main_txt_msg); txt.setText(getStringFromNative()); } }
ndk { moduleName "JniTest" ldLibs "log", "z", "m" abiFilters "armeabi", "armeabi-v7a", "x86" }
sourceSets{ main{ jniLibs.srcDirs = ['libs'] } }
javah -d jni -classpath後面的"; 加載So所在的class包名+class名
C:\git_source\demo\JniTest>javah -d jni -classpath C:\studio\android-sdk-windows\platforms\android-23\android.jar;app\src\main\java com.example.jni.jnitest.MainActivity這裡的後面一部分是相對於"C:\git_source\demo\JniTest"目錄的路徑 命令執行成功會自動生成一個jni文件夾和對應的"包名.h"文件,這裡的文件名是:com_example_jni_jnitest_MainActivity.h
/* DO NOT EDIT THIS FILE - it is machine generated */ #include#include #ifndef LOG_TAG #define LOG_TAG "ANDROID_LAB" #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) #endif /* Header for class com_example_jni_jnitest_MainActivity */ #ifndef _Included_com_example_jni_jnitest_MainActivity #define _Included_com_example_jni_jnitest_MainActivity #ifdef __cplusplus extern "C" { #endif /* * Class: com_example_jni_jnitest_MainActivity * Method: getStringFromNative * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_example_jni_jnitest_MainActivity_getStringFromNative(JNIEnv * env, jobject jObj){ return (*env)->NewStringUTF(env,"Hello From JNI!"); } #ifdef __cplusplus } #endif #endif
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) #bzlib模塊 bzlib_files := \ main.c LOCAL_MODULE := libbz LOCAL_SRC_FILES := $(bzlib_files) include $(BUILD_STATIC_LIBRARY) #bspath模塊 include $(CLEAR_VARS) LOCAL_MODULE := main LOCAL_SRC_FILES := main.c LOCAL_STATIC_LIBRARIES := libbz #引入libbz庫 include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := JniTest LOCAL_SRC_FILES := main.c LOCAL_STATIC_LIBRARIES := main LOCAL_LDLIBS := -llog#加入log include $(BUILD_SHARED_LIBRARY)"bzlib_files :"裡面放的是所有的c源文件;"LOCAL_MODULE := JniTest"表示生成的So庫名,因為我加載So庫的名字是"JniTest"所以這裡需要同名;
static { System.loadLibrary("JniTest"); }Application.mk文件:
APP_CFLAGS += -Wno-error=format-security APP_ABI := armeabiAPP_ABI := armeabi 表示只支持armeabi架構的Jni,如需添加其他架構可在後面追加
原因是JniTest項目裡面的jni目錄下沒有Android.mk文件
FATAL EXCEPTION: main Process: com.example.jni.jnitest, PID: 30152 java.lang.UnsatisfiedLinkError: com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader[DexPathList[[dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-support-annotations-23.4.0_d560f708638dfceb3917510f1cc1dc667f754e94-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-internal_impl-23.4.0_00c09662b55d223b900e647a021f5f9dd9b4b6e9-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-com.android.support-support-vector-drawable-23.4.0_8b8204428df1954e75ed14d23129e230d51e4401-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-com.android.support-support-v4-23.4.0_ab900e8ce412421b38e4e809122c4e820ea3b15b-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-com.android.support-appcompat-v7-23.4.0_60d063a4fe66aa6d98c8e4fbd0183a230d80c604-classes.dex", dex file "/data/data/com.example.jni.jnitest/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-23.4.0_7565542ff4cab32ab703cc056ccf47fe61af9054-classes.dex"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64, /vendor/lib64, /system/lib64]]] couldn't find "libJniTest.so" at java.lang.Runtime.loadLibrary(Runtime.java:378) at java.lang.System.loadLibrary(System.java:998) at com.example.jni.jnitest.MainActivity.第一步:需要在app的build.gradle裡面的Android{}裡面添加如下代碼,指定jni放置目錄:(MainActivity.java:11) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1572) at android.app.Instrumentation.newActivity(Instrumentation.java:1068) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466) at android.app.ActivityThread.access$1200(ActivityThread.java:152) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5538) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:958)
sourceSets{ main{ jniLibs.srcDirs = ['libs'] } }
第二步:然後將so文件放在app文件夾下的libs裡面
第三步:編譯運行一、Socket通信簡介Android與服務器的通信方式主要有兩種,一是Http通信,一是Socket通信。兩者的最大差異在於,http連接使用的是“請求&m
最近一直在研究android wear SDK,總體感受來說就是和現有的android 其他的開發SDK還是有很多新的東西。例如手機終端與手表端的通信機制,手表端的UI規
本文給大家帶來一個很實用的小控件ClearEditText,就是在Android系統的輸入框右邊加入一個小圖標,點擊小圖標可以清除輸入框裡面的內容,IOS上面直接設置某個
源碼DEMO 等待審核中....... --------------------------------------------------------------