android杂记

Android gradle aar发布

2023-03-11  本文已影响0人  印记乄

碰到一个需求,需将项目sdk发布到内网maven中,这个项目结构比较特殊,生成的sdk可以根据配置打包不同的模块。
特此记录Android sdk maven发布流程。

apply plugin: 'maven-publish'

//过滤不需要加入依赖规则的引用依赖
boolean exludeDependency(Dependency dependency) {
    if (dependency.version != "unspecified"
            && dependency.name != "unspecified"
            && dependency.name != "cos-android") {
        return true
    }
    return false

}


def publicationList = new ArrayList<>()

class LocalArtifact {
    String taskName;
    String artifactId;

    LocalArtifact(String taskName, String artifactId) {
        this.taskName = taskName
        this.artifactId = artifactId
    }

}

void initPublicationList(List publicationList) {
    if (rootProject.ext.prop.supportObu) {
        publicationList.add(new LocalArtifact("bundleChenYiDebugAar", "replay-debug"))
        publicationList.add(new LocalArtifact("bundleChenYiReleaseAar", "replay-release"))
    } else {
        publicationList.add(new LocalArtifact("bundleDebugAar", "replay-debug"))
        publicationList.add(new LocalArtifact("bundleReleaseAar", "replay-release"))
    }

}


publishing {
    publications {
        initPublicationList(publicationList)
        println("publicationList == >> " + publicationList.size())
        publicationList.forEach { localArtifact ->
            println("publicationList == >> " + localArtifact.taskName)
            "${localArtifact.artifactId}"(MavenPublication) {
                groupId = "com.xxx.xxx"//sdk包名
                artifactId = localArtifact.artifactId//该aar包的名称
                version = "${rootProject.ext.android.versionName}"//版本号
                if (rootProject.ext.prop.isSnapshots) {
                    version += "-SNAPSHOT"   //建议debug版本采用snapshot,snapshot可以不更新版本号,引用放即可更新sdk。 
                }
                afterEvaluate { artifact(tasks.getByName("${localArtifact.taskName}")) }
                pom.withXml {
                    def dependenciesNode = asNode().appendNode("dependencies")
                    configurations.implementation.allDependencies.forEach() {
                        Dependency dependency ->
                            if (exludeDependency(dependency)) {
                                def dependencyNode = dependenciesNode.appendNode('dependency')
                                dependencyNode.appendNode('groupId', dependency.group)
                                dependencyNode.appendNode('artifactId', dependency.name)
                                dependencyNode.appendNode('version', dependency.version)
                            }
                    }
                }
            }
        }
    }
    repositories {
        maven {
          //分别relase地址和snapshots地址。
            def releasesRepoUrl = "https:xxx/repository/maven/xxx"
            def snapshotsRepoUrl = "https://xxx/repository/maven/xxxx-snapshots"
            url = rootProject.ext.prop.isSnapshots ? snapshotsRepoUrl : releasesRepoUrl
            credentials {
                username "${System.getenv("mavenUser")}"
                password "${System.getenv("mavenPwd")}"
            }
            authentication {
                basic(BasicAuthentication)
            }
        }
    }
}
//工程根目录下build.gradle中。
//一定是allprojects中
allprojects {
    repositories {
        google()
        mavenCentral()
        //...
        maven {
            url 'https://mirrors.tencent.com/repository/maven/cv2x-snapshots'
        }
    }
}

//对应模块的dependencies中
implementation 'com.xxx:xxx:xxx-SNAPSHOT'
//implementation 'com.xxx:xxx:xxx'
//对应模块 build.gradle中
configurations.all { resolutionStrategy.cacheChangingModulesFor 0, 'seconds' }

上一篇 下一篇

猜你喜欢

热点阅读