Example #1
0
  // 加载提取出来的SO文件
  static boolean _loadUnzipSo(String libname, int version) {
    boolean initSuc = false;
    try {

      // 尝试libwebp.so
      Log.w(LOGTAG, "init my so libray:" + libname);
      if (isExist(libname, version)) {
        System.load(_targetSoFile(libname, version));
      } else {
        Log.e(LOGTAG, "so not in fetched place:" + libname);
        TBS.Ext.commitEvent(EventID_SO_INIT, "so not in fetched place:" + libname);
      }
      initSuc = true;
    } catch (Exception e) {
      initSuc = false;
      Log.e(LOGTAG, "init so failed library:" + libname + ",e=" + e.getMessage());
      e.printStackTrace();
      TBS.Ext.commitEvent(
          EventID_SO_INIT, "so not in fetched place:" + libname + ",e=" + e.getMessage());
    } catch (java.lang.UnsatisfiedLinkError e2) {
      initSuc = false;
      Log.e(LOGTAG, "init so failed failed(UnsatisfiedLinkError):" + e2.getMessage());
      e2.printStackTrace();
      TBS.Ext.commitEvent(
          EventID_SO_INIT, "so not in fetched place:" + libname + ",e=" + e2.getMessage());

    } catch (java.lang.Error e3) {
      initSuc = false;
      e3.printStackTrace();
      TBS.Ext.commitEvent(
          EventID_SO_INIT, "so not in fetched place:" + libname + ",e=" + e3.getMessage());
    }
    return initSuc;
  }
Example #2
0
  /**
   * 完成加载so库的操作,先尝试用loadlibrary加载,如果失败则从apk中提取出armebi下的so,再加载。
   *
   * @param libname 库名字,例如webp,不必为libwep.so
   * @param version 版本号,正整数,新的版本>老的版本
   */
  public static boolean initSo(String libName, int version) {

    boolean InitSuc = false;

    // 首先,通过System.loadLibrary方法,从libs下库加载
    try {

      Log.i(LOGTAG, "init libray: " + libName);
      System.loadLibrary(libName);
      Log.i(LOGTAG, "init libray sucess:" + libName);

      InitSuc = true;
    } catch (Exception e) {
      InitSuc = false;
      Log.e(LOGTAG, "init libray failed:" + e.getMessage());
      e.printStackTrace();
    } catch (java.lang.UnsatisfiedLinkError e2) {
      InitSuc = false;
      Log.e(LOGTAG, "init library failed(UnsatisfiedLinkError):" + e2.getMessage());
      e2.printStackTrace();

    } catch (java.lang.Error e3) {
      InitSuc = false;
      e3.printStackTrace();
      Log.e(LOGTAG, "init library failed(Error):" + e3.getMessage());
    }

    try {

      if (!InitSuc) {

        // 从apk中提取的文件已经存在
        if (isExist(libName, version)) {
          boolean res = _loadUnzipSo(libName, version);
          if (res) return res;
          else {
            removeSoIfExit(libName, version);
            // 以有的SO有问题,删除然后尝试再解
            TBS.Ext.commitEvent(EventID_SO_INIT, "the exist target so is bad lib:" + libName);
          }
        }

        // 从libs下加载失败,则从apk中读出SO,加载
        String cpuType = _cpuType();
        if (cpuType.equalsIgnoreCase(MIPS) || cpuType.equalsIgnoreCase(X86)) {
          Log.w(LOGTAG, "cpu type" + cpuType + " no so in libs");
          TBS.Ext.commitEvent(
              EventID_SO_INIT, "no so in libs for cpu:" + cpuType + " ,lib:" + libName);

        } else {
          try {
            InitSuc = unZipSelectedFiles(libName, version);
          } catch (ZipException e) {
            e.printStackTrace();
            TBS.Ext.commitEvent(
                EventID_SO_INIT, "no so in libs for cpu:" + cpuType + " ,lib:" + libName);
          } catch (IOException e2) {
            e2.printStackTrace();
            TBS.Ext.commitEvent(
                EventID_SO_INIT, "no so in libs for cpu:" + cpuType + " ,lib:" + libName);
          }
        }
      }
    } catch (Exception e) {
      InitSuc = false;
      Log.e(LOGTAG, "init libray failed:" + e.getMessage());
      e.printStackTrace();
      TBS.Ext.commitEvent(
          EventID_SO_INIT, "unzip andload fail lib:" + libName + ",e=" + e.getMessage());
    } catch (java.lang.UnsatisfiedLinkError e2) {
      InitSuc = false;
      Log.e(LOGTAG, "init library failed(UnsatisfiedLinkError):" + e2.getMessage());
      e2.printStackTrace();
      TBS.Ext.commitEvent(
          EventID_SO_INIT,
          "unzip andload failed(UnsatisfiedLinkError) lib:" + libName + ",e=" + e2.getMessage());

    } catch (java.lang.Error e3) {
      InitSuc = false;
      Log.e(LOGTAG, "init library failed(Error):" + e3.getMessage());
      e3.printStackTrace();
      TBS.Ext.commitEvent(
          EventID_SO_INIT, "unzip andload failed(Error) lib:" + libName + ",e=" + e3.getMessage());
    }

    if (!InitSuc) {
      Log.e(LOGTAG, "initSo return false lib: " + libName);
      TBS.Ext.commitEvent(
          EventID_SO_INIT, "initSo return false lib: " + libName + ",cputype:" + _cpuType());
    }
    return InitSuc;
  }
