一个非常好的从jar文件中加载so动态库方法,在android的gif支持开源中用到。这个项目的gif解码是用jni c实现的,避免了OOM等问题。

项目地址:https://github.com/koral--/android-gif-drawable

如果是把java文件生成jar。jni生成的so文件放到使用apk的libs/armeabi/lib_gif.so.....

gifExample.apk:

/libs/gif.jar

/libs/armeabi/lib_gi.so

这样做会报错,提示xml里面找不到GifImageView。

只能用项目之间依赖,so会自动进入生成的apk,不用拷贝。

调用方法:

//开始调用:
static {
LibraryLoader.loadLibrary(null, LibraryLoader.BASE_LIBRARY_NAME);
}

进入这里:

package pl.droidsonroids.gif;

import android.content.Context;
import android.support.annotation.NonNull; import java.lang.reflect.Method; /**
* Helper used to work around native libraries loading on some systems.
* See <a href="https://medium.com/keepsafe-engineering/the-perils-of-loading-native-libraries-on-android-befa49dce2db">ReLinker</a> for more details.
*/
public class LibraryLoader {
static final String SURFACE_LIBRARY_NAME = "pl_droidsonroids_gif_surface";
static final String BASE_LIBRARY_NAME = "pl_droidsonroids_gif";
private static Context sAppContext; /**
* Intitializes loader with given `Context`. Subsequent calls should have no effect since application Context is retrieved.
* Libraries will not be loaded immediately but only when needed.
* @param context any Context except null
*/
public static void initialize(@NonNull final Context context) {
sAppContext = context.getApplicationContext();
} static Context getContext() {
if (sAppContext == null) {
try {
final Class<?> activityThread = Class.forName("android.app.ActivityThread");
final Method currentApplicationMethod = activityThread.getDeclaredMethod("currentApplication");
sAppContext = (Context) currentApplicationMethod.invoke(null);
} catch (Exception e) {
throw new RuntimeException("LibraryLoader not initialized. Call LibraryLoader.initialize() before using library classes.", e);
}
}
return sAppContext;
} static void loadLibrary(Context context, final String library) {
try {
System.loadLibrary(library);
} catch (final UnsatisfiedLinkError e) {
if (SURFACE_LIBRARY_NAME.equals(library)) {
loadLibrary(context, BASE_LIBRARY_NAME);
}
if (context == null) {
context = getContext();
}
ReLinker.loadLibrary(context, library);
}
}
}

