且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android 本地依赖 aar 管理(工程内建 maven 仓库)

更新时间:2022-08-16 11:24:12

aar添加到内建仓库使用脚本 publishlocalmaven.sh:

#!/bin/bash
##############################使用方式##############################
# 需安装 shyaml https://github.com/0k/shyaml
HELP="
用法:

1. 将要添加到仓库的 aar 包放在 lib_aar 文件夹下;

2. 配置 config.yaml, 格式:
groupId: com.thirdparty.aar
artifacts:
 -
   file: ambilWarna.aar
   artifactId: ambilWarna
   version: 1.0.0

3. 运行 publishlocalmaven.sh 脚本:
./publishlocalmaven.sh

4. 添加成功后将原始 aar 文件删除;

5. 需依赖 aar 的模块引用:
implementation 'com.thirdparty.aar:ambilWarna:1.0.0'

"


CONFIG=config.yaml   # 配置文件

if [ -n $CONFIG  ]; then

  current_dir=$(cd `dirname $0`;pwd)    # 脚本所在目录

  config_file=${current_dir}/${CONFIG}    # 配置文件全路径

  groupId=$(cat $config_file | shyaml get-value groupId)

  echo groupId: $groupId

  j=$(cat $config_file | shyaml get-length artifacts)

  for ((i=0; i<=$j-1; i++))
  do
    file=$current_dir/$(cat $config_file | shyaml get-value artifacts.$i.file)
    artifactId=$(cat $config_file | shyaml get-value artifacts.$i.artifactId)
    version=$(cat $config_file | shyaml get-value artifacts.$i.version)
    if [ -f $file ];then
      mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file \
      -Dfile=$file \
      -DgroupId=$groupId -DartifactId=$artifactId \
      -Dversion=$version -Dpackaging=aar \
      -DlocalRepositoryPath=$current_dir
    else
      echo "文件不存在!!! -> [$file]"
    fi
  done

fi

配置文件 config.yaml:

groupId: com.thirdparty.aar
artifacts:
 -
   file: ambilWarna.aar
   artifactId: ambilWarna
   version: 1.0.0

使用方式

1. 主工程的 build.gradle 中添加项目内建仓库位置

repositories {
    maven {
        // 把 lib_aar 当成本地仓库存放离线使用的 aar
        url("file://${project.rootDir.absolutePath}/lib_aar")
    }
}

2. 将要添加到仓库的 aar 包(本地依赖包) 放在 lib_aar 文件夹下;

project
    |__ app
    |__ lib_aar                    # 内建仓库位置
        |__ com.thirdparty.aar     # 添加到内建仓库的仓库 aar 依赖
            |__ ambilWarna
                |__ 1.0.0
                    |__ ambilWarna-1.0.0.aar
                    |__ ambilWarna-1.0.0.pom
                |__ maven-metadata-local.xml
        |__ config.yaml            # 添加本地依赖 aar 到内建仓库的仓库的脚本使用的配置文件
        |__ publishlocalmaven.sh   # 添加本地依赖 aar 到内建仓库的仓库的脚本
        |__ ambilWarna.aar         # 要添加到内建仓库的本地依赖 aar, 添加完成后删除
    |__ build.gradle

3. 配置 config.yaml, 格式:

groupId: com.thirdparty.aar       # 添加依赖的 groupId, 不需要修改
artifacts:
 -
  file: ambilWarna.aar            # 要添加的本地依赖 aar 文件名, 文件要放在脚本当前目录下
  artifactId: ambilWarna          # 要添加的本地依赖 aar 添加后的 artifactId
  version: 1.0.0                  # 要添加的本地依赖 aar 添加后的 version

4. 运行 publishlocalmaven.sh 脚本:

安装 shyaml https://github.com/0k/shyaml

cd lib_aar

chmod a+x publishlocalmaven.sh

./publishlocalmaven.sh

5. 添加成功后将原始 aar 文件删除;

6. 需依赖 aar 的模块 build.gradle 中引用内建仓库的 aar:

dependencies {
    implementation 'com.thirdparty.aar:ambilWarna:1.0.0'
}

推荐文章