java.lang.UnsatisfiedLinkError:找不到本机方法:package.ClassName.stringFromJNI :()Ljava / lang / String;

时间:2015-05-10 09:15:54

标签: java android android-ndk

我正在开发一个Android应用程序,我正在使用NDK库,一切正常。在清理项目时,我收到了这个警告:

  

Android NDK:警告:APP_PLATFORM android-21比./AndroidManifest.xml中的android:minSdkVersion 3大

当我访问我的活动以查看我的本机代码的结果时,我得到以下异常:

05-10 13:01:33.001: E/AndroidRuntime(15024): java.lang.UnsatisfiedLinkError: Native method not found: com.zitza.murncy.activities.HelloJni.stringFromJNI:()Ljava/lang/String;
05-10 13:01:33.001: E/AndroidRuntime(15024):    at com.zitza.murncy.activities.HelloJni.stringFromJNI(Native Method)
05-10 13:01:33.001: E/AndroidRuntime(15024):    at com.zitza.murncy.activities.HelloJni.onCreate(HelloJni.java:36)

我的活动类以及头文件和清单如下所示。

我的HelloJni课程

package zitza.murncy.activities;

import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;


public class HelloJni extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        /* Create a TextView and set its content.
         * the text is retrieved by calling a native
         * function.
         */
        TextView  tv = new TextView(this);
        tv.setText( stringFromJNI() );
        setContentView(tv);
    }

    /* A native method that is implemented by the
     * 'hello-jni' native library, which is packaged
     * with this application.
     */
    public native String  stringFromJNI();

    /* This is another native method declaration that is *not*
     * implemented by 'hello-jni'. This is simply to show that
     * you can declare as many native methods in your Java code
     * as you want, their implementation is searched in the
     * currently loaded native libraries only the first time
     * you call them.
     *
     * Trying to call this function will result in a
     * java.lang.UnsatisfiedLinkError exception !
     */
    public native String  unimplementedStringFromJNI();

    /* this is used to load the 'hello-jni' library on application
     * startup. The library has already been unpacked into
     * /data/data/com.example.hellojni/lib/libhello-jni.so at
     * installation time by the package manager.
     */
    static {
        System.loadLibrary("hello-jni");
    }
}

我的Android.mk文件

# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.c

include $(BUILD_SHARED_LIBRARY)

我的Application.mk文件

APP_ABI := all

我的hello-jni.c

#include <string.h>
#include <jni.h>

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java
 */
jstring
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

    return (*env)->NewStringUTF(env, "AIzaSUhfkedpl0AS8" ABI ".");
}

我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.murncy"
    android:versionCode="3"
    android:versionName="3.0" >

<!--     <uses-sdk android:minSdkVersion="3" /> -->

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

    <!-- If c2dm permission is removed, parse starts working, else c2dm takes control -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.murgency.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.murncy.permission.C2D_MESSAGE" />

    <application
        android:name="com.zitza.murncy.MUrgencyApplication"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/Sherlock.__Theme.Light" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAiJ3CMj_kKyjgWa-tNSLjUhfkedpl0AS8" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />



        <activity
            android:name="com.zitza.murncy.activities.HelloJni"
            android:label="Testing Native Code"
            android:screenOrientation="portrait" />




        <activity
            android:name="com.zitza.murncy.activities.SplashScreen"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme"
            android:windowSoftInputMode="stateAlwaysHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>



        <!-- Contact change listener on boot -->

        <service android:name="com.zitza.services.ContactService" />

        <receiver
            android:name="com.zitza.services.BootReceiver"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service android:name="com.zitza.services.ContactService" />
        <service
            android:name="com.zitza.services.GPSLoggerService"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.ACCESS_FINE_LOCATION" >
        </service>

        <!-- Parse push -->
        <service android:name="com.parse.PushService" />

<!--         <receiver -->
<!--             android:name="com.parse.GcmBroadcastReceiver" -->
<!--             android:permission="com.google.android.c2dm.permission.SEND" > -->
<!--             <intent-filter> -->
<!--                 <action android:name="com.google.android.c2dm.intent.RECEIVE" /> -->
<!--                 <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> -->

<!--                 <category android:name="com.murgency" /> -->
<!--             </intent-filter> -->
<!--         </receiver> -->

        <receiver android:name="com.parse.ParseBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>

        <!-- Custom receiver -->
<!--         <receiver -->
<!--             android:name="com.zitza.services.ParsePushReceiver" -->
<!--             android:exported="false" > -->
<!--             <intent-filter> -->
<!--                 <action android:name="android.intent.action.BOOT_COMPLETED" /> -->
<!--                 <action android:name="android.intent.action.USER_PRESENT" /> -->
<!--                 <action android:name="com.murgency.UPDATE_STATUS" /> -->
<!--             </intent-filter> -->
<!--         </receiver> -->

        <!-- GCM -->
        <service
            android:name="com.zitza.services.GcmIntentService"
            android:enabled="true" />

        <receiver
            android:name="com.zitza.services.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.RECEIVE" >
            <intent-filter android:priority="1" >
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.murncy.UPDATE_STATUS" />

                <category android:name="com.murncy" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

要获取本机功能的正确签名,您可以自动创建JNI标头:

/* JNI header: this header can be build automatically with "javah":
 * javah -classpath bin/classes -d jni activities.murncy.zitza.HelloJni
 * (maybe you have to add "-bootclasspath /opt/android-sdk/platforms/android-21/android.jar") 
*/
#include "activities_murncy_zitza_HelloJni.h"

要避免此警告,您可以将以下内容添加到Application.mk

# minimium Android version 
APP_PLATFORM := android-10

修改

javah -bootclasspath /opt/android-sdk/platforms/android-10/android.jar -classpath bin/classes -d jni activities.murncy.zitza.HelloJni

(当然,您必须调整安装Android SDK的路径)