最终到这里:

 
 /**
* Copyright 2015 KeepSafe Software, Inc.
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pl.droidsonroids.gif; import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Build; import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; /**
* Based on https://github.com/KeepSafe/ReLinker
* ReLinker is a small library to help alleviate {@link UnsatisfiedLinkError} exceptions thrown due
* to Android's inability to properly install / load native libraries for Android versions before
* API 21
*/
class ReLinker {
private static final String LIB_DIR = "lib";
private static final int MAX_TRIES = 5;
private static final int COPY_BUFFER_SIZE = 8192; private ReLinker() {
// No instances
} /**
* Utilizes the regular system call to attempt to load a native library. If a failure occurs,
* then the function extracts native .so library out of the app's APK and attempts to load it.
* <p/>
* <strong>Note: This is a synchronous operation</strong>
*/
static void loadLibrary(Context context, final String library) {
final String libName = System.mapLibraryName(library);
synchronized (ReLinker.class) {
final File workaroundFile = unpackLibrary(context, libName);
System.load(workaroundFile.getAbsolutePath());
}
} /**
* Attempts to unpack the given library to the workaround directory. Implements retry logic for
* IO operations to ensure they succeed.
*
* @param context {@link Context} to describe the location of the installed APK file
* @param libName The name of the library to load
*/
private static File unpackLibrary(final Context context, final String libName) {
File outputFile = new File(context.getDir(LIB_DIR, Context.MODE_PRIVATE), libName);// + BuildConfig.VERSION_NAME);
if (outputFile.isFile()) {
return outputFile;
} final File cachedLibraryFile = new File(context.getCacheDir(), libName );//+ BuildConfig.VERSION_NAME);
if (cachedLibraryFile.isFile()) {
return cachedLibraryFile;
} final FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
return filename.startsWith(libName);
}
};
clearOldLibraryFiles(outputFile, filter);
clearOldLibraryFiles(cachedLibraryFile, filter); final ApplicationInfo appInfo = context.getApplicationInfo();
final File apkFile = new File(appInfo.sourceDir);
ZipFile zipFile = null;
try {
zipFile = openZipFile(apkFile); int tries = 0;
while (tries++ < MAX_TRIES) {
ZipEntry libraryEntry = getLibraryEntry(libName, zipFile); InputStream inputStream = null;
FileOutputStream fileOut = null;
try {
inputStream = zipFile.getInputStream(libraryEntry);
fileOut = new FileOutputStream(outputFile);
copy(inputStream, fileOut);
} catch (IOException e) {
if (tries > MAX_TRIES / 2) {
outputFile = cachedLibraryFile;
}
continue;
} finally {
closeSilently(inputStream);
closeSilently(fileOut);
}
setFilePermissions(outputFile);
break;
}
} finally {
closeSilently(zipFile);
}
return outputFile;
} @SuppressWarnings("deprecation") //required for old API levels
private static ZipEntry getLibraryEntry(final String libName, final ZipFile zipFile) {
String jniNameInApk; ZipEntry libraryEntry = null;
// if (Build.VERSION.SDK_INT >= 21 && Build.SUPPORTED_ABIS.length > 0) {
// for (final String ABI : Build.SUPPORTED_ABIS) {
// jniNameInApk = "lib/" + ABI + "/" + libName;
// libraryEntry = zipFile.getEntry(jniNameInApk);
//
// if (libraryEntry != null) {
// break;
// }
// }
// } else {
jniNameInApk = "lib/" + Build.CPU_ABI + "/" + libName;
libraryEntry = zipFile.getEntry(jniNameInApk);
} if (libraryEntry == null) {
throw new IllegalStateException("Library " + libName + " for supported ABIs not found in APK file");
}
return libraryEntry;
} private static ZipFile openZipFile(final File apkFile) {
int tries = 0;
ZipFile zipFile = null;
while (tries++ < MAX_TRIES) {
try {
zipFile = new ZipFile(apkFile, ZipFile.OPEN_READ);
break;
} catch (IOException ignored) {
}
} if (zipFile == null) {
throw new RuntimeException("Could not open APK file: " + apkFile.getAbsolutePath());
}
return zipFile;
} @SuppressWarnings("ResultOfMethodCallIgnored") //intended, nothing useful can be done
private static void clearOldLibraryFiles(final File outputFile, final FilenameFilter filter) {
final File[] fileList = outputFile.getParentFile().listFiles(filter);
if (fileList != null) {
for (File file : fileList) {
file.delete();
}
}
} @SuppressWarnings("ResultOfMethodCallIgnored") //intended, nothing useful can be done
@SuppressLint("SetWorldReadable") //intended, default permission
private static void setFilePermissions(File outputFile) {
// Try change permission to rwxr-xr-x
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
outputFile.setReadable(true, false);
outputFile.setExecutable(true, false);
outputFile.setWritable(true);
}
} /**
* Copies all data from an {@link InputStream} to an {@link OutputStream}.
*
* @param in The stream to read from.
* @param out The stream to write to.
* @throws IOException when a stream operation fails.
*/
private static void copy(InputStream in, OutputStream out) throws IOException {
final byte[] buf = new byte[COPY_BUFFER_SIZE];
while (true) {
final int bytesRead = in.read(buf);
if (bytesRead == -1) {
break;
}
out.write(buf, 0, bytesRead);
}
} /**
* Closes a {@link Closeable} silently (without throwing or handling any exceptions)
*
* @param closeable {@link Closeable} to close
*/
private static void closeSilently(final Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException ignored) {
}
}
}

获取当前apk路径

