第一步将So文件导入到jnilibs目录下
第二步配置build.gradle如下所示
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
}
布局文件为
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/jnitext"
android:text="Hello"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:textColor="#f0f"
android:textSize="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
package org.ccser.warning; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import org.w3c.dom.Text; /** * Created by Administrator on 2016/5/12. */ public class JniTextActivity extends Activity{ private TextView textView; //编译一下So文件注意下 导如的So文件为libPhxLib.so 在编译的时候去掉前面的lib和.so后缀 static { System.loadLibrary("PhxLib"); } //这里声明一个native方法 方法名字必须跟So文件保持一致。 public native String getVersion(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.jintextactivity); textView = (TextView) findViewById(R.id.jnitext); Log.d("TAG", getVersion()); textView.setText(getVersion()); } }