2013-10-04 23 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

五個月前發佈的**第一條評論**中提出了該問題。 –

相關問題