在单位的项目中,需要一个算法来计算一个特殊值,可是相关的部门却不给计算公式,只是给了几个封装好的so文件和一个.h文件,让传入几个值,然后得到相应的值。拷。。。。。没法子,人家不给,我们只好调用这个了。
这里在导入so文件和调用其中的方法时,遇到很多坑,这里做一个记录,以备以后查询。
我使用的as1.5版本,一直没有升级呢。所以这里的所有方法,都是针对这个版本来的。
第一步,我们将相关的so文件,复制到libs文件里。
这里,相关部门给出了所有应该有的文件夹的.so文件,如下图
这里要注意一点,如果你加完之后,找不到.so文件,你看一下你有没有armeabi-v7a文件夹,在此文件夹里,一定要有so文件,不然在有些版本中就会出现错误。
第二步,修改,我们app下面的build.gradle文件,添加如下代码
task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {
destinationDir file("$projectDir/libs")
baseName "Native_Libs2"
extension "jar"
from fileTree(dir: "libs", include: "**/*.so")
into "lib"
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
} 这段代码,是与buildTypes同级的,如下图
第三步,建立相应的包与类文件
这里我们要根据给的.h文件来建立相应的包,文件名是这个com_romaway_stocklib_StockIndex.h那么,我们就要建立一个com.romaway.stocklib包,然后在此包中,建立StockIndex.class文件。建好后,如下图
第四步,根据给出的.h文件,我们根据里面的方法,编写相应的方法,我的文件里,是这样的,只有一个方法,带了10多个数组。
下面是我的.h文件,我们/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class com_romaway_stocklib_StockIndex */
#ifndef _Included_com_romaway_stocklib_StockIndex
#define _Included_com_romaway_stocklib_StockIndex
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_romaway_stocklib_StockIndex
* Method: CalcZiWuLine
* Signature: ([D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D)I
*这里注意,看到_CalcZiWuLine了吗,这个就是你要编写的方法名了
*/
JNIEXPORT jint JNICALL Java_com_romaway_stocklib_StockIndex_CalcZiWuLine
(JNIEnv *, jclass, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray);
#ifdef __cplusplus
}
#endif
#endif StockIndex.class文件如下
public class StockIndex{
public static native void CalcZiWuLine(double[] list_open,double[] list_close,double[] list_high,double[] list_low,
double[] list1,double[] list2,double[] list3,double[] list4,double[] list5,
double[] list6,double[] list7,double[] list8,double[] list9,double[] list10,
double[] list11,double[] list12,double[] list13);
}
OK,当你写完之后,你会发现这页有提示有错,这里呢,我们先不去管它,一会做统一处理。
第五步,在你想要的地方进行调用这个方法
package com.example.cg.sofileopera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.romaway.stocklib.StockIndex;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
static
{
System.loadLibrary("StockIndex"); //引入包名
}
private Button btn_view;
private TextView txt_1;
private TextView txt_2;
private TextView txt_3;
private TextView txt_4;
private TextView txt_5;
private TextView txt_6;
private TextView txt_7;
private TextView txt_8;
private TextView txt_9;
private TextView txt_10;
private TextView txt_11;
private TextView txt_12;
private TextView txt_13;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControls();
}
/**
* 初始化控件
*/
private void initControls(){
btn_view = (Button)findViewById(R.id.btn_view);
btn_view.setOnClickListener(this);
txt_1 = (TextView)findViewById(R.id.txt_1);
txt_2 = (TextView)findViewById(R.id.txt_2);
txt_3 = (TextView)findViewById(R.id.txt_3);
txt_4 = (TextView)findViewById(R.id.txt_4);
txt_5 = (TextView)findViewById(R.id.txt_5);
txt_6 = (TextView)findViewById(R.id.txt_6);
txt_7 = (TextView)findViewById(R.id.txt_7);
txt_8 = (TextView)findViewById(R.id.txt_8);
txt_9 = (TextView)findViewById(R.id.txt_9);
txt_10 = (TextView)findViewById(R.id.txt_10);
txt_11 = (TextView)findViewById(R.id.txt_11);
txt_12 = (TextView)findViewById(R.id.txt_12);
txt_13 = (TextView)findViewById(R.id.txt_13);
}
@Override
public void onClick(View view){
switch (view.getId())
{
case R.id.btn_view:
tempData();
break;
}
}
private void tempData(){
double[] arrOpen = new double[5];
double[] arrClose = new double[5];
double[] arrHigh = new double[5];
double[] arrLow = new double[5];
double[] arr1 = new double[5];
double[] arr2 = new double[5];
double[] arr3 = new double[5];
double[] arr4 = new double[5];
double[] arr5 = new double[5];
double[] arr6 = new double[5];
double[] arr7 = new double[5];
double[] arr8 = new double[5];
double[] arr9 = new double[5];
double[] arr10 = new double[5];
double[] arr11 = new double[5];
double[] arr12 = new double[5];
double[] arr13 = new double[5];
arrOpen[0] = 19.69;
arrOpen[1] = 25.99;
arrOpen[2] = 28.59;
arrOpen[3] = 28.59;
arrOpen[4] = 34.6;
arrClose[0] = 23.63;
arrClose[1] = 25.99;
arrClose[2] = 28.59;
arrClose[3] = 31.45;
arrClose[4] = 34.6;
arrHigh[0] = 23.63;
arrHigh[1] = 25.99;
arrHigh[2] = 28.59;
arrHigh[3] = 31.45;
arrHigh[4] = 34.6;
arrLow[0] = 19.69;
arrLow[1] = 25.99;
arrLow[2] = 28.59;
arrLow[3] = 31.45;
arrLow[4] = 34.6;
new StockIndex().CalcZiWuLine(arrOpen, arrClose, arrHigh, arrLow, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9, arr10,
arr11, arr12, arr13);
String str1= "";
String str2= "";
String str3= "";
String str4= "";
String str5= "";
String str6= "";
String str7= "";
String str8= "";
String str9= "";
String str10= "";
String str11= "";
String str12= "";
String str13= "";
for(int i=0;i<5;i++)
{
str1 += arr1[i] + ",";
str2 += arr2[i] + ",";
str3 += arr3[i] + ",";
str4 += arr4[i] + ",";
str5 += arr5[i] + ",";
str6 += arr6[i] + ",";
str7 += arr7[i] + ",";
str8 += arr8[i] + ",";
str9 += arr9[i] + ",";
str10 += arr10[i] + ",";
str11 += arr11[i] + ",";
str12 += arr12[i] + ",";
str13 += arr13[i] + ",";
}
txt_1.setText(str1);
txt_2.setText(str2);
txt_3.setText(str3);
txt_4.setText(str4);
txt_5.setText(str5);
txt_6.setText(str6);
txt_7.setText(str7);
txt_8.setText(str8);
txt_9.setText(str9);
txt_10.setText(str10);
txt_11.setText(str11);
txt_12.setText(str12);
txt_13.setText(str13);
}
}
代码完成,现在编译,运行,报错。没事,这里只要设置一下。File--->Settings--->Plugins,将Android NDK Support后面的对勾去掉如下图
再运行,一切正常,得到了我想要的值
如果觉得《android 引入 .so android studio导入so文件 并调用其中的方法》对你有帮助,请点赞、收藏,并留下你的观点哦!