分享

android studio导入so文件,并调用其中的方法

 instl 2018-09-21

       在单位的项目中,需要一个算法来计算一个特殊值,可是相关的部门却不给计算公式,只是给了几个封装好的so文件和一个.h文件,让传入几个值,然后得到相应的值。拷。。。。。没法子,人家不给,我们只好调用这个了。

       这里在导入so文件和调用其中的方法时,遇到很多坑,这里做一个记录,以备以后查询。

       我使用的as1.5版本,一直没有升级呢。所以这里的所有方法,都是针对这个版本来的。

       第一步,我们将相关的so文件,复制到libs文件里。

                       这里,相关部门给出了所有应该有的文件夹的.so文件,如下图

                      

                       这里要注意一点,如果你加完之后,找不到.so文件,你看一下你有没有armeabi-v7a文件夹,在此文件夹里,一定要有so文件,不然在有些版本中就会出现错误。

       第二步,修改,我们app下面的build.gradle文件,添加如下代码

                      

  1. task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {
  2. destinationDir file("$projectDir/libs")
  3. baseName "Native_Libs2"
  4. extension "jar"
  5. from fileTree(dir: "libs", include: "**/*.so")
  6. into "lib"
  7. }

  8. tasks.withType(JavaCompile) {
  9. compileTask -> compileTask.dependsOn(nativeLibsToJar)
  10. }
                    这段代码,是与buildTypes同级的,如下图

                   

           第三步,建立相应的包与类文件

                          这里我们要根据给的.h文件来建立相应的包,文件名是这个com_romaway_stocklib_StockIndex.h那么,我们就要建立一个com.romaway.stocklib包,然后在此包中,建立StockIndex.class文件。建好后,如下图

                         

                         
          第四步,根据给出的.h文件,我们根据里面的方法,编写相应的方法,我的文件里,是这样的,只有一个方法,带了10多个数组。

                         下面是我的.h文件,我们

  1. /* DO NOT EDIT THIS FILE - it is machine generated */
  2. #include <jni.h>
  3. /* Header for class com_romaway_stocklib_StockIndex */

  4. #ifndef _Included_com_romaway_stocklib_StockIndex
  5. #define _Included_com_romaway_stocklib_StockIndex
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /*
  10. * Class: com_romaway_stocklib_StockIndex
  11. * Method: CalcZiWuLine
  12. * Signature: ([D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D[D)I
  13. *这里注意,看到_CalcZiWuLine了吗,这个就是你要编写的方法名了
  14.  */
  15.  JNIEXPORT jint JNICALL Java_com_romaway_stocklib_StockIndex_CalcZiWuLine
  16. (JNIEnv *, jclass, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray, jdoubleArray);

  17. #ifdef __cplusplus
  18. }
  19. #endif
  20. #endif
                    StockIndex.class文件如下

                

  1. public class StockIndex {


  2. public static native void CalcZiWuLine(double[] list_open,double[] list_close,double[] list_high,double[] list_low,

  3. double[] list1,double[] list2,double[] list3,double[] list4,double[] list5,
  4. double[] list6,double[] list7,double[] list8,double[] list9,double[] list10,
  5. double[] list11,double[] list12,double[] list13);
  6. }

           OK,当你写完之后,你会发现这页有提示有错,这里呢,我们先不去管它,一会做统一处理。

          第五步,在你想要的地方进行调用这个方法

        

  1. package com.example.cg.sofileopera;

  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.TextView;

  7. import com.romaway.stocklib.StockIndex;

  8. public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  9. static
  10. {
  11. System.loadLibrary("StockIndex"); //引入包名
  12. }


  13. private Button btn_view;
  14. private TextView txt_1;
  15. private TextView txt_2;
  16. private TextView txt_3;
  17. private TextView txt_4;
  18. private TextView txt_5;
  19. private TextView txt_6;
  20. private TextView txt_7;
  21. private TextView txt_8;
  22. private TextView txt_9;
  23. private TextView txt_10;
  24. private TextView txt_11;
  25. private TextView txt_12;
  26. private TextView txt_13;

  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);

  31. initControls();
  32. }

  33. /**
  34. * 初始化控件
  35. */
  36. private void initControls() {
  37. btn_view = (Button)findViewById(R.id.btn_view);
  38. btn_view.setOnClickListener(this);

  39. txt_1 = (TextView)findViewById(R.id.txt_1);
  40. txt_2 = (TextView)findViewById(R.id.txt_2);
  41. txt_3 = (TextView)findViewById(R.id.txt_3);
  42. txt_4 = (TextView)findViewById(R.id.txt_4);
  43. txt_5 = (TextView)findViewById(R.id.txt_5);
  44. txt_6 = (TextView)findViewById(R.id.txt_6);
  45. txt_7 = (TextView)findViewById(R.id.txt_7);
  46. txt_8 = (TextView)findViewById(R.id.txt_8);
  47. txt_9 = (TextView)findViewById(R.id.txt_9);
  48. txt_10 = (TextView)findViewById(R.id.txt_10);
  49. txt_11 = (TextView)findViewById(R.id.txt_11);
  50. txt_12 = (TextView)findViewById(R.id.txt_12);
  51. txt_13 = (TextView)findViewById(R.id.txt_13);
  52. }

  53. @Override
  54. public void onClick(View view) {
  55. switch (view.getId())
  56. {
  57. case R.id.btn_view:
  58. tempData();
  59. break;
  60. }
  61. }


  62. private void tempData() {

  63. double[] arrOpen = new double[5];
  64. double[] arrClose = new double[5];
  65. double[] arrHigh = new double[5];
  66. double[] arrLow = new double[5];



  67. double[] arr1 = new double[5];
  68. double[] arr2 = new double[5];
  69. double[] arr3 = new double[5];
  70. double[] arr4 = new double[5];
  71. double[] arr5 = new double[5];
  72. double[] arr6 = new double[5];
  73. double[] arr7 = new double[5];
  74. double[] arr8 = new double[5];
  75. double[] arr9 = new double[5];
  76. double[] arr10 = new double[5];
  77. double[] arr11 = new double[5];
  78. double[] arr12 = new double[5];
  79. double[] arr13 = new double[5];

  80. arrOpen[0] = 19.69;
  81. arrOpen[1] = 25.99;
  82. arrOpen[2] = 28.59;
  83. arrOpen[3] = 28.59;
  84. arrOpen[4] = 34.6;

  85. arrClose[0] = 23.63;
  86. arrClose[1] = 25.99;
  87. arrClose[2] = 28.59;
  88. arrClose[3] = 31.45;
  89. arrClose[4] = 34.6;

  90. arrHigh[0] = 23.63;
  91. arrHigh[1] = 25.99;
  92. arrHigh[2] = 28.59;
  93. arrHigh[3] = 31.45;
  94. arrHigh[4] = 34.6;

  95. arrLow[0] = 19.69;
  96. arrLow[1] = 25.99;
  97. arrLow[2] = 28.59;
  98. arrLow[3] = 31.45;
  99. arrLow[4] = 34.6;

  100. new StockIndex().CalcZiWuLine(arrOpen, arrClose, arrHigh, arrLow, arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9, arr10,
  101. arr11, arr12, arr13);

  102. String str1= "";
  103. String str2= "";
  104. String str3= "";
  105. String str4= "";
  106. String str5= "";
  107. String str6= "";
  108. String str7= "";
  109. String str8= "";
  110. String str9= "";
  111. String str10= "";
  112. String str11= "";
  113. String str12= "";
  114. String str13= "";

  115. for(int i=0;i<5;i++)
  116. {
  117. str1 += arr1[i] + ",";
  118. str2 += arr2[i] + ",";
  119. str3 += arr3[i] + ",";
  120. str4 += arr4[i] + ",";
  121. str5 += arr5[i] + ",";
  122. str6 += arr6[i] + ",";
  123. str7 += arr7[i] + ",";
  124. str8 += arr8[i] + ",";
  125. str9 += arr9[i] + ",";
  126. str10 += arr10[i] + ",";
  127. str11 += arr11[i] + ",";
  128. str12 += arr12[i] + ",";
  129. str13 += arr13[i] + ",";
  130. }

  131. txt_1.setText(str1);
  132. txt_2.setText(str2);
  133. txt_3.setText(str3);
  134. txt_4.setText(str4);
  135. txt_5.setText(str5);
  136. txt_6.setText(str6);
  137. txt_7.setText(str7);
  138. txt_8.setText(str8);
  139. txt_9.setText(str9);
  140. txt_10.setText(str10);
  141. txt_11.setText(str11);
  142. txt_12.setText(str12);
  143. txt_13.setText(str13);
  144. }
  145. }

        代码完成,现在编译,运行,报错。没事,这里只要设置一下。File--->Settings--->Plugins,将Android NDK Support后面的对勾去掉如下图

      

     
     再运行,一切正常,得到了我想要的值

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多