gpt4 book ai didi

使用 android-ndk-r9 的 Android NDK 应用程序在最终 apk 中缺少 *.so 文件并且未找到 native 方法

转载 作者:行者123 更新时间:2023-11-29 01:48:16 27 4
gpt4 key购买 nike

更新:我设法用最终 apk 打包 *.so 文件,只需将其附加到 build.gradle:

    tasks.withType(com.android.build.gradle.tasks.PackageApplication) {
pkgTask -> pkgTask.jniDir new File(projectDir, 'src/main/libs')
}

但是现在native函数没有被调用。

native.c代码

    #include <jni.h>
#include <string.h>
#include <android/log.h>

JNIEXPORT jstring JNICALL Java_com_myndkapp_mybullet_MainActivity_helloLog(JNIEnv * env, jobject this) {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", "Check string you received in the Activity");
return (*env)->NewStringUTF(env, "Hello from Native code :)");
}

来自 MainActivity.java 的 fragment :

    .
..
static {
System.loadLibrary("myfndk");
}
private native String helloLog();
.
..

ADB 日志的一些痕迹在这里:

    11-18 16:53:43.261    3469-3469/? D/PackageAddedReceiver﹕ package added com.myndkapp.mybullet
11-18 16:53:43.856 2963-3214/? V/SmartFaceService - 3rd party pause﹕ onReceive [android.intent.action.ACTIVITY_STATE/com.myndkapp.mybullet/create]
11-18 16:53:43.886 2823-2823/? D/dalvikvm﹕ Trying to load lib /data/app-lib/com.myndkapp.mybullet-1/libmyfndk.so 0x42a99270
11-18 16:53:43.886 2823-2823/? D/dalvikvm﹕ Added shared lib /data/app-lib/com.myndkapp.mybullet-1/libmyfndk.so 0x42a99270
11-18 16:53:43.886 2823-2823/? D/dalvikvm﹕ No JNI_OnLoad found in /data/app-lib/com.myndkapp.mybullet-1/libmyfndk.so 0x42a99270, skipping init
11-18 16:53:50.596 2823-2823/? W/dalvikvm﹕ No implementation found for native Lcom/myndkapp/mybullet/MainActivity;.helloLog:()Ljava/lang/String;
11-18 16:53:50.606 2823-2823/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.UnsatisfiedLinkError: Native method not found: com.myndkapp.mybullet.MainActivity.helloLog:()Ljava/lang/String;
at com.myndkapp.mybullet.MainActivity.helloLog(Native Method)
at com.myndkapp.mybullet.MainActivity.access$000(MainActivity.java:15)
at com.myndkapp.mybullet.MainActivity$1.onClick(MainActivity.java:35)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
11-18 16:53:50.691 2963-3214/? V/SmartFaceService - 3rd party pause﹕ onReceive [android.intent.action.ACTIVITY_STATE/com.myndkapp.mybullet/pause]
11-18 16:53:50.696 2963-3043/? D/CrashAnrDetector﹕ processName: com.myndkapp.mybullet
11-18 16:53:50.696 2963-3043/? D/CrashAnrDetector﹕ broadcastEvent : com.myndkapp.mybullet data_app_crash
11-18 16:54:05.781 2963-3449/? I/ActivityManager﹕ Process com.myndkapp.mybullet (pid 2823) (adj 9) has died.

我正在尝试在 Fedora 19 x86_64 上的 Android Studio 0.3.5 132.910074 中使用 android-ndk-r9 构建一个 android NDK 应用.

看起来 libmyfndk.so 没有添加到最终的 apk 文件中。我通过查看 apk 文件的内容确认了这一点。当我在我的 Galaxy S4 (Jelly Bean 4.3) 上运行这个测试应用程序时,它会抛出错误。

    11-17 19:03:28.816  26472-26472/com.myndkapp.mybullet E/AndroidRuntime﹕ 
FATAL EXCEPTION: main
java.lang.UnsatisfiedLinkError: Couldn't load myfndk from loader
dalvik.system.PathClassLoader[dexPath=/data/app/com.myndkapp.mybullet-1.apk,
libraryPath=/data/app-lib/com.myndkapp.mybullet-1]: findLibrary returned null

我在这里寻找解决方案:

1.) https://groups.google.com/forum/#!msg/adt-dev/xj51eCWwhFw/pfhvCoquPysJ

2.) https://groups.google.com/forum/?fromgroups#!searchin/adt-dev/so/adt-dev/nQobKd2Gl_8/Z5yWAvCh4h4J

3.) Android studio, gradle and NDK

3.1) https://stackoverflow.com/a/16790675/1673000 (This worked for me)

4.) https://github.com/OnlyInAmerica/Android-JNI-Gradle

5.) Include .so library in apk in android studio

这些似乎都不起作用。

这是我的 build-gradle 文件的内容

    buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.3'
}
}
apply plugin: 'android'

repositories {
mavenCentral()
}

android {
compileSdkVersion 18
buildToolsVersion '19.0.0'

defaultConfig {
minSdkVersion 10
targetSdkVersion 19
}
}

dependencies {
compile 'com.android.support:appcompat-v7:+'
}

任何提示,为什么没有调用 Native 函数?

最佳答案

它没有将 native.c 的代码放在 libmyfndk.so 中,那是因为我的 missing '=' Android.mk 在这一行:

LOCAL_SRC_FILES : native.c

将以上行更改为 LOCAL_SRC_FILES := native.c,解决了这个问题。

关于使用 android-ndk-r9 的 Android NDK 应用程序在最终 apk 中缺少 *.so 文件并且未找到 native 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20032153/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com