java windows so文件_windows下编译使用NDK,调用SO文件 | 学步园
下載后把壓縮包解壓出來,例如:D:\ndk,目錄下的ndk-build.cmd就是用來編譯的批處理命令。
這里以D:\ndk\samples\hello-jni為例,打開D:\ndk\samples\hello-jni\jni\hello-jni.c查看代碼:
/** 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.
**/
#include
#include
/*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 )
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
因為我們是拿這個c源碼文件來使用,如果遷就Java_com_example_hellojni_HelloJni_stringFromJNI
函數名的話,在我們的android工程中java類的聲明就要是:com/example/hellojni/HelloJni.java。
反之,如果我們的android工程已經創建好,并且包名是com.example.hellojni,stringFromJNI函數我們寫在了MainActivity.java類里,
那么這里的c函數就要修改為:Java_com_example_hellojni_MainActivity_stringFromJNI,否則在尋找函數時會找不到。
然后執行命令:D:\ndk\ndk-build.cmd(如果設置過環境變量則直接使用ndk-build.cmd)來編譯hello-jni,如果沒有錯誤會輸出:
Gdbserver????? : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver
Gdbsetup?????? : libs/armeabi/gdb.setup"Compile thumb : hello-jni <= hello-jni.cSharedLibrary? : libhello-jni.soInstall??????? : libhello-jni.so => libs/armeabi/libhello-jni.so
編譯成功會在D:\ndk\samples\hello-jni\libs\armeabi目錄下生成libhello-jni.so文件。
三、創建android應用程序并使用so文件
打開eclipse創建一個android應用程序HelloJni,默認的com.example.hellojni包下面有一個MainActivity.java,
在此包下添加一個HelloJni.java,
package com.example.hellojni;
public class HelloJni {
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 !*/
}
MainActivity.java修改為:
package com.example.hellojni;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
HelloJni jni = new HelloJni();
TextView tv = new TextView(this);
String str = jni.stringFromJNI();
Log.d("HelloJni", str);
tv.setText(str);
setContentView(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu);
return true;
}
static {
System.loadLibrary("hello-jni");
}
}
把編譯生成的libhello-jni.so文件復制到F:\eclipse_workspace\HelloJni\libs\armeabi目錄下(armeabi如果不存在則手動創建之),然后編譯運行,效果圖如下:
參考資料:
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的java windows so文件_windows下编译使用NDK,调用SO文件 | 学步园的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谷歌翻译,一键复活脚本
- 下一篇: Pytorch 自定义激活函数前向与反向