Skip to content

Instantly share code, notes, and snippets.

@ivanempire
Created February 5, 2022 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ivanempire/209cb82e35478ca21964708c211900c0 to your computer and use it in GitHub Desktop.
Save ivanempire/209cb82e35478ca21964708c211900c0 to your computer and use it in GitHub Desktop.
Companion gist for setting up Artifactory in Gradle
// Goes into your root build.gradle for initial project setup
buildscript {
repositories {
google()
jcenter() // You'll need jcenter, ignore the warning - you know, like real devs do :D
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.1.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.17.2"
}
}
// Also into root build.gradle
// Applying the necessary plugins to all projects
allprojects {
apply plugin: "com.jfrog.artifactory"
apply plugin: "maven-publish"
}
// Also into root build.gradle
// Specify destination settings for Artifactory to publish artifacts to
artifactory {
contextUrl = "https://your_artifactory_url"
publish {
repository {
repoKey = "your_artifactory_repo_key"
username = "your_artifactory_username"
password = "your_artifactory_apiKey"
maven = true
}
}
}
// SAMPLE 1
// Can go into root build.gradle, or into your main project - the one that will be generating
// an Android application package. If you put it into the module build.gradle, remove the
// project("app") {} outer closure.
// Example setup for publishing a single APK from your app project. Publication will assemble
// the APK first (set up by the "dependsOn" line), and then push the APK to the specified
// artifactId, groupId with version string v1.0.0. The repository used will be taken from the
// artifactory block above.
// To run: ./gradlew :app:artifactoryPublish
project("app") {
artifactoryPublish.dependsOn("assemble")
publishing {
publications {
apk(MavenPublication) {
artifactId = "artifactory_artifactId"
groupId = "artifactory_groupId"
version = "v1.0.0"
artifact("$buildDir/outputs/apk/release/${project.getName()}-release-unsigned.apk")
}
}
}
artifactoryPublish {
publications(publishing.publications.apk)
}
}
// SAMPLE 2
// Add this to your library module's build.gradle file
// Near identical example to the one above, but will publish a single AAR from your library
// project to the specified coordinates.
// To run: ./gradlew :libraryModuleName:artifactoryPublish
artifactoryPublish.dependsOn("assemble")
publishing {
publications {
aar(MavenPublication) {
artifactId = "artifactory_artifactId"
groupId = "artifactory_groupId"
version = "v1.0.0"
// AAR name will depend on your project settings here
artifact("$buildDir/outputs/aar/path_to_some_aar.aar")
}
}
}
artifactoryPublish {
publications(publishing.publications.aar)
}
// SAMPLE 3
// Add this to your library module's build.gradle file
// A bit of a different example than the one above, but if your and your team need to publish
// a debug and release version of your library project, this is the way to do it. This block
// will create two artifacts in your Artifactory repository: artifactory_artifactId-debug and
// artifactory_artifactId-release. Could be useful if you'd like to consume different versions
// for different configurations.
// To run: ./gradlew :libraryModuleName:artifactoryPublish
artifactoryPublish.dependsOn("assemble")
publishing {
publications {
android.buildTypes.all { variant ->
"${variant.name}Aar"(MavenPublication) {
artifactId = "artifactory_artifactId-${variant.name}"
groupId = "artifactory_groupId"
version = "v1.0.0"
// Going off of default Gradle naming configurations
artifact("$buildDir/outputs/aar/${project.getName()}-${variant.name}.aar")
// If you need multiple build types under one artifactId, for example debug and
// release AARs under libraryModuleName, then the above line needs to be changed
// to the following:
artifact("$buildDir/outputs/aar/${project.getName()}-${variant.name}.aar") {
classifier variant.name
extension "aar"
}
// You will need to adjust the way you import the library in your consuming
// application to something like:
// implementation "groupId:artifactId:v1.0.0-debug:aar"
}
}
}
}
artifactoryPublish {
// This is the "shorthand" for publications("debugAar", "releaseAar")
android.buildTypes.all { variant ->
publications("${variant.name}Aar")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment