使用android SDK原代码 开发APK,将so文件打包进apk,这样可以只提供给客户一个apk.
1.含义全代码的so打包.
a)建立工程,并使用本地调用,java文件如下
1.含义全代码的so打包.
a)建立工程,并使用本地调用,java文件如下
02.
import
android.app.Activity;
03.
import
android.os.Bundle;
04.
import
android.widget.TextView;
05.
public
class
jnitest
extends
Activity {
06.
/** Called when the activity is first created. */
07.
@Override
08.
public
void
onCreate(Bundle savedInstanceState) {
09.
super
.onCreate(savedInstanceState);
10.
TextView tv =
new
TextView(
this
);
11.
int
x =
55
;
12.
int
y =
8
;
13.
System.loadLibrary(
"apptest"
);
14.
//System.loadLibrary("testapi");
15.
int
z = appadd(x, y);
16.
tv.setText( x +
"+"
+ y +
"="
+ z );
17.
setContentView(tv);
18.
}
19.
public
native
int
appadd(
int
x,
int
y);
20.
}
b)在工程的根目录建立Android.mk,内容如下.
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := user eng
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := jnitest
LOCAL_JNI_SHARED_LIBRARIES := libapptest
include $(BUILD_PACKAGE)
include $(LOCAL_PATH)/jni/Android.mk
# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := user eng
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := jnitest
LOCAL_JNI_SHARED_LIBRARIES := libapptest
include $(BUILD_PACKAGE)
include $(LOCAL_PATH)/jni/Android.mk
# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
其中LOCAL_MODULE_TAGS名字要和目标系统一致,否则编译产生的apk文件不能安装到目标系统,.
LOCAL_JNI_SHARED_LIBRARIES := libapptest 把我们另外编译成的so文件放到apk文件里的libs/armeabi里
include $(LOCAL_PATH)/jni/Android.mk为了编译本地文件成为so
c)在工程根目录创建jni文件夹,在jni文件里创建Android.mk和callapp.c,他们内容如下:
LOCAL_JNI_SHARED_LIBRARIES := libapptest 把我们另外编译成的so文件放到apk文件里的libs/armeabi里
include $(LOCAL_PATH)/jni/Android.mk为了编译本地文件成为so
c)在工程根目录创建jni文件夹,在jni文件里创建Android.mk和callapp.c,他们内容如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := user eng
LOCAL_MODULE := libapptest
LOCAL_SRC_FILES := callapp.c
LOCAL_C_INCLUDES += \
$(JNI_H_INCLUDE)
LOCAL_PRELINK_MODULE := false
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := user eng
LOCAL_MODULE := libapptest
LOCAL_SRC_FILES := callapp.c
LOCAL_C_INCLUDES += \
$(JNI_H_INCLUDE)
LOCAL_PRELINK_MODULE := false
include $(BUILD_SHARED_LIBRARY)
LOCAL_MODULE_TAGS :要和工程的一致,否则编译不通过
LOCAL_C_INCLUDES += \$(JNI_H_INCLUDE) 为了让jni编译通过,因为工程通过jni调用本地库。
callapp.c文件内容如下:
2.
JNIEXPORT jint JNICALL Java_test_jni_jnitest_appadd
3.
(JNIEnv *env, jobject obj, jint x, jint y){
4.
return
x +y;
5.
}
d)把工程文件放到SDK platform\packages\apps目录下,然后进入该目录,命令里输入mm,进行该工程的编译.(之前要再platform之前把工程目标设定好)
e)编译出来的apk 在platform/out/target/product/sk886x/system/app里可以找到,其中sk886x 是目标平台. 打开该apk文件,可以看到libapptest.so存在在包里的libs/armeabi文件夹里. 运行该apk,可以正常运行,显示正确答案.
e)编译出来的apk 在platform/out/target/product/sk886x/system/app里可以找到,其中sk886x 是目标平台. 打开该apk文件,可以看到libapptest.so存在在包里的libs/armeabi文件夹里. 运行该apk,可以正常运行,显示正确答案.