在 Android Studio 中使用 .so 文件
Using .so files in Android Studio
我是 Android 的新手。我有一个基本的 hello-world 本机代码函数,如下所示:
#include <string.h>
#include <jni.h>
#include <cassert>
#include <string>
#include <iostream>
#include <fromhere.h>
using namespace std;
/* This is a trivial JNI example.
* The string returned can be used by java code*/
extern "C"{
JNIEXPORT jstring JNICALL
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz )
{
#if defined(__arm__)
#if defined(__ARM_ARCH_7A__)
#if defined(__ARM_NEON__)
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a/NEON (hard-float)"
#else
#define ABI "armeabi-v7a/NEON"
#endif
#else
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a (hard-float)"
#else
#define ABI "armeabi-v7a"
#endif
#endif
#else
#define ABI "armeabi"
#endif
#elif defined(__i386__)
#define ABI "x86"
#elif defined(__x86_64__)
#define ABI "x86_64"
#elif defined(__mips64) /* mips64el-* toolchain defines __mips__ too */
#define ABI "mips64"
#elif defined(__mips__)
#define ABI "mips"
#elif defined(__aarch64__)
#define ABI "arm64-v8a"
#else
#define ABI "unknown"
#endif
string s = returnit();
jstring retval = env->NewStringUTF(s.c_str());
return retval;
}
}
现在如果我写fromhere.cpp如下:
#include <string>
using namespace std;
string returnit()
{
string s="Hello World";
return s;
}
我可以通过编写 fromhere.h 文件并在其中声明 returnit 来包含 fromhere.h,并且只在 Android.mk 的 LOCAL_SRC_FILES 和 [= 中包含上述文件的名称23=] 出现在我从 java class.
制作的文本视图中
但我想将这些 fromhere.cpp 和 fromhere.h 编译为预构建的 .so 文件构建 ny ndk 并使用其中的 returnit() 函数。有人可以逐步向我解释如何在 Android Studio 中具体执行此操作吗?
废话请指正
您说您使用的是 Android Studio,但默认情况下 Android Studio 当前会忽略您的 Makefile 并使用它自己自动生成的 Makefile,不支持本机依赖项(目前)。
如果您停用内置支持并自己调用 ndk-build,请将类似的内容放入 build.gradle:
android {
sourceSets.main {
jniLibs.srcDir 'src/main/libs' //set libs as .so's location instead of jniLibs
jni.srcDirs = [] //disable automatic ndk-build call with auto-generated Android.mk
}
}
这是使用 Makefile 的解决方案:
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := fromhere.cpp
LOCAL_MODULE := fromhere
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH) # useless here, but if you change the location of the .h for your lib, you'll have to set its absolute path here.
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := hello-world.cpp
LOCAL_MODULE := hello-world
LOCAL_SHARED_LIBRARIES := fromhere
include $(BUILD_SHARED_LIBRARY)
我是 Android 的新手。我有一个基本的 hello-world 本机代码函数,如下所示:
#include <string.h>
#include <jni.h>
#include <cassert>
#include <string>
#include <iostream>
#include <fromhere.h>
using namespace std;
/* This is a trivial JNI example.
* The string returned can be used by java code*/
extern "C"{
JNIEXPORT jstring JNICALL
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz )
{
#if defined(__arm__)
#if defined(__ARM_ARCH_7A__)
#if defined(__ARM_NEON__)
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a/NEON (hard-float)"
#else
#define ABI "armeabi-v7a/NEON"
#endif
#else
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a (hard-float)"
#else
#define ABI "armeabi-v7a"
#endif
#endif
#else
#define ABI "armeabi"
#endif
#elif defined(__i386__)
#define ABI "x86"
#elif defined(__x86_64__)
#define ABI "x86_64"
#elif defined(__mips64) /* mips64el-* toolchain defines __mips__ too */
#define ABI "mips64"
#elif defined(__mips__)
#define ABI "mips"
#elif defined(__aarch64__)
#define ABI "arm64-v8a"
#else
#define ABI "unknown"
#endif
string s = returnit();
jstring retval = env->NewStringUTF(s.c_str());
return retval;
}
}
现在如果我写fromhere.cpp如下:
#include <string>
using namespace std;
string returnit()
{
string s="Hello World";
return s;
}
我可以通过编写 fromhere.h 文件并在其中声明 returnit 来包含 fromhere.h,并且只在 Android.mk 的 LOCAL_SRC_FILES 和 [= 中包含上述文件的名称23=] 出现在我从 java class.
制作的文本视图中但我想将这些 fromhere.cpp 和 fromhere.h 编译为预构建的 .so 文件构建 ny ndk 并使用其中的 returnit() 函数。有人可以逐步向我解释如何在 Android Studio 中具体执行此操作吗?
废话请指正
您说您使用的是 Android Studio,但默认情况下 Android Studio 当前会忽略您的 Makefile 并使用它自己自动生成的 Makefile,不支持本机依赖项(目前)。
如果您停用内置支持并自己调用 ndk-build,请将类似的内容放入 build.gradle:
android {
sourceSets.main {
jniLibs.srcDir 'src/main/libs' //set libs as .so's location instead of jniLibs
jni.srcDirs = [] //disable automatic ndk-build call with auto-generated Android.mk
}
}
这是使用 Makefile 的解决方案:
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := fromhere.cpp
LOCAL_MODULE := fromhere
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH) # useless here, but if you change the location of the .h for your lib, you'll have to set its absolute path here.
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := hello-world.cpp
LOCAL_MODULE := hello-world
LOCAL_SHARED_LIBRARIES := fromhere
include $(BUILD_SHARED_LIBRARY)