final ApplicationInfo appInfo = context.getApplicationInfo();
Log.d("zhibin","appInfo.sourceDir: "+ appInfo.sourceDir);
输出:/system/app/xxx.apk


对应sdk 5.0以下版本,修正一些不支持的变量:

    @SuppressWarnings("deprecation") //required for old API levels
private static ZipEntry getLibraryEntry(final String libName, final ZipFile zipFile) {
String jniNameInApk; ZipEntry libraryEntry = null;
// if (Build.VERSION.SDK_INT >= 21 && Build.SUPPORTED_ABIS.length > 0) {
// for (final String ABI : Build.SUPPORTED_ABIS) {
// jniNameInApk = "lib/" + ABI + "/" + libName;
// libraryEntry = zipFile.getEntry(jniNameInApk);
//
// if (libraryEntry != null) {
// break;
// }
// }
// } else {
jniNameInApk = "lib/" + Build.CPU_ABI + "/" + libName;
Log.d("zhibin","Search directory for jniNameInApk: "+ jniNameInApk);
libraryEntry = zipFile.getEntry(jniNameInApk); //直接指定
if(libraryEntry == null){
jniNameInApk = "lib/armeabi" + "/" + libName;
Log.d("zhibin","Correct it to jniNameInApk: "+ jniNameInApk);
libraryEntry = zipFile.getEntry(jniNameInApk);
}
} if (libraryEntry == null) {
throw new IllegalStateException("Library " + libName + " for supported ABIs not found in APK file");
}
return libraryEntry;
}

共外部调用资源:

背景:工作中需要开发一个广告插件,并提供给其它人使用。这里就需要把自己的插件程序,打成jar来提供给他人引用。
但是遇到一个问题:插件程序中无法使用资源文件。

试过以下几种方式解决:

1、从插件程序中导出jar包
论坛上有人说导出的jar包中无法包含Drawable等资源文件,一些图片等数据,需要放到Assert文件中使用。
其实,关于这个问题,我做了尝试:
首先,需要说明导出jar包含什么文件是由你导出时选择来决定的,比如下图:

如果你选择了res文件夹,则打包出的jar文件是可以包含res文件到。

但是包含文件并不代表可以使用。如果你想当然在插件程序中使用R.drawable.XXXX等方式获取
资源会报错!
当然别人通过R.XX.XX也只能看到自己的资源文件,而无法获取jar中的资源文件。

2、获取jar包中的文件

虽然无法直接引用资源文件,但是如果外边程序想获取某个资源文件时,也是可行的。
其原理是以数据流读取jar中指定的文件。
比如读取Assert文件下的icon.jpg文件:
你可以在插件中封装一个对外的方法:
    publicstatic Drawable getAssertDrawable(Context context,StringfileName){
       try {
          InputStreaminStream=context.getAssets().open(fileName);
          return newBitmapDrawable(BitmapFactory.decodeStream(inStream));
       } catch(IOException e) {
         Log.e(LOG_TAG, "Assert中"+fileName+"不存在");
       }
       returnnull;
    }
直接使用该方法可以得到文件。
后来又尝试在外部程序,直接使用context.getAssets().open(fileName)方法获取jar中文件,
让人喜出望外的是竟然成功了。呵呵!
后来分析,外部程序编译时,其实连同jar包中内容一起混编。jar包中的Assert文件会同外部程序的Assert一起
由AssertManager管理。
所以当你jar包中Assert内部文件和外部Assert中的文件有命名冲突时,编译器会报错的。

另外,还有人提供另外一种方法来读取诸如Drawable等文件夹下的文件。
    publicstatic Drawable getDrawableForJar(String resName,Classclass){
       InputStreaminStream=class.getResourceAsStream(resName);
       return newBitmapDrawable(BitmapFactory.decodeStream(inStream));
    }
使用class.getResourceAsStream()方法读取,注意这里resName是文件的相对路径,比如jar根目录下res/drawable/icon.png,
则调用方法为:class.getResourceAsStream(/res/drawable/icon.png);

