如何减少需要openCv lib(仅用于图像处理)的Android应用的apk大小 [英] How to reduce the apk size of Android app which need openCv lib( only for Image processing)

查看:234
本文介绍了如何减少需要openCv lib(仅用于图像处理)的Android应用的apk大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最小的SDK 15构建一个Android应用,该SDK使用OpenCV库.问题是当我构建的apk大小大于60 MB时,这不是很好.

I'm building an Android app with min SDK 15, which uses OpenCV library.The problem is when I build apk size of it is more than 60 MB which is not very great.

我检查了应用程序文件,由于amr64armeabimipsx86等所有体系结构中的libopencv_java3.so文件,我发现大小太大.

I checked the app file, I could see that the size is too big due to libopencv_java3.so file in all architectures like amr64, armeabi, mips, x86 etc.

我仅使用opneCv进行图像处理.该库还具有许多其他功能,例如视频处理,3d图像,对象检测等,而我的应用程序中不需要

I am using opneCv only for image processing.This library has a lot of other features like video processing, 3d image, object detection and much more, which I don't need in my app like

如果我删除所有这些文件并构建APK,则大小将减少50 MB,但是要使该应用程序正常运行,我需要安装OpenCVManager才能运行我的应用程序.

If I remove all those files and build APK, then size will be reduced 50 MB, but to make this app work I need to install OpenCVManager to run my app.

有什么方法可以减小我的apk大小?

Is there any way by which I can reduce my apk size?

或者仅从我感兴趣的OpenCV中提取功能并为这些功能创建.so是有可能的吗?

Or is it possible just to extract only the feature from OpenCV which I am interested in and create .so for those features?

推荐答案

对我来说, alijandro答案第二个答案是最佳解决方案,但我陷入了这一过程.所以,我通过apk split解决了这个问题.您需要上传与该平台相关的apk以及一个通用apk.这很理想,因为OpenCV在模块级别不提供添加的依赖关系.

For me, alijandro answer 2nd answer was the best solution but I got stuck in the process. So, I solved the issue by apk split. You need to upload apk's related to the platform and also a universal apk.It would be ideal , since OpenCV doesn't provide adding dependency at module level.

万一apk将在平台不匹配的情况下下载. 此处 是写得很好的博客,内容涉及apk发布多个apk.您需要维护相同的应用程序ID,密钥库,相同的应用程序版本但不同的代码版本

Universal apk will be downloaded in case platform does not match.Here is nicely written blog about apk publishing mutiple apk. You need to maintain same app id , Keystore, same app version but different code version

第1步.添加用于spk拆分的gradle代码.包括要为其创建单独的APK的体系结构.

Step 1.Add gradle code for spk split. Include the architecture for which you want to create separate apk.

android{
//......
splits {
    abi {
        enable true
        reset()
        include "armeabi","armeabi-v7a",'arm64-v8a',"mips","x86","x86_64",'arm64-v8a'
        universalApk true
    }
}
}

第2步.为不同平台的不同代码版本添加以下代码.

Step 2.Add below code for different Code version for different platform.

android{
//......
}
ext.versionCodes = ['armeabi': 3, 'armeabi-v7a': 4, 'arm64-v8a': 5, 
mips: 6, 'x86': 7, 'x86_64': 8]
import com.android.build.OutputFile
// For each APK output variant, override versionCode with a combination 
// of ABI APK value * 1000 + defaultConfig.versionCode
android.applicationVariants.all { variant ->
// assign different version code for each output
variant.outputs.each { output ->
    def abiFilter = output.getFilter(OutputFile.ABI)
    def abiMultiplier = 0
    if (abiFilter != null) {
        abiMultiplier = project.ext.versionCodes.get(abiFilter)
    }
    output.versionCodeOverride =
            abiMultiplier * 1000 + android.defaultConfig.versionCode
   }
  }

例如,如果初始版本代码为1,则生成的代码将为:

For Example, if the initial version code was 1, then generated codes will be:

 armeabi -> 3001
 armeabi-v7a -> 4001
 arm64-v8a -> 5001
 mips -> 6001
 x86 -> 7001
 x86_64 -> 8001

所有的Gradle代码都将像这样.

All together your Gradle code will look like this.

 android{
//......
splits {
abi {
    enable true
    reset()
    include "armeabi","armeabi-v7a",'arm64-
v8a',"mips","x86","x86_64",'arm64-v8a'
    universalApk true
 }
 }

 ext.versionCodes = ['armeabi': 3, 'armeabi-v7a': 4, 'arm64-v8a': 5, 
mips: 6, 'x86': 7, 'x86_64': 8]
import com.android.build.OutputFile
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def abiFilter = output.getFilter(OutputFile.ABI)
def abiMultiplier = 0
if (abiFilter != null) {
    abiMultiplier = project.ext.versionCodes.get(abiFilter)
}
output.versionCodeOverride =
        abiMultiplier * 1000 + android.defaultConfig.versionCode
}
}

现在,您需要将所有apk一次发布在playstore上.

Now you need to publish all the apk on playstore one by one.

注意:发布时请确保保留所有平台和通用apk.

Note: While publishing make sure you retain all the platform and universal apk.

这篇关于如何减少需要openCv lib(仅用于图像处理)的Android应用的apk大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