android sdk打包aar方案步骤
1.使用fat-aar库https://github.com/kezong/fat-aar-android/blob/master/README_CN.md
第一步:添加以下代码到你工程根目录下的build.gradle
文件中:
For Maven Central (The lastest release is available on Maven Central):
buildscript {repositories {mavenCentral()}dependencies {classpath 'com.github.kezong:fat-aar:1.3.8'} }
第二步: Add plugin
添加以下代码到你的sdk所在library的build.gradle
中:
apply plugin: 'com.kezong.fat-aar'
第三步: Embed dependencies
embed
你所需要的工程, 用法类似implementation
代码所示:
dependencies {implementation fileTree(dir: 'libs', include: '*.jar')// java dependencyembed project(path: ':lib-java', configuration: 'default')// aar dependencyembed project(path: ':lib-aar', configuration: 'default')// aar dependencyembed project(path: ':lib-aar2', configuration: 'default')// local full aar dependency, just build in flavor1flavor1Embed project(path: ':lib-aar-local', configuration: 'default')// local full aar dependency, just build in debugdebugEmbed(name: 'lib-aar-local2', ext: 'aar')// remote jar dependencyembed 'com.google.guava:guava:20.0'// remote aar dependencyembed 'com.facebook.fresco:fresco:1.12.0'// don't want to embed inimplementation('androidx.appcompat:appcompat:1.2.0') }
第四步: 执行assemble命令
- 在你的工程目录下执行assemble指令,其中lib-main为你主library的工程名称,你可以根据不同的flavor以及不同的buildType来决定执行具体的assemble指令
# assemble all ./gradlew :lib-main:assemble# assemble debug ./gradlew :lib-main:assembleDebug# assemble flavor ./gradlew :lib-main:assembleFlavor1Debug
2.将会引起冲突的库使用jar包依赖可以减小aar的体积
3.将sdk所在目录的混淆打开可以减小aar的体积
遇到的坑:
1.如果sdk目录的libs包下引用了jar包,需要在sdk的build.gradle中dependencies添加
implementation fileTree(dir: 'libs', include: '*.jar')
否则会出现jar包打不进sdk中的问题
2.module里面通过Gradle方式集成的依赖不会打进aar里面,把需要打进aar里面的东西用jar依赖,可能会引起依赖冲突的库,用Gradle方式