Android Studio 如何编译SO
程序员文章站
2024-03-25 08:43:34
...
1,编写native java文件
public class OpenCvRebuild {
static {
System.loadLibrary("openCvRebuild");
}
public static native void testOpenCV(String name,int num);
public static native String testReturnString(float num);
public static native int testReturnInt(double num);
public static native float testReturnFloat(String name,int num,float order,double age);
public static native double testReturnDouble(String test);
}
2.生成对应的.h文件,使用最基础的方法
2.1首先将java文件编译成.class文件 ,使用命令javac OpenCvRebuild.java
打开Android Studio的Terminal工具,定位到OpenCvRebuild.java所在的目录(可以直接将OpenCvRebuild.java所在目录直接拖拽到Termianl中):
D:\demo\MyApplication\app\src\main\java\com\test\myapplication>javac OpenCvRebuild.java
D:\demo\MyApplication\app\src\main\java\com\test\myapplication>
刷新一下目录,就可以看到在myapplication目录下面多了一个.class文件:
2.2 将.class文件生成.h文件
在Terminal工具中将文件路径定位到java
输入指令:
D:\demo\MyApplication\app\src\main\java>javah com.test.myapplication.OpenCvRebuild
D:\demo\MyApplication\app\src\main\java>
之后刷新目录可以得到一个.h文件
到此位置,.h文件就创建好了
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_test_myapplication_OpenCvRebuild */
#ifndef _Included_com_test_myapplication_OpenCvRebuild
#define _Included_com_test_myapplication_OpenCvRebuild
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_test_myapplication_OpenCvRebuild
* Method: testOpenCV
* Signature: (Ljava/lang/String;I)V
*/
JNIEXPORT void JNICALL Java_com_test_myapplication_OpenCvRebuild_testOpenCV
(JNIEnv *, jobject, jstring, jint);
/*
* Class: com_test_myapplication_OpenCvRebuild
* Method: testReturnString
* Signature: (F)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_test_myapplication_OpenCvRebuild_testReturnString
(JNIEnv *, jobject, jfloat);
/*
* Class: com_test_myapplication_OpenCvRebuild
* Method: testReturnInt
* Signature: (D)I
*/
JNIEXPORT jint JNICALL Java_com_test_myapplication_OpenCvRebuild_testReturnInt
(JNIEnv *, jobject, jdouble);
/*
* Class: com_test_myapplication_OpenCvRebuild
* Method: testReturnFloat
* Signature: (Ljava/lang/String;IFD)F
*/
JNIEXPORT jfloat JNICALL Java_com_test_myapplication_OpenCvRebuild_testReturnFloat
(JNIEnv *, jobject, jstring, jint, jfloat, jdouble);
/*
* Class: com_test_myapplication_OpenCvRebuild
* Method: testReturnDouble
* Signature: (Ljava/lang/String;)D
*/
JNIEXPORT jdouble JNICALL Java_com_test_myapplication_OpenCvRebuild_testReturnDouble
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
下面根据.h文件写.c或.cpp文件
3.在main目录下创建一个jni目录,将.h文件剪切到jni目录下面,并根据.h文件生成.c文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#include <com_test_myapplication_OpenCvRebuild.h>
/* Header for class com_test_myapplication_OpenCvRebuild */
/*
* Class: com_test_myapplication_OpenCvRebuild
* Method: testOpenCV
* Signature: (Ljava/lang/String;I)V
*/
JNIEXPORT void JNICALL Java_com_test_myapplication_OpenCvRebuild_testOpenCV
(JNIEnv *env, jobject object, jstring name, jint num){
}
/*
* Class: com_test_myapplication_OpenCvRebuild
* Method: testReturnString
* Signature: (F)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_test_myapplication_OpenCvRebuild_testReturnString
(JNIEnv *env, jobject obj, jfloat num){
return (*env)->NewStringUTF(env,"testReturnString");
}
/*
* Class: com_test_myapplication_OpenCvRebuild
* Method: testReturnInt
* Signature: (D)I
*/
JNIEXPORT jint JNICALL Java_com_test_myapplication_OpenCvRebuild_testReturnInt
(JNIEnv *env, jobject obj, jdouble num){
return 10;
}
/*
* Class: com_test_myapplication_OpenCvRebuild
* Method: testReturnFloat
* Signature: (Ljava/lang/String;IFD)F
*/
JNIEXPORT jfloat JNICALL Java_com_test_myapplication_OpenCvRebuild_testReturnFloat
(JNIEnv *env, jobject obj, jstring name, jint intNum, jfloat floatNum, jdouble doubleNum){
return 1.10;
}
JNIEXPORT jdouble JNICALL Java_com_test_myapplication_OpenCvRebuild_testReturnDouble
(JNIEnv *env, jobject obj, jstring name){
return 2.0004;
}
4.再在jni目录下面创建Android.mk和Application.mk
Android.mk
LOCAL_MODULE标识生成的so库名称
LOCAL_SRC_FILES:jni目录下的.c文件,如果是多个.c文件可以用"\"分开
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := openCvRebuild
LOCAL_SRC_FILES :=\
testC.c
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
APP_MODULES := openCvRebuild
APP_CFLAGS += -DSTDC_HEADERS
APP_PLATFORM := android-21
5.现在所有的准备文件都已经做好,可以直接打包.so
在Terminal工具中定位到jni目录,输入指令ndk-build
刷新目录,得到一个libs包,将这个包中的内容粘贴到jniLibs中,就可以调用了
下一篇: cmd中如何切换指定目录
推荐阅读
-
Android Studio 如何编译SO
-
通过Android Studio编译安装App报错INSTALL_FAILED_TEST_ONLY终极解决方案
-
如何单独编译安卓系统源码指定模块 博客分类: android系统学习
-
Android studio 3版本编译Gradle3.0 JNI的.so工程
-
android反编译 学习如何隐藏应用图标 HadoopnginxEmacsdotnetnukerubygems
-
(转)如何导入Module作为依赖 博客分类: android studio Module
-
android studio 如何快速添加依赖
-
Android 源码如何编译调试
-
Android 源码如何编译调试
-
Android Studio gradle 编译提示‘default not found’ 解决办法