2013-10-04 26 views
14

我从来没有在ndk上工作过。但是我有一个使用ndk的项目。无法运行程序“ ndk-build.cmd”:启动失败

它给我java.lang.UnsatisfiedLinkError: Native method not found:

我试图在谷歌搜索。我得到了很多链接 但所有都与jni.cpp文件 有关但是我的错误是在java文件中。所以我无法找到如何纠正它。

"java.lang.UnsatisfiedLinkError: Native method not found: il.co.telavivapp2u.onceapponatime.SharedResources.ocvBitmapPreMultAlpha:(Landroi‌​‌​d/graphics/Bitmap;Landroid/graphics/Bitmap;) 

我整合了NDK后this link。 这个项目是由另一个开发人员完成的 我们正在添加更多的功能。 这部分是由以前的开发完成的。

我刚刚添加了Google Search API活动和图库图片活动,它将在网格上显示图片。以前的开发者已经将一些图像放在可绘制的文件夹中,并将其显示在图库视图中。无论他在结尾做了什么,它都能够完美运行。即使现在也是。但我已经添加了相同的东西没有发生

点击应用程序可绘制的图库视图上的图像后,它将转到摄像机活动,它将以所选图像为背景捕获图像。然后我们可以编辑并保存该图像。但是如果手机图库和谷歌搜索图像捕获应用程序后,去ANR。

我已经设置NDK的路径和变量时,也能比得过我安装的C C++插件

而且控制台显示

Cannot run program "\ndk-build.cmd": Launching failed . 

我不能够理解我在哪里犯错。请帮帮我。

JNI FILE

的ANR发生在行号207

这里是我的代码:

package il.co.telavivapp2u.onceapponatime; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Locale; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Context; 
import android.content.res.Configuration; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Point; 
import android.os.Environment; 
import android.util.Log; 
import android.view.Display; 

public class SharedResources { 
    public static Bitmap bmpOld = null; 
    public static Bitmap bmpOldScaled = null; 
    public static Bitmap bmpNew = null; 
    public static Bitmap bmpNewScaled = null; 

    public static int scaledX = 0, scaledY = 0; 
    public static int dispX = 0, dispY = 0; 
    public static int fullX = 0, fullY = 0; 
    public static int picX = 0, picY = 0; 

    public static String fileDir = "/OnceAppOnATime/"; 
    public static String fileTempDir = fileDir + "/.temp/"; 
    public static String fileTempNew = fileTempDir + "/temp-new.jpg"; 
    public static String fileTempOld = fileTempDir + "/temp-old.jpg"; 
    public static String fileTempMask = fileTempDir + "/temp-mask.jpg"; 
    public static String fileTempBlend = fileTempDir + "/temp-blend.jpg"; 
    public static String fileTempRetouch = fileTempDir + "/temp-retouch.jpg"; 
    //public static String fileLastBlend = ""; 

    public static BitmapFactory.Options op = new BitmapFactory.Options(); 

    public static Locale localeHebrew = null; 

    public static int taskID = -1; 

    public static boolean Init(Activity activity) { return Init(activity, false); } 
    @SuppressLint("NewApi") 
    @SuppressWarnings("deprecation") 
    public static boolean Init(Activity activity, boolean force) { 
     if (dispX > 0 && dispY > 0) { // Don't re-init to avoid wrong file names 
      if (!force) 
       return false; 
     } else { 
      fileDir = Environment.getExternalStorageDirectory() + fileDir; 
      fileTempDir = Environment.getExternalStorageDirectory() + fileTempDir; 
      fileTempNew = Environment.getExternalStorageDirectory() + fileTempNew; 
      fileTempOld = Environment.getExternalStorageDirectory() + fileTempOld; 
      fileTempMask = Environment.getExternalStorageDirectory() + fileTempMask; 
      fileTempBlend = Environment.getExternalStorageDirectory() + fileTempBlend; 
      fileTempRetouch = Environment.getExternalStorageDirectory() + fileTempRetouch; 
     } 

     taskID = activity.getTaskId(); 

     // Find Hebrew locale, if available 
     Locale availableLocales[] = Locale.getAvailableLocales(); 
     for (int i = 0; i < availableLocales.length; ++i) { 
      String lang = availableLocales[i].getLanguage(); 
      if (lang.equals("he") || lang.equals("iw")) { 
       localeHebrew = availableLocales[i]; 
       break; 
      } 
     } 

     op.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     //op.inScaled = false; // Not needed if loading bitmaps from drawable-nodpi 
     op.inMutable = true; 

     Display display = activity.getWindowManager().getDefaultDisplay(); 
     if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB_MR2) { 
      dispX = display.getWidth(); 
      dispY = display.getHeight(); 
     } else { 
      Point dispSize = new Point(); 
      display.getSize(dispSize); 
      dispX = dispSize.x; 
      dispY = dispSize.y; 
     } 
     Log.w("Display Size", dispX + "x" + dispY); 
     //scaledX = dispX/2; scaledY = dispY/2; 
     scaledX = dispX; scaledY = dispY; 

