Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

[WEEX-454][Android] fix can't find libweexjss when deploy #1259

Merged
merged 1 commit into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions android/sdk/src/main/java/com/taobao/weex/WXEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,26 @@
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.taobao.weex.common.WXConfig;
import com.taobao.weex.utils.FontDO;
import com.taobao.weex.utils.LogLevel;
import com.taobao.weex.utils.TypefaceUtil;
import com.taobao.weex.utils.WXFileUtils;
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.WXSoInstallMgrSdk;
import com.taobao.weex.utils.WXUtils;

import org.w3c.dom.Text;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import dalvik.system.PathClassLoader;

public class WXEnvironment {

public static final String OS = "android";
Expand Down Expand Up @@ -100,6 +107,9 @@ public class WXEnvironment {
private static String sGlobalFontFamily;

public static final String CORE_SO_NAME = "weexcore";
public static final String CORE_JSS_SO_NAME = "weexjss";

private static String CORE_JSS_SO_PATH = null;

private static Map<String, String> options = new HashMap<>();
static {
Expand Down Expand Up @@ -334,4 +344,65 @@ public static void setApkDebugable(boolean debugable){
openDebugLog = false;
}
}

public static String findSoPath(String libName) {
final String libPath = ((PathClassLoader) (WXEnvironment.class.getClassLoader())).findLibrary(libName);
WXLogUtils.e(libName + "'s Path is" + libPath);
return libPath;
}

public static String getCacheDir() {
final Application application = getApplication();
if (application == null || application.getApplicationContext() == null)
return null;
return application.getApplicationContext().getCacheDir().getPath();
}

public static boolean extractSo() {
File sourceFile = new File(getApplication().getApplicationContext().getApplicationInfo().sourceDir);
final String cacheDir = getCacheDir();
if (sourceFile.exists() && !TextUtils.isEmpty(cacheDir)) {
try {
WXFileUtils.extractSo(sourceFile.getAbsolutePath(), cacheDir);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
return false;
}

private static String findLibJssRealPath() {
String soPath = findSoPath(CORE_JSS_SO_NAME);
String realName = "lib" + CORE_JSS_SO_NAME + ".so";
if (TextUtils.isEmpty(soPath)) {
String cacheDir = getCacheDir();
if (TextUtils.isEmpty(cacheDir)) {
return "";
}
if (cacheDir.indexOf("/cache") > 0) {
soPath = new File(cacheDir.replace("/cache", "/lib"), realName).getAbsolutePath();
}
}
final File soFile = new File(soPath);
if (soFile.exists())
return soPath;
else {
//unzip from apk file
final boolean success = extractSo();
if (success)
return new File(getCacheDir(), realName).getAbsolutePath();
}
return "";
}

public static String getLibJssRealPath() {
if(TextUtils.isEmpty(CORE_JSS_SO_PATH)) {
CORE_JSS_SO_PATH = findLibJssRealPath();
WXLogUtils.e("findLibJssRealPath " + CORE_JSS_SO_PATH);
}

return CORE_JSS_SO_PATH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import android.support.annotation.UiThread;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

Expand Down Expand Up @@ -1795,6 +1794,7 @@ private WXParams assembleDefaultOptions() {
wxParams.setShouldInfoCollect(config.get("infoCollect"));
wxParams.setLogLevel(config.get(WXConfig.logLevel));
wxParams.setUseSingleProcess(isUseSingleProcess ? "true" : "false");
wxParams.setLibJssPath(WXEnvironment.getLibJssRealPath());
String appName = config.get(WXConfig.appName);
if (!TextUtils.isEmpty(appName)) {
wxParams.setAppName(appName);
Expand Down
12 changes: 10 additions & 2 deletions android/sdk/src/main/java/com/taobao/weex/bridge/WXParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
package com.taobao.weex.bridge;

import android.util.Log;

import com.taobao.weex.utils.WXLogUtils;

import java.util.Map;
Expand All @@ -39,6 +37,7 @@ public class WXParams {
private String needInitV8;
private String cacheDir;
private String useSingleProcess;
private String libJssPath;

private Map<String, String> options;

Expand Down Expand Up @@ -171,4 +170,13 @@ public void setNeedInitV8(boolean need) {
this.needInitV8 = "0";
}
}

public String getLibJssPath() {
WXLogUtils.e("getLibJssPath is running " + libJssPath);
return libJssPath;
}

public void setLibJssPath(String libJssPath) {
this.libJssPath = libJssPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.text.TextUtils;
import android.util.Base64;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -34,6 +35,9 @@
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class WXFileUtils {

Expand Down Expand Up @@ -178,4 +182,31 @@ public static String base64Md5(byte[] bts){
return "";
}
}

public static void extractSo(String apkFile, String path) throws IOException {
ZipFile zip = new ZipFile(apkFile);
InputStream zipInputStream = new BufferedInputStream(new FileInputStream(apkFile));
ZipInputStream zin = new ZipInputStream(zipInputStream);
ZipEntry zipEntry;
while ((zipEntry = zin.getNextEntry()) != null) {
if(zipEntry.isDirectory()){
continue;
}
if(zipEntry.getName().contains("lib/armeabi/") && zipEntry.getName().contains("weex")){
String[] fileNames = zipEntry.getName().split("/");
String fileName = fileNames[fileNames.length - 1];
InputStream inputStream = zip.getInputStream(zipEntry);
byte[] data = new byte[1024];
FileOutputStream outputStream =new FileOutputStream(path + "/" + fileName);
while (inputStream.read(data) != -1) {
outputStream.write(data);
}
outputStream.close();

}
}
zin.closeEntry();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import com.taobao.weex.BuildConfig;
import com.taobao.weex.IWXStatisticsListener;
Expand Down Expand Up @@ -238,6 +239,8 @@ public static void copyStartUpSo() {
}
inputStream.close();
outputStream.close();
} else {
WXEnvironment.extractSo();
}
}
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "WeexJSConnection.h"

#include "ashmem.h"
#include "WeexProxy.h"
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
Expand All @@ -35,6 +36,8 @@
#include <errno.h>

extern const char *s_cacheDir;
extern const char *g_jssSoPath;
extern const char *g_jssSoName;
extern bool s_start_pie;

static void doExec(int fd, bool traceEnable, bool startupPie = true);
Expand Down Expand Up @@ -167,10 +170,7 @@ void printLogOnFile(const char *log) {
#endif
}

static std::string __attribute__((noinline)) findPath();

static void findPath(std::string &executablePath, std::string &icuDataPath) {
unsigned long target = reinterpret_cast<unsigned long>(__builtin_return_address(0));
static void findIcuDataPath(std::string &icuDataPath) {
FILE *f = fopen("/proc/self/maps", "r");
if (!f) {
return;
Expand All @@ -181,30 +181,9 @@ static void findPath(std::string &executablePath, std::string &icuDataPath) {
if (icuDataPath.empty() && strstr(line, "icudt")) {
icuDataPath.assign(strstr(line, "/"));
icuDataPath = icuDataPath.substr(0, icuDataPath.length() - 1);
continue;
}
char *end;
unsigned long val;
errno = 0;
val = strtoul(line, &end, 16);
if (errno)
continue;
if (val > target)
continue;
end += 1;
errno = 0;
val = strtoul(end, &end, 16);
if (errno)
continue;
if (val > target) {
executablePath.assign(strstr(end, "/"));
std::size_t found = executablePath.rfind('/');
if (found != std::string::npos) {
executablePath = executablePath.substr(0, found);
}
}
if (!executablePath.empty()
&& !icuDataPath.empty()) {

if (!icuDataPath.empty()) {
break;
}
}
Expand Down Expand Up @@ -257,13 +236,24 @@ std::unique_ptr<const char *[]> EnvPBuilder::build() {
void doExec(int fd, bool traceEnable, bool startupPie) {
std::string executablePath;
std::string icuDataPath;
findPath(executablePath, icuDataPath);
findIcuDataPath(icuDataPath);
if(g_jssSoPath != nullptr) {
executablePath = g_jssSoPath;
}
if (executablePath.empty()) {
executablePath = WeexCore::WeexProxy::findLibJssSoPath();
}
#if PRINT_LOG_CACHEFILE
std::ofstream mcfile;
mcfile.open(logFilePath, std::ios::app);
mcfile << "jsengine WeexJSConnection::doExec executablePath:" << executablePath << std::endl;
mcfile << "jsengine WeexJSConnection::doExec icuDataPath:" << icuDataPath << std::endl;
#endif
std::string::size_type pos = std::string::npos;
std::string libName = g_jssSoName;
pos = executablePath.find(libName);
if (pos != std::string::npos) {
executablePath.replace(pos, libName.length(), "");

if (executablePath.empty()) {
LOGE("executablePath is empty");
Expand All @@ -274,7 +264,9 @@ void doExec(int fd, bool traceEnable, bool startupPie) {
#endif

return;
}
} else {
LOGE("executablePath is %s", executablePath.c_str());
}}
if (icuDataPath.empty()) {
LOGE("icuDataPath is empty");
#if PRINT_LOG_CACHEFILE
Expand Down
Loading