Example #3
0
  static boolean unZipSelectedFiles(String libname, int version) throws ZipException, IOException {

    String sourcePath = "lib/armeabi/lib" + libname + ".so";
    try {
      String zipPath = "";
      Context context = mContext;
      if (context == null) {
        TBS.Ext.commitEvent(EventID_SO_INIT, "TaoApplication.context is null lib:" + libname);
        return false;
      }

      ApplicationInfo aInfo = context.getApplicationInfo();
      if (null != aInfo) {
        zipPath = aInfo.sourceDir;
      }

      ZipFile zf;
      zf = new ZipFile(zipPath);

      for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
        ZipEntry entry = ((ZipEntry) entries.nextElement());
        if (entry.getName().startsWith(sourcePath)) {

          InputStream in = null;
          FileOutputStream os = null;
          FileChannel channel = null;
          int total = 0;
          try {

            // 确保老的库被删除,这代码应该是不跑到的,因为最开始需要检查只有在不存在的情况下,才提取
            removeSoIfExit(libname, version);

            // copy 文件
            in = zf.getInputStream(entry);
            os =
                context.openFileOutput(
                    "lib" + libname + "bk" + version + ".so", Context.MODE_PRIVATE);
            channel = os.getChannel();

            byte[] buffers = new byte[1024];
            int realLength;

            while ((realLength = in.read(buffers)) > 0) {
              // os.write(buffers);
              channel.write(ByteBuffer.wrap(buffers, 0, realLength));
              total += realLength;
            }
            Log.i(LOGTAG, "so filesize:" + total);
          } finally {
            if (in != null) {
              try {
                in.close();
              } catch (Exception e) {
                e.printStackTrace();
              }
            }

            if (channel != null)
              try {
                channel.close();
              } catch (Exception e) {
                e.printStackTrace();
              }

            if (os != null)
              try {
                os.close();
              } catch (Exception e) {
                e.printStackTrace();
              }

            if (zf != null) {
              zf.close();
              zf = null;
            }
          }

          if (total > 0) {
            return _loadUnzipSo(libname, version);
          } else {
            TBS.Ext.commitEvent(EventID_SO_INIT, "unzip fail:" + libname);
            return false;
          }
        }
      }
    } catch (java.io.IOException e) {
      e.printStackTrace();
      TBS.Ext.commitEvent(EventID_SO_INIT, "unzip fail:" + libname + ",e=" + e.getMessage());
    }
    return false;
  }