     return true; 
    } 

    public static void setLocale(Activity activity, Locale locale) { 
     // This doesn't work reliably 
     Locale.setDefault(locale); 
     Configuration config = new Configuration(); 
     config.locale = locale; 
     activity.getBaseContext().getResources().updateConfiguration(config, 
      activity.getBaseContext().getResources().getDisplayMetrics()); 
    } 

    public static boolean haveScaling() { 
     return (dispX != scaledX || dispY != scaledY); 
    } 

    public static void SaveTempBitmap(Bitmap bitmap, String filename) { 
     try { 
      new File(fileTempDir).mkdirs(); 
      FileOutputStream out = new FileOutputStream(filename); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 98, out); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void RecycleOldBitmaps(boolean full, boolean scaled) { 
     if (full && bmpOld != null) { 
      bmpOld.recycle(); 
      bmpOld = null; 
     } 
     if (scaled && bmpOldScaled != null) { 
      bmpOldScaled.recycle(); 
      bmpOldScaled = null; 
     } 
    } 
    public static void RecycleNewBitmaps(boolean full, boolean scaled) { 
     if (full && bmpNew != null) { 
      bmpNew.recycle(); 
      bmpNew = null; 
     } 
     if (scaled && bmpNewScaled != null) { 
      bmpNewScaled.recycle(); 
      bmpNewScaled = null; 
     } 
    } 

    //            0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 
    public static int sample2sample[] = new int[] {1, 1, 2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 16, 
     16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32}; 
    public static Bitmap LoadScaledBitmap(Context ctx, int resId, float fracX, float fracY) { 
     // See: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 
     BitmapFactory.Options opts = new BitmapFactory.Options(); 
     opts.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(ctx.getResources(), resId, opts); 
     int imageHeight = opts.outHeight; 
     int imageWidth = opts.outWidth; 

     float requestX = dispX * fracX, requestY = dispY * fracY; 
     opts.inSampleSize = (int)(Math.min(imageWidth/requestX, imageHeight/requestY)); 
     if (opts.inSampleSize < 0 || opts.inSampleSize > 32) // Sometimes index=2147483647 for some reason... 
      opts.inSampleSize = 1; 
     opts.inSampleSize = sample2sample[opts.inSampleSize]; 
     Log.w("Bitmap Decoder", "Samples: " + opts.inSampleSize); 

     opts.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     //opts.inScaled = false; // Not needed if loading bitmaps from drawable-nodpi 
     opts.inMutable = true; 
     opts.inJustDecodeBounds = false; 
     return BitmapFactory.decodeResource(ctx.getResources(), resId, opts); 
    } 
    public static Bitmap LoadScaledBitmap(String filename, float fracX, float fracY) { 
     // See: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 
     BitmapFactory.Options opts = new BitmapFactory.Options(); 
     opts.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(filename, opts); 
     int imageHeight = opts.outHeight; 
     int imageWidth = opts.outWidth; 

     float requestX = dispX * fracX, requestY = dispY * fracY; 
     opts.inSampleSize = (int)(Math.min(imageWidth/requestX, imageHeight/requestY)); 
     if (opts.inSampleSize < 0 || opts.inSampleSize > 32) // Sometimes index=2147483647 for some reason... 
      opts.inSampleSize = 1; 
     opts.inSampleSize = sample2sample[opts.inSampleSize]; 
     Log.w("Bitmap Decoder", "Samples: " + opts.inSampleSize); 

     opts.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     //opts.inScaled = false; // Not needed if loading bitmaps from drawable-nodpi 
     opts.inMutable = true; 
     opts.inJustDecodeBounds = false; 
     return BitmapFactory.decodeFile(filename, opts); 
    } 

    public static String FileNameNow() { 
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.ENGLISH); 
     return fileDir + sdf.format(new Date()) + ".jpg"; 
    } 

    public static native void ocvBitmapPyramidalBlend(String fNew, String fOld, String fMask, String fBlend, int levels); 
    public static String ocvBitmapPyramidalBlendTimed(int levels) { 
     String fBlend = fileTempBlend;//FileNameNow(); 

     long t = System.nanoTime(); 
     ocvBitmapPyramidalBlend(fileTempNew, fileTempOld, fileTempMask, fBlend, levels); 
     long dt = (System.nanoTime() - t)/1000; // Microseconds 
     Log.w("OpenCV", "Blended (pyramidal) bitmaps in " + (dt/1000.0f) + " ms"); 

     //fileLastBlend = fBlend; 
     return fBlend; 
    } 

    public static native void ocvBitmapPreMultAlpha(Bitmap bitmapImg, Bitmap bitmapMask); 
    public static void ocvBitmapPreMultAlphaTimed(Bitmap bitmapImg, Bitmap bitmapMask) { 
     long t = System.nanoTime(); 
     ocvBitmapPreMultAlpha(bitmapImg, bitmapMask); 
     long dt = (System.nanoTime() - t)/1000; // Microseconds 
     Log.i("Native", "Applied premultiplied alpha to bitmap in " + (dt/1000.0f) + " ms"); 
    } 

    public static native void ocvBitmapContrastSaturationSet(Bitmap bitmapImg); 
    public static void ocvBitmapContrastSaturationSetTimed(Bitmap bitmapImg) { 
     long t = System.nanoTime(); 
     ocvBitmapContrastSaturationSet(bitmapImg); 
     long dt = (System.nanoTime() - t)/1000; // Microseconds 
     Log.i("Native", "Assigned contrast/saturation bitmap in " + (dt/1000.0f) + " ms"); 
    } 

    public static native void ocvBitmapContrastSaturationSrc(Bitmap bitmapImg, Bitmap bitmapSrc, float contrast, float saturation); 
    public static void ocvBitmapContrastSaturationSrcTimed(Bitmap bitmapImg, Bitmap bitmapSrc, float contrast, float saturation) { 
     long t = System.nanoTime(); 
     ocvBitmapContrastSaturationSrc(bitmapImg, bitmapSrc, contrast, saturation); 
     long dt = (System.nanoTime() - t)/1000; // Microseconds 
     Log.i("Native", "Applied contrast/saturation (from src) to bitmap in " + (dt/1000.0f) + " ms"); 
    } 

    public static native void ocvBitmapContrastSaturation(Bitmap bitmapImg, float contrast, float saturation); 
    public static void ocvBitmapContrastSaturationTimed(Bitmap bitmapImg, float contrast, float saturation) { 
     long t = System.nanoTime(); 
     ocvBitmapContrastSaturation(bitmapImg, contrast, saturation); 
     long dt = (System.nanoTime() - t)/1000; // Microseconds 
     Log.i("Native", "Applied contrast/saturation to bitmap in " + (dt/1000.0f) + " ms"); 
    } 

} 

