1、安装ndk,操作如下:
窗口->首选项->Android->NDK 点击游览,把NDK路径加上
jni文件说明:
在JNI文件夹内要有Android.mk文件,用于编译
格式如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := appManage //生成的so文件名
LOCAL_SRC_FILES :=\
wisdomAppManage.cpp\
bzlib.c \
blocksort.c \
compress.c \
crctable.c \
decompress.c \
huffman.c \
randtable.c \
bzip2.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
项目配置:
工程右击->android tools->add native->support
jni层C、C++与Java对接文件
#include <jni.h>
/* Header for class com_droidupdate_jni_PatchUtil */
#ifndef _Included_com_droidupdate_jni_PatchUtil
#define _Included_com_droidupdate_jni_PatchUtil
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_droidupdate_jni_PatchUtil
* Method: applyPatchToOldApk
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_hst_cz_packagemanager_pach_PatchUtil_applyPatchToOldApk
(JNIEnv *, jclass, jstring, jstring, jstring);
#ifdef __cplusplus
}
#endif
#endif
com_hst_cz_packagemanager_pach为java包名
java代码编写:
package com.hst.cz.packagemanager.pach;
static {
System.loadLibrary("appManage"); //所生成的so文件
};
private static native int applyPatchToOldApk(String oldapk_filepath,
String newapk_savepath, String patchpath);
/**
* @param oldApkPath
* 旧版apk文件的路�?
* @param newApkPath
* 新版apk文件的路�?
* @param patchPath
* 增量包的路径
* @throws IOException
*/
public static int applyPatch(String oldApkPath, String newApkPath,
String patchPath) throws IOException {
return applyPatchToOldApk(oldApkPath, newApkPath, patchPath);
}
jni层的C、C++打印消息:
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "Myloc", __VA_ARGS__)