下面列出了android.content.Context#getPackageCodePath ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 根据包名构建目标Context,并调用getPackageCodePath()来定位apk
*
* @param context context参数
* @param modulePackageName 当前模块包名
* @return return apk file
*/
private File findApkFile(Context context, String modulePackageName) {
if (context == null) {
return null;
}
try {
Context moduleContext = context.createPackageContext(
modulePackageName,
Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
String apkPath = moduleContext.getPackageCodePath();
return new File(apkPath);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
private static long getApkFileChecksum(Context context) {
String apkPath = context.getPackageCodePath();
Long chksum = null;
try {
// Open the file and build a CRC32 checksum.
FileInputStream fis = new FileInputStream(new File(apkPath));
CRC32 chk = new CRC32();
CheckedInputStream cis = new CheckedInputStream(fis, chk);
byte[] buff = new byte[80];
while (cis.read(buff) >= 0) ;
chksum = chk.getValue();
} catch (Exception e) {
e.printStackTrace();
}
return chksum;
}
/**
* 获取某包下所有类
*
* @param className 过滤的类名
* @return 类的完整名称
*/
public static List<String> getPkgClassNames(Context context, String className) {
List<String> classNameList = new ArrayList<>();
String pPath = context.getPackageCodePath();
File dir = new File(pPath).getParentFile();
String[] paths = dir.list();
if (paths == null) {
classNameList.addAll(getPkgClassName(pPath, className));
} else {
String dPath = dir.getPath();
for (String path : dir.list()) {
String fPath = dPath + "/" + path;
if (!fPath.endsWith(".apk")) {
continue;
}
classNameList.addAll(getPkgClassName(fPath, className));
}
}
return classNameList;
}
/**
* Gets all of the classes within a package
* @param context the context
* @param packageName the package name to fetch classes from
* @return an list of named classes within package
*/
public static ArrayList<String> getClassesOfPackage(Context context, String packageName) {
ArrayList<String> classes = new ArrayList<>();
try {
String packageCodePath = context.getPackageCodePath();
DexFile df = new DexFile(packageCodePath);
for (Enumeration<String> iter = df.entries(); iter.hasMoreElements(); ) {
String className = iter.nextElement();
if (className.contains(packageName)) {
classes.add(className.substring(className.lastIndexOf(".") + 1, className.length()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
return classes;
}
public void makeReady(Context context) {
try {
DexFile dexFile = new DexFile(context.getPackageCodePath());
for (Enumeration<String> item = dexFile.entries(); item.hasMoreElements(); ) {
String element = item.nextElement();
if (element.startsWith(App.class.getPackage().getName())) {
Class<? extends OModel> clsName = (Class<? extends OModel>) Class.forName(element);
if (clsName != null && clsName.getSuperclass() != null &&
OModel.class.isAssignableFrom(clsName.getSuperclass())) {
String modelName = getModelName(context, clsName);
if (modelName != null) {
this.models.put(modelName, clsName);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取dexFile所有类
*
* @param context
* @return
*/
public static List<String> getAllClassNameFromDexFile(Context context, String packageName) {
final List<String> classList = new ArrayList<>();
try {
final DexFile df = new DexFile(context.getPackageCodePath());
String str;
for (final Enumeration<String> iter = df.entries(); iter.hasMoreElements(); ) {
str = iter.nextElement();
if ((packageName != null && str.startsWith(packageName))
|| (packageName == null || "".equals(packageName))) {
classList.add(str);
}
}
df.close();
} catch (IOException e) {
Log.e("IOException " + e.getMessage());
}
return classList;
}
/**
* 根据包名构建目标Context,并调用getPackageCodePath()来定位apk
*
* @param context context参数
* @param modulePackageName 当前模块包名
* @return return apk file
*/
@TargetApi(Build.VERSION_CODES.FROYO)
private File findApkFile(Context context, String modulePackageName) {
if (context == null) {
return null;
}
try {
Context moudleContext = context.createPackageContext(modulePackageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
String apkPath = moudleContext.getPackageCodePath();
return new File(apkPath);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据包名构建目标Context,并调用getPackageCodePath()来定位apk
* @param context context参数
* @param modulePackageName 当前模块包名
* @return return apk file
*/
private File findApkFile(Context context,String modulePackageName){
if (context==null){
return null;
}
try {
Context moudleContext = context.createPackageContext(modulePackageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
String apkPath = moudleContext.getPackageCodePath();
return new File(apkPath);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据包名构建目标Context,并调用getPackageCodePath()来定位apk
*
* @param context context参数
* @param modulePackageName 当前模块包名
* @return return apk file
*/
private File findApkFile(Context context, String modulePackageName) {
if (context == null) {
return null;
}
try {
Context moudleContext = context.createPackageContext(modulePackageName, Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
String apkPath = moudleContext.getPackageCodePath();
return new File(apkPath);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
private static byte[] getApkFileDigest(Context context) {
String apkPath = context.getPackageCodePath();
try {
return getDigest(new FileInputStream(apkPath), "SHA-256");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
static synchronized Context loadNativeLib(Context context) {
try {
if (nativeLibLoaded) return context;
ApplicationInfo otherAppInfo = context.getPackageManager().getApplicationInfo(context.getApplicationContext().getPackageName(), 0);
String primaryCpuAbi = (String) ApplicationInfo.class.getField("primaryCpuAbi").get(otherAppInfo);
if (primaryCpuAbi != null) {
String path = "lib/" + primaryCpuAbi + "/libvtm-jni.so";
File cacheFile = new File(context.getApplicationContext().getCacheDir().getAbsolutePath() + "/.gmscore/" + path);
cacheFile.getParentFile().mkdirs();
File apkFile = new File(context.getPackageCodePath());
if (!cacheFile.exists() || cacheFile.lastModified() < apkFile.lastModified()) {
ZipFile zipFile = new ZipFile(apkFile);
ZipEntry entry = zipFile.getEntry(path);
if (entry != null) {
copyInputStream(zipFile.getInputStream(entry), new FileOutputStream(cacheFile));
} else {
Log.d(TAG, "Can't load native library: " + path + " does not exist in " + apkFile);
}
}
Log.d(TAG, "Loading vtm-jni from " + cacheFile.getPath());
System.load(cacheFile.getAbsolutePath());
nativeLibLoaded = true;
}
} catch (Exception e) {
Log.w(TAG, e);
}
if (!nativeLibLoaded) {
Log.d(TAG, "Loading native vtm-jni");
System.loadLibrary("vtm-jni");
nativeLibLoaded = true;
}
return context;
}