在单位的项目中,需要一个算法来计算一个特殊值,可是相关的部门却不给计算公式,只是给了几个封装好的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") from fileTree(dir: "libs", include: "**/*.so")
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 */ /* Header for class com_romaway_stocklib_StockIndex */
#ifndef _Included_com_romaway_stocklib_StockIndex #define _Included_com_romaway_stocklib_StockIndex * Class: com_romaway_stocklib_StockIndex * 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);
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 {
System.loadLibrary("StockIndex"); //引入包名
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
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);
public void onClick(View view) {
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];
new StockIndex().CalcZiWuLine(arrOpen, arrClose, arrHigh, arrLow, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9, arr10,
代码完成,现在编译,运行,报错。没事,这里只要设置一下。File--->Settings--->Plugins,将Android NDK Support后面的对勾去掉如下图
再运行,一切正常,得到了我想要的值
|