Android Studio中调用So库方法

in android •  7 years ago 

在Android Studio中so库的存放方式有两种:
(1) 将AS切换到Project目录,在main下新建jniLibs,在jniLibs下新建armeabi-v7a,把so库copy进去;
(2)在C/C++编译目录下的CmakeLists.txt里设置:
#设置生成的so动态库最后输出的路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../app/src/main/jniLibs/${ANDROID_ABI})
编译的时候就会将so库打包进apk。
调用So库里的方法也有两种方式:
(1)So库有对应的jar包,把jar包放进libs里就可以调用so库里面的方法了,当然得先把so放进jniLibs里;
(2)只有So库,没有jar包,又想调用so里的方法,前提当然是要把so库放进jniLibs里(可以把so放在系统创建的app的build/intermediates/debug/obj/armemba-v7a或build/intermediates/release/obj/armemba-v7a,然后编译)
创建一个类,调用so库里的方法:
package com.tcp;

import android.util.Log;

//cd src
//javah -d ../jni com.tcp.AndroidCallJni
public class AndroidCallJni {

static  
{  
   //System.loadLibrary("c2android");
   System.loadLibrary("android2c");
} 
static private final String TAG = "AndroidCallJni";
    
static public void proccessRecvMessage(String recv)
{
    // TODO Auto-generated method stub
    Log.e(TAG,"recv="+recv);
    Log.e(TAG,"测试测试......"+recv);
    
}   

static public native   int    Start(String userId, String sip, int sport, int hport);
static public native   int    SendMessage(String msg);
//static public native   int    Exit(); 

}

注意,C里的包名必须与上述类对应,应该是:com.tcp.AndroidCallJni.调用时,要在你的工程里创建com.tcp.AndroidCallJni类,然后在MainActivity里: AndroidCallJni.Start("1704", serverIP, 9509, 9529);

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!