在Android Studio中使用Android.mk打包C文件为so lib成功但真机运行时崩溃问题(入门问题)

嘤鸣求友 2016-09-08 04:17:04
非常基础的功能,就是分别建立一个C语言和C++返回两个数的值并在手机中显示:
在MainActivity同级建立一个Java类:UnityNdk0908.java

package example0831.studyopencv.com.unityndktest0908;

/**
* Created by wr on 2016/9/8.
*/
public class UnityNdk0908 {
public static native int AddCIntFun(int x, int y);
public static native float AddCppFloatFun(float x, float y);
static {
System.loadLibrary("UnityNativeLMF0908");//at example0831.studyopencv.com.unityndktest0908.UnityNdk0908.<clinit>(UnityNdk0908.java:10)
}
}

make生成class然后javah生成头文件放在jni文件夹里面,
UnityNativeCInt.c

//
// Created by wr on 2016/9/8.
//

#include "UnityNativeCInt.h"
#include "example0831_studyopencv_com_unityndktest0908_UnityNdk0908.h"

#ifndef __NATIVE_CODE__
#define __NATIVE_CODE__
//extern "C"
jint JNICALL Java_example0831_studyopencv_com_unityndktest0908_UnityNdk0908_AddCIntFun
(JNIEnv *env, jclass obj, jint x, jint y)
{
return (x+y);
}

#endif


UnityNativeCppFloat.cpp 文件:

//
// Created by wr on 2016/9/8.
//

#include "UnityNativeCppFloat.h"
#include "example0831_studyopencv_com_unityndktest0908_UnityNdk0908.h"
extern "C"
{
jfloat JNICALL Java_example0831_studyopencv_com_unityndktest0908_UnityNdk0908_AddCppFloatFun
(JNIEnv *env, jclass obj, jfloat x, jfloat y)
{
return (x + y);
}
}


Android.mk:
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := UnityNativeLM
LOCAL_MODULE_FILENAME := UnityNativeLMF0908
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_SRC_FILES := UnityNativeCInt.c \
UnityNativeCppFloat.cpp
LOCAL_LDLIBS := -llog -landroid

include $(BUILD_SHARED_LIBRARY)

————————————————
Application.mk:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-18


build.gradle:
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "24.0.0"

defaultConfig {
applicationId "example0831.studyopencv.com.unityndktest0908"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
}

sourceSets.main {
jni.srcDirs = []
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
commandLine "C:\\Users\\10155493\\AppData\\Local\\Android\\Sdk\\ndk-bundle\\ndk-build.cmd",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=src/main/jniLibs',
'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
'NDK_APPLICATION_MK=src/main/jni/Application.mk'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}



buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
.......

最后把MainActivity也写上吧:

package example0831.studyopencv.com.unityndktest0908;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "整形 = " + UnityNdk0908.AddCIntFun(1,2)+" 浮点型 = " + UnityNdk0908.AddCppFloatFun(1.5f, 99.1f),
Toast.LENGTH_LONG).show();//at example0831.studyopencv.com.unityndktest0908.MainActivity.onCreate(MainActivity.java:13)
}
}


结构图:




错误信息:
09-08 16:03:54.423 23203-23203/example0831.studyopencv.com.unityndktest0908 W/System: ClassLoader referenced unknown path: /data/app/example0831.studyopencv.com.unityndktest0908-1/lib/arm64
09-08 16:03:57.175 23203-23203/example0831.studyopencv.com.unityndktest0908 W/System: ClassLoader referenced unknown path: /data/app/example0831.studyopencv.com.unityndktest0908-1/lib/arm64
09-08 16:03:57.182 23203-23203/example0831.studyopencv.com.unityndktest0908 I/nubialog: DispatchMessage in MainLooper, delay:2772ms msg:{ when=-2s780ms what=110 obj=AppBindData{appInfo=ApplicationInfo{517e802 example0831.studyopencv.com.unityndktest0908}} target=android.app.ActivityThread$H }
09-08 16:03:57.299 23203-23203/example0831.studyopencv.com.unityndktest0908 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
09-08 16:03:57.338 23203-23203/example0831.studyopencv.com.unityndktest0908 D/AndroidRuntime: Shutting down VM
09-08 16:03:57.338 23203-23203/example0831.studyopencv.com.unityndktest0908 E/AndroidRuntime: FATAL EXCEPTION: main
Process: example0831.studyopencv.com.unityndktest0908, PID: 23203
java.lang.UnsatisfiedLinkError: com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader[DexPathList[[dex file "/data/data/example0831.studyopencv.com.unityndktest0908/files/instant-run/dex/slice-support-annotations-23.4.0_ebcab8a59fb52703cf07543e3b18b632f558f760-classes.dex", dex file "/data/data/example0831.studyopencv.com.unityndktest0908/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/example0831.studyopencv.com.unityndktest0908/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/example0831.studyopencv.com.unityndktest0908/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/example0831.studyopencv.com.unityndktest0908/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/example0831.studyopencv.com.unityndktest0908/files/instant-run/de..................
at java.lang.Runtime.loadLibrary(Runtime.java:367)
at java.lang.System.loadLibrary(System.java:1076)
at example0831.studyopencv.com.unityndktest0908.UnityNdk0908.<clinit>(UnityNdk0908.java:10)
at example0831.studyopencv.com.unityndktest0908.MainActivity.onCreate(MainActivity.java:13)

at android.app.Activity.performCreate(Activity.java:6301)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1121)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2455)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2562)
at android.app.ActivityThread.access$900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:160)
at android.app.ActivityThread.main(ActivityThread.java:5610)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
09-08 16:04:00.104 23203-23203/example0831.studyopencv.com.unityndktest0908 I/Process: Sending signal. PID: 23203 SIG: 9

卡了很长时间了,望各位前辈多多指点
...全文
275 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
嘤鸣求友 2016-09-13
  • 打赏
  • 举报
回复
这个比较简单吧,刚发完第二天就解决问题了……

80,473

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