这里主要是采用ClassLoader的下面几个方法来实现:

  public URL getResource(String name);

  public InputStream getResourceAsStream(String name)

  public static InputStreamgetSystemResourceAsStream(String name)

  public static URL getSystemResource(String name)

  后两个方法可以看出是静态的方法,这几个方法都可以从Jar中读取图片资源,但是对与动画的gif文件,笔者在尝试过程中发现,存在一些差异。

  String gifName为Gif文件在Jar中的相对路径。

  (1)使用了两个静态方法

  BufferedImageimage = ImageIO.read(ClassLoader.getSystemResourceAsStream(gifName));

  或者

  Image image = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource(gifName));

  这两种方式可以成功地读取gif文件,但是对于gif动画,显示出来地是静态的。

  (2)使用其他两个方法

  Image image = Toolkit.getDefaultToolkit().getImage( this .getClass.getClassLoader()
.getResource(gifName));

  再这种方式下动画可以正常显示了。

3、使用library方法加载资源文件

在论坛中看到帖子讲述如何把工程作为libarary,让其他工程添加library,编译后会自动生成jar,然后在哪来使用。 
当时看到此贴,喜出望外,所以赶紧尝试下!

方法:选择插件工程,右键选择属性,选择Android,勾选下面Is Liabrary选项。 
然后,选择我们现有的工程,右键属性,选择Android,在library下add相应的库。你会看到,刚才我们设置的插件项目,就在其中。最后,点击应用,完成。

详细步骤:

按如下方法设置:

1. 假设要引用的android工程叫LibProject,引入到的工程叫MainProject;

2.设置LibProject,右键->Properties->Android,将Islibrary项选中,然后Apply;

3.设置MainProject,右键->->Properties->Android,在Library中,点击Add按钮,将LibProject工程加入,Apply即可。

你会看到我们的工程中多出插件工程的引用,而且可以使用R.XXX.XXX获取资源文件。

以为可以解决了,但是发现并没有生成想要的jar文件。在插件工程中,倒是有编译的class文件,却没有jar包。 
而我们往往是不能像这样把原工程给别人直接引用的。 
经过多次试验,始终没有生成jar,非常奇怪别人怎么弄得。。。

另外,拿以前通过这种方式生成的jar文件看,里面也不包含资源文件夹。。 
可以把生成的类共享出去。

把.so文件打包到jar中

查了一些方法,其中一个我比较喜欢,再load动态库的时候,把so文件复制到tmp目录下,然后删掉

//modify the static block

static {
try {
Class c = HelloJNI.class;
URL location =
c.getProtectionDomain().getCodeSource().getLocation();
ZipFile zf = new ZipFile(location.getPath());
// libhellojni.so is put in the lib folder
InputStream in = zf.getinputStream(zf.getEntry("lib/libhellojni.so"));
File f = File.createTempFile("JNI-", "Temp");
FileOutputStream out = new FileOutputStream(f);
byte [] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
in.close();
out.close();
System.load(f.getAbsolutePath());
f.delete();
} catch (Exception e) { // I am still lazy ~~~
e.printStackTrace();
}
}

