文章目录
一、修改 7zr 交叉编译脚本 Android.mk
二、完整的 7zr 交叉编译脚本 Android.mk
三、交叉编译 lib7zr.a 静态库
四、Android Studio 导入 lib7zr.a 静态库
五、参考资料
前置博客 :
【Android 安装包优化】使用 lib7zr.so 动态库处理压缩文件 ( 修改 7zr 交叉编译脚本 Android.mk | 交叉编译 lib7zr.so 动态库 )
【Android 安装包优化】使用 lib7zr.so 动态库处理压缩文件 ( 拷贝 lib7zr.so 动态库到 Android Studio 工程 | 配置 build.gradle 构建脚本 )
【Android 安装包优化】使用 lib7zr.so 动态库处理压缩文件 ( 拷贝 lib7zr.so 动态库头文件到 Android 工程中 | 配置 CMakeLists.txt 构建脚本 )
【Android 安装包优化】使用 lib7zr.so 动态库处理压缩文件 ( 测试 lib7zr.so 动态库调用 )
【Android 安装包优化】使用 lib7zr.so 动态库处理压缩文件 ( jni 中 main 函数声明 | 命令行处理 | jni 调用 lib7zr.so 函数库处理压缩文件完整代码 )
在之前的一系列博客中使用 lib7zr.so 动态库处理压缩文件 , 本篇博客中使用静态库处理压缩文件 , 仅做参考 ;
一、修改 7zr 交叉编译脚本 Android.mk
之前的博客 【Android 安装包优化】p7zip 源码交叉编译 Android 平台可执行程序 ( 下载 p7zip 源码 | 交叉编译 Android 中使用 7z 可执行程序 ) 中 , 下载并编译了 p7zip 开源项目源码 , 交叉编译出了可以在 Android 平台执行的可执行文件 ;
D:\develop\7zip\p7zip_16.02\CPP\ANDROID\7zr 目录下是 7zr 项目的构建脚本 Android.mk 和 Application.mk , 主要使用该构建脚本交叉编译出
可执行文件 7zr
静态库 lib7zr.a ;
进入 7zr 目录下 , 有一个 jni 目录 和 makefile 文件 ,
jni 目录中是 Android.mk 和 Application.mk 两个构建脚本 , 可以使用 ndk-build 进行编译 ;
打开该 makefile 文件 , 该文件最后几行代码给出了相关操作方法 ;
# FIXME debug: cd jni ; ndk-build NDK_DEBUG=1 adb push ./libs/armeabi/7zr /data/app/ adb push ./libs/armeabi/gdbserver /data/app/ adb shell chmod 777 /data/app/7zr adb shell chmod 777 /data/app/gdbserver adb forward tcp:1234: tcp:1234 adb shell /data/app/gdbserver :1234 /data/app/7zr
进入 D:\develop\7zip\p7zip_16.02\CPP\ANDROID\7zr\jni 目录 , 执行 ndk-build 命令 , 进行交叉编译 ;
修改 Application.mk 构建脚本 :
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64 APP_PLATFORM := android-8
注释可执行文件的编译选项 : -fPIE 指的是生成与地址无关的可执行文件 , 该选项只能应用于可执行文件 , 不能用于静态库编译 ;
# Needed since ANDROID 5, these programs run on android-16 (Android 4.1+) # -fPIE 指的是生成与地址无关的可执行文件 , 该选项只能应用于可执行文件 , # 不能用于动态库编译 # 如果编译动态库 , 需要注释掉下面两个配置 # 如果编译静态库 , 需要注释掉下面两个配置 # 如果编译可执行文件 , 可以保留下面两个配置 #LOCAL_CFLAGS += -fPIE #LOCAL_LDFLAGS += -fPIE -pie
设置 Android.mk 构建脚本的编译结果 :
include $(BUILD_STATIC_LIBRARY)
include $(BUILD_EXECUTABLE) 配置表示生成可执行文件 ;
include $(BUILD_SHARED_LIBRARY) 配置表示生成 .so 动态库 ;
include $(BUILD_STATIC_LIBRARY) 配置表示生成 .a 静态库 ;
二、完整的 7zr 交叉编译脚本 Android.mk
修改完成的构建脚本 :
# # build 7zr for armeabi and armeabi-v7a CPU # # WARNING : file generated by generate.py # LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # 编译模块 # 如果编译可执行文件, 编译结果是 7zr # 如果编译动态库, 编译结果是 lib7zr.so # 如果编译静态库, 编译结果是 lib7zr.a LOCAL_MODULE := 7zr # 设置编译器参数 # -I 指定头文件引入目录 , 会引入后面目录中所有的 .h 头文件 LOCAL_CFLAGS := -DANDROID_NDK -fexceptions \ -DNDEBUG -D_REENTRANT -DENV_UNIX \ -DBREAK_HANDLER -D_NO_CRYPTO \ -DUNICODE -D_UNICODE -DUNIX_USE_WIN_FILE \ -I../../../7zip/Archive \ -I../../../7zip/Archive/7z \ -I../../../7zip/Archive/BZip2 \ -I../../../7zip/Archive/Common \ -I../../../7zip/Archive/GZip \ -I../../../7zip/Archive/Cab \ -I../../../7zip/Archive/Lzma \ -I../../../7zip/Archive/Tar \ -I../../../7zip/Archive/Zip \ -I../../../7zip/Archive/Split \ -I../../../7zip/Archive/Z \ -I../../../7zip/Compress \ -I../../../7zip/Crypto \ -I../../../7zip/UI/Console \ -I../../../7zip/UI/Common \ -I../../../Windows \ -I../../../Common \ -I../../../7zip/Common \ -I../../../../C \ -I../../../myWindows \ -I../../../ \ -I../../../include_windows # 源文件引入 LOCAL_SRC_FILES := \ ../../../../CPP/7zip/Archive/7z/7zCompressionMode.cpp \ ../../../../CPP/7zip/Archive/7z/7zDecode.cpp \ ../../../../CPP/7zip/Archive/7z/7zEncode.cpp \ ../../../../CPP/7zip/Archive/7z/7zExtract.cpp \ ../../../../CPP/7zip/Archive/7z/7zFolderInStream.cpp \ ../../../../CPP/7zip/Archive/7z/7zHandler.cpp \ ../../../../CPP/7zip/Archive/7z/7zHandlerOut.cpp \ ../../../../CPP/7zip/Archive/7z/7zHeader.cpp \ ../../../../CPP/7zip/Archive/7z/7zIn.cpp \ ../../../../CPP/7zip/Archive/7z/7zOut.cpp \ ../../../../CPP/7zip/Archive/7z/7zProperties.cpp \ ../../../../CPP/7zip/Archive/7z/7zRegister.cpp \ ../../../../CPP/7zip/Archive/7z/7zSpecStream.cpp \ ../../../../CPP/7zip/Archive/7z/7zUpdate.cpp \ ../../../../CPP/7zip/Archive/Common/CoderMixer2.cpp \ ../../../../CPP/7zip/Archive/Common/DummyOutStream.cpp \ ../../../../CPP/7zip/Archive/Common/HandlerOut.cpp \ ../../../../CPP/7zip/Archive/Common/InStreamWithCRC.cpp \ ../../../../CPP/7zip/Archive/Common/ItemNameUtils.cpp \ ../../../../CPP/7zip/Archive/Common/MultiStream.cpp \ ../../../../CPP/7zip/Archive/Common/OutStreamWithCRC.cpp \ ../../../../CPP/7zip/Archive/Common/ParseProperties.cpp \ ../../../../CPP/7zip/Archive/LzmaHandler.cpp \ ../../../../CPP/7zip/Archive/SplitHandler.cpp \ ../../../../CPP/7zip/Archive/XzHandler.cpp \ ../../../../CPP/7zip/Common/CWrappers.cpp \ ../../../../CPP/7zip/Common/CreateCoder.cpp \ ../../../../CPP/7zip/Common/FilePathAutoRename.cpp \ ../../../../CPP/7zip/Common/FileStreams.cpp \ ../../../../CPP/7zip/Common/FilterCoder.cpp \ ../../../../CPP/7zip/Common/InBuffer.cpp \ ../../../../CPP/7zip/Common/InOutTempBuffer.cpp \ ../../../../CPP/7zip/Common/LimitedStreams.cpp \ ../../../../CPP/7zip/Common/MethodId.cpp \ ../../../../CPP/7zip/Common/MethodProps.cpp \ ../../../../CPP/7zip/Common/OffsetStream.cpp \ ../../../../CPP/7zip/Common/OutBuffer.cpp \ ../../../../CPP/7zip/Common/ProgressUtils.cpp \ ../../../../CPP/7zip/Common/PropId.cpp \ ../../../../CPP/7zip/Common/StreamBinder.cpp \ ../../../../CPP/7zip/Common/StreamObjects.cpp \ ../../../../CPP/7zip/Common/StreamUtils.cpp \ ../../../../CPP/7zip/Common/UniqBlocks.cpp \ ../../../../CPP/7zip/Common/VirtThread.cpp \ ../../../../CPP/7zip/Compress/Bcj2Coder.cpp \ ../../../../CPP/7zip/Compress/Bcj2Register.cpp \ ../../../../CPP/7zip/Compress/BcjCoder.cpp \ ../../../../CPP/7zip/Compress/BcjRegister.cpp \ ../../../../CPP/7zip/Compress/BranchMisc.cpp \ ../../../../CPP/7zip/Compress/BranchRegister.cpp \ ../../../../CPP/7zip/Compress/ByteSwap.cpp \ ../../../../CPP/7zip/Compress/CopyCoder.cpp \ ../../../../CPP/7zip/Compress/CopyRegister.cpp \ ../../../../CPP/7zip/Compress/DeltaFilter.cpp \ ../../../../CPP/7zip/Compress/Lzma2Decoder.cpp \ ../../../../CPP/7zip/Compress/Lzma2Encoder.cpp \ ../../../../CPP/7zip/Compress/Lzma2Register.cpp \ ../../../../CPP/7zip/Compress/LzmaDecoder.cpp \ ../../../../CPP/7zip/Compress/LzmaEncoder.cpp \ ../../../../CPP/7zip/Compress/LzmaRegister.cpp \ ../../../../CPP/7zip/UI/Common/ArchiveCommandLine.cpp \ ../../../../CPP/7zip/UI/Common/ArchiveExtractCallback.cpp \ ../../../../CPP/7zip/UI/Common/ArchiveOpenCallback.cpp \ ../../../../CPP/7zip/UI/Common/Bench.cpp \ ../../../../CPP/7zip/UI/Common/DefaultName.cpp \ ../../../../CPP/7zip/UI/Common/EnumDirItems.cpp \ ../../../../CPP/7zip/UI/Common/Extract.cpp \ ../../../../CPP/7zip/UI/Common/ExtractingFilePath.cpp \ ../../../../CPP/7zip/UI/Common/HashCalc.cpp \ ../../../../CPP/7zip/UI/Common/LoadCodecs.cpp \ ../../../../CPP/7zip/UI/Common/OpenArchive.cpp \ ../../../../CPP/7zip/UI/Common/PropIDUtils.cpp \ ../../../../CPP/7zip/UI/Common/SetProperties.cpp \ ../../../../CPP/7zip/UI/Common/SortUtils.cpp \ ../../../../CPP/7zip/UI/Common/TempFiles.cpp \ ../../../../CPP/7zip/UI/Common/Update.cpp \ ../../../../CPP/7zip/UI/Common/UpdateAction.cpp \ ../../../../CPP/7zip/UI/Common/UpdateCallback.cpp \ ../../../../CPP/7zip/UI/Common/UpdatePair.cpp \ ../../../../CPP/7zip/UI/Common/UpdateProduce.cpp \ ../../../../CPP/7zip/UI/Console/BenchCon.cpp \ ../../../../CPP/7zip/UI/Console/ConsoleClose.cpp \ ../../../../CPP/7zip/UI/Console/ExtractCallbackConsole.cpp \ ../../../../CPP/7zip/UI/Console/HashCon.cpp \ ../../../../CPP/7zip/UI/Console/List.cpp \ ../../../../CPP/7zip/UI/Console/Main.cpp \ ../../../../CPP/7zip/UI/Console/MainAr.cpp \ ../../../../CPP/7zip/UI/Console/OpenCallbackConsole.cpp \ ../../../../CPP/7zip/UI/Console/PercentPrinter.cpp \ ../../../../CPP/7zip/UI/Console/UpdateCallbackConsole.cpp \ ../../../../CPP/7zip/UI/Console/UserInputUtils.cpp \ ../../../../CPP/Common/CRC.cpp \ ../../../../CPP/Common/CommandLineParser.cpp \ ../../../../CPP/Common/CrcReg.cpp \ ../../../../CPP/Common/IntToString.cpp \ ../../../../CPP/Common/ListFileUtils.cpp \ ../../../../CPP/Common/MyString.cpp \ ../../../../CPP/Common/MyVector.cpp \ ../../../../CPP/Common/MyWindows.cpp \ ../../../../CPP/Common/NewHandler.cpp \ ../../../../CPP/Common/Sha256Reg.cpp \ ../../../../CPP/Common/StdInStream.cpp \ ../../../../CPP/Common/StdOutStream.cpp \ ../../../../CPP/Common/StringConvert.cpp \ ../../../../CPP/Common/StringToInt.cpp \ ../../../../CPP/Common/UTFConvert.cpp \ ../../../../CPP/Common/Wildcard.cpp \ ../../../../CPP/Common/XzCrc64Reg.cpp \ ../../../../CPP/Windows/ErrorMsg.cpp \ ../../../../CPP/Windows/FileDir.cpp \ ../../../../CPP/Windows/FileFind.cpp \ ../../../../CPP/Windows/FileIO.cpp \ ../../../../CPP/Windows/FileName.cpp \ ../../../../CPP/Windows/PropVariant.cpp \ ../../../../CPP/Windows/PropVariantConv.cpp \ ../../../../CPP/Windows/Synchronization.cpp \ ../../../../CPP/Windows/System.cpp \ ../../../../CPP/Windows/TimeUtils.cpp \ ../../../../CPP/myWindows/myAddExeFlag.cpp \ ../../../../CPP/myWindows/mySplitCommandLine.cpp \ ../../../../CPP/myWindows/wine_date_and_time.cpp \ ../../../../C/7zCrc.c \ ../../../../C/7zCrcOpt.c \ ../../../../C/7zStream.c \ ../../../../C/Alloc.c \ ../../../../C/Bcj2.c \ ../../../../C/Bcj2Enc.c \ ../../../../C/Bra.c \ ../../../../C/Bra86.c \ ../../../../C/BraIA64.c \ ../../../../C/CpuArch.c \ ../../../../C/Delta.c \ ../../../../C/LzFind.c \ ../../../../C/LzFindMt.c \ ../../../../C/Lzma2Dec.c \ ../../../../C/Lzma2Enc.c \ ../../../../C/LzmaDec.c \ ../../../../C/LzmaEnc.c \ ../../../../C/MtCoder.c \ ../../../../C/Sha256.c \ ../../../../C/Threads.c \ ../../../../C/Xz.c \ ../../../../C/XzCrc64.c \ ../../../../C/XzCrc64Opt.c \ ../../../../C/XzDec.c \ ../../../../C/XzEnc.c \ ../../../../C/XzIn.c \ # Needed since ANDROID 5, these programs run on android-16 (Android 4.1+) # -fPIE 指的是生成与地址无关的可执行文件 , 该选项只能应用于可执行文件 , # 不能用于动态库编译 # 如果编译动态库 , 需要注释掉下面两个配置 # 如果编译可执行文件 , 可以保留下面两个配置 #LOCAL_CFLAGS += -fPIE #LOCAL_LDFLAGS += -fPIE -pie # include $(BUILD_EXECUTABLE) 配置表示生成可执行文件 # include $(BUILD_SHARED_LIBRARY) 配置表示生成 .so 动态库 # include $(BUILD_STATIC_LIBRARY) 配置表示生成 .a 静态库 # 此处生成静态库 include $(BUILD_STATIC_LIBRARY)
三、交叉编译 lib7zr.a 静态库
使用最低版本的 NDK 进行编译 , 使用高版本 NDK 编译各种报错 ;
使用 16.1.4479499 版本的 NDK 进行编译 :
>D:\001_Programs\001_Android\002_Sdk\Sdk\ndk\16.1.4479499\ndk-build.cmd