偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.
全部博文(1750)
分类: Android平台
2014-08-14 19:47:47
另外通用的添加API的方法
android APP调用自定义java API,自定义java API调用自定义jni接口
1:在android源码目录framework/base下面创建add目录
add目录里面的结构如下:
里面的Nadd.java android_jnitest_Nadd.cpp 和Android.mk内容如下:
Nadd.java
package android.jnitest; public class Nadd { static { System.loadLibrary("hello_jni"); } public native int nadd(int a, int b); }
android_jnitest_Nadd.cpp
#define LOG_TAG "hello-JNI" #include#include #include #include #include #include #include #include "jni.h" #include "JNIHelp.h" #include "android_runtime/AndroidRuntime.h" static jint native_nadd(JNIEnv *env, jobject obj, jint a, jint b) { return (a + b); } static JNINativeMethod gMethods[] = { {"nadd", "(II)I", (void *)native_nadd}, }; static int register_android_jnitest_Nadd(JNIEnv *env) { return android::AndroidRuntime::registerNativeMethods(env, "android/jnitest/Nadd", gMethods, NELEM(gMethods)); } jint JNI_OnLoad(JavaVM *vm, void *reserved) { JNIEnv *env = NULL; if (vm->GetEnv((void **)&env, JNI_VERSION_1_4) != JNI_OK) { printf("Error GetEnv\n"); return -1; } assert(env != NULL); if (register_android_jnitest_Nadd(env) < 0) { printf("register_android_test_hello error.\n"); return -1; } return JNI_VERSION_1_4; }
Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_PRELINK_MODULE := false LOCAL_SRC_FILES := \ android_jnitest_Nadd.cpp LOCAL_SHARED_LIBRARIES := \ libandroid_runtime LOCAL_MODULE := libhello_jni include $(BUILD_SHARED_LIBRARY)
上面的步骤已经将代码准备好了
2:修改build/core/pathmap.mk文件
在FRAMEWORKS_BASE_SUBDIRS变量后添加 add\ ,代码如下
FRAMEWORKS_BASE_SUBDIRS := \
$(addsuffix /java, \
core \
graphics \
location \
media \
opengl \
sax \
telephony \
wifi \
vpn \
keystore \
add \
)
3:修改frameworks/base/Android.mk 文件
在packages_to_document变量后添加自己源代码的包名称,如 android/jnitest ,结果如下
packages_to_document := \
android \
javax/microedition/khronos\
android/jnitest
4:make update-api
这一步更新api,api文档在frameworks/base/api/current.txt
5:修改build/target/product/base.mk
在PRODUCT_PACKAGES += \后面添加 libhello_jni
PRODUCT_PACKAGES += \
20-dns.conf \
95-configured \
am \
android.policy \
android.test.runner \
app_process \
applypatch \
bmgr \
bugreport \
content \
dhcpcd \
dhcpcd-run-hooks \
dnsmasq \
framework \
fsck_msdos \
ime \
javax.obex \
libSR_AudioIn \
libandroid \
libandroid_runtime \
libandroid_servers \
libaudioeffect_jni \
libaudioflinger \
libbundlewrapper \
libcamera_client \
libcameraservice \
libchromium_net \
libdl \
libdrm1 \
libdrm1_jni \
libeffects \
libiprouteutil \
libjni_latinime \
libjnigraphics \
libmedia \
libmedia_jni \
libmediaplayerservice \
libmtp \
libnetlink \
libnetutils \
libreference-ril \
libreverbwrapper \
libril \
librtp_jni \
libsensorservice \
libskia \
libsonivox \
libsoundpool \
libsqlite \
libstagefright \
libstagefright_amrnb_common \
libstagefright_avc_common \
libstagefright_enc_common \
libstagefright_foundation \
libstagefright_omx \
libstagefright_yuv \
libsystem_server \
libusbhost \
libutils \
libvisualizer \
libvorbisidec \
libwpa_client \
media \
media_cmd \
mediaserver \
monkey \
mtpd \
ndc \
netcfg \
netd \
ping \
platform.xml \
pppd \
pm \
racoon \
run-as \
schedtest \
screenshot \
sdcard \
services \
settings \
svc \
system_server \
tc \
vdc \
vold \
webview \
wm\
libhello_jni
6:make PRODUCT-sdk-sdk -j 4
7:编译好后,自己的sdk会编译到out/host/linux-x86/下,里面的android-sdk_eng.xxx_linux-x86就是自己的sdk,在eclipse中选择自己sdk的路径
如下图:我这里只是介绍,没有选择,用户可以选择~/out/host/linux-x86/android-sdk_eng.xxx_linux-x86路径,然后点击Apply
8:编写app测试
eclipse创建一个最简单的hello工程,
将里面的MainActivity.java内容替换如下(或者来这里下载)
package com.example.jnitest;
import android.jnitest.Nadd;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Nadd test = new Nadd();
setTitle("The Native Add Result is "+String.valueOf(test.nadd(30, 30)));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//========================================================================================
开放systemProperty的方法.
vi ./frameworks/base/core/java/android/os/SystemProperties.java
修改 其中 @Hide, 中间插几个空格, 其实就是把javadoc的这个类开放出来.
make update-api
make PRODUCT-sdk-sdk -j 4
out/host/linux-x86/下,里面的android-sdk_eng.xxx_linux-x86就是自己的sdk
编译android sdk 时:不要直接make sdk
windows版sdk:
$make -j4 PRODUCT-sdk-win_sdk
参考development/build/tools/windows_sdk.mk