打包jar文件 外部调用资源 so等的更多相关文章

  1. 关于在打包Jar文件时遇到的资源路径问题(二)

    在关于<关于在打包Jar文件时遇到的资源路径问题(一)>中,以及描述了当资源与可执行JAr分离时的资源路径代码的编写问题,后来想了想,为什么将<Java核心技术卷一>中的程序1 ...

  2. 关于在打包Jar文件时遇到的资源路径问题(一)

    当我们将程序写好,并进行打包成Jar文件时,通常都带有各种资源,这些资源可以是图像或者声音文件,也可以是别的如文本文件或二进制文件等,这些资源都和代码密切相关.例如在一个JPanel类上显示一些可能变 ...

  3. Eclipse将android项目打包jar文件

    Eclipse+android打包jar文件 蔡建良 2016-3-12 以Android-SlideExpandableListView开源框架为例,将源码Library打包成jar文件并包含R.c ...

  4. AndroidStduio3.0 使用gradle将module打包jar文件

    AndroidStduio3.0使用gradle将module打包jar文件,首先需要安装gradle. 打开控制台输入      open -e .bash_profile     命令,就可以打开 ...

  5. Intellij打包jar文件,“java.lang.SecurityException: Invalid signature file digest for Manifest main attrib

    下面是使用Intellij 打包jar文件的步骤,之后会有运行jar文件时遇到的错误. 打包完成. ================================================== ...

  6. eclipse打包jar文件(含外部jar包)的方法

    在项目发布前,使用eclipse导出普通的jar包时,如果配置不好,在运行命令Java -jar /test.jar 时可能会出现如下三类错误信息: 1.no main manifest attrib ...

  7. eclipse打包jar文件

    论文仿真做线性回归分类在人脸识别中应用与研究,在单机下实现LRC算法后,又在Hadoop云平台下实现了该算法.在比较实验结果时候需要放在相同硬件条件下比较.但是LRC单机算法是在windows下的ec ...

  8. java 打包jar文件以在没有安装JDK或JRE的机子上运行

    前言: java号称“一次编译,到处运行”,但这有个前提,那就是你的机子上得安装java环境.对于开发人员或其他一些比较懂计算机的人来说这没什么,但是对于一些不懂计算机的人来说这会很麻烦,他们更希望的 ...

  9. Java 图片爬虫,java打包jar文件

    目录 1. Java 图片爬虫,制作 .jar 文件 spider.java 制作 jar 文件 添加执行权限 1. Java 图片爬虫,制作 .jar 文件 spider.java spider.j ...

随机推荐

  1. ASP.NET MVC3入门教程之第一个WEB应用程序

    本文转载自:http://www.youarebug.com/forum.php?mod=viewthread&tid=91&extra=page%3D1 上一节,我们已经搭建好了AS ...

  2. [BZOJ2654]tree(二分+MST)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2654 分析:此题很奇葩,我们可以给所有白边加上一个权值mid,那么在求得的MST中白边 ...

  3. 迷你DVD管理器

    import java.text.*; import java.util.*; class DVDSet { String[] name=new String[50]; //定义一个DVD数组 boo ...

  4. 十天冲刺---Day5

    站立式会议 站立式会议内容总结: 燃尽图 照片 PM确实不应该交给组内编码最强的人来做. 编码的过程还要考虑整个项目的流程压力较大. 需要队友的支持和沟通.

  5. Java创始人

    詹姆斯·高斯林(英语:James Gosling,1955年5月19日-),出生于加拿大,软件专家,Java编程语言的共同创始人之一,一般公认他为“Java之父”. 在他12岁的时候,他已能设计电子游 ...

  6. 那些用JavaScript写的操作系统

    之前有人说过Chrome是新的C语言运行环境(Chrome Is The New C Runtime) ,不过笔者更倾向于Web是新的C语言运行环境,而且这种技术绝对没有版权问题,也绝不会被一家公司垄 ...

  7. 让apache后端显示真实客户端IP

    公司是nginx做代理,后端的web服务用的是apache,然后我现在要分析日志,但是,我的apache日志上显示的是代理服务器的ip地址,不是客户的真实IP 所以这里我需要修改一下,让apache的 ...

  8. [转]Hibernate查询对象所有字段,单个字段 ,几个字段取值的问题

    原文地址:http://www.ablanxue.com/prone_3552_1.html 1. 查询整个映射对象所有字段 Java代码 //直接from查询出来的是一个映射对象,即:查询整个映射对 ...

  9. sql-truncate,delete,drop删除表数据的区别

    TRUNCATE TABLE 删除表中的所有行,而不记录单个行删除操作. 语法 TRUNCATE TABLE name 参数 name 是要截断的表的名称或要删除其全部行的表的名称. 注释 TRUNC ...

  10. HTTP协议学习---(七)代理

    本文介绍一些HTTP代理服务器的概念和工作原理 阅读目录 什么是代理服务器 Fiddler就是个典型的代理 代理作用一:FQ 代理作用二:匿名访问 代理作用三:通过代理上网 代理作用四:通过代理缓存, ...