而且right click on project - >Android Tools -> Add Native Support

Add Native Support is missing. I have Android Native Development Tools installed. Then also it's missing. 
+2

看起来你在你的IDE中配置了ndk-build的错误路径。然后使用zip文件工具验证一个或多个.so文件以.apk结尾。最后是你明确从Java加载库? –

+0

“D:\ NDK \ android-ndk-r9”这是我的NDk路径,我将它设置在我的ide中。 @ChrisStratton – TheLittleNaruto

+0

由于这个项目是由另一个开发人员完成的,所以我不确定你在说什么库。但是,项目的libs文件夹中有两个文件夹,一个是“armeabi”,另一个是“armeabi-v7a”。每个文件夹都包含两个.so文件。一个是“libOAOAT.so”,另一个是“libopencv_java.so”。还有一个使用“OpenCV Library - 2.4.3”的库位于同一个工作区中。 @ChrisStratton – TheLittleNaruto

回答

1

UnsatisfiedLinked错误是由于破坏java class和c class之间的桥梁; java中的方法名称应与C/C++类中的方法匹配。 虽然在Java和c/C++之间创建编译桥,所以如果方法名称不正确,它不会响应。 例子是以下 方法名injava是继

public native String Stub(){} 

应在JNI,但您的应用程序包名+类名+方法名像下面

JNIEXPORT jstring JNICALL Java_com_packageName_ClassName_MethodName 
+0

我也添加了JNI文件和SharedResources。函数名称相同 这是在SharedResources中public static native void ocvBitmapPreMultAlpha(Bitmap bitmapImg,Bitmap bitmapMask); 和JNI文件是一样的 – TheLittleNaruto

+0

分享你的Java和JNI方法的代码 –

+0

我想我已经共享已经看到我的问题底部。 – TheLittleNaruto

-1

关键的错误是“无法运行程序”是相同的ndk-build“:启动失败”。如果没有运行,则不会生成.so,这会导致UnsatisfiedLinkError。首先解决ndk-build问题。

尝试从“\ ndk-build.cmd”中删除最初的'\'。当我运行“ndk-build.cmd”时,它可以工作。

+0

这已经在评论中得到了广泛的报道。海报宣称.so文件存在于apk中 - 尽管可能并非如此(在当前版本中)等等。基本上,这个问题显示了去年秋天被抛弃的所有外观。 –

+0

我所看到的只是那些提到他们认为可能需要的Java代码更改的人,以及修复NDK路径的建议。这些帮助我都没有。删除斜线工作,并没有人建议。 – DrChandra

+0

五个月前发布的**第一条评论**中提出了该问题。 –

相关问题