Quickly Toggle Between Artifactory and Local Android Libraries

Siju
2 min readAug 18, 2024

--

When working with an Android library, it’s common to make changes and quickly test them in your app. Constantly switching between using your local library and an artifactory version can be tedious, especially if it involves commenting and uncommenting lines in your build.gradle file.

Here's a more efficient way to toggle between the two using a simple boolean flag.

Note: This post uses some of tips from my previous post .

Step 1: Create a Configuration File

First, create a config.properties file to manage the switch between your Artifactory and local project. The file should contain a boolean flag like this:

# Set to false to use local library project
useArtifactory=true

Step 2: Set Up a Gradle Settings File

Next, create a settings_library.gradle file that loads the properties from the configuration file and determines which library to use.

def properties = new Properties()
file("config.properties").withInputStream { properties.load(it) }
def useArtifactory = properties.getProperty("useArtifactory").toBoolean()

def osName = System.getProperty("os.name").toLowerCase()

if (!useArtifactory) {
include ':moduleName'
if (osName.contains("win")) {
project(':moduleName').projectDir = new File('C:\\Users\\sj\\AndroidStudioProjects\\TestLibrary', 'app')
}
else {
project(':moduleName').projectDir = new File('/Users/sj/AndroidStudioProjects/TestLibrary', 'app')
}
}

Step 3: Reference the Settings File

In your main settings.gradle file, reference the settings_library.gradle file to apply the configuration:

apply from:'settings_library.gradle'

Step 4: Load Properties in the Root build.gradle

To make the useArtifactory variable accessible across all your build.gradle files, load the properties again in your root build.gradle:

def properties = new Properties()
file("config.properties").withInputStream { properties.load(it) }
ext.useArtifactory = properties.getProperty("useArtifactory").toBoolean()

Step 5: Adjust Your Module’s build.gradle

Finally, in your module’s build.gradle, use the useArtifactory flag to toggle between the artifactory and local versions of your library:

dependencies {
if (useArtifactory) {
implementation "com.test.library:1.0.0" //Fetching the library from artifactory
}
else {
implementation project(':moduleName')
}
}

Note: You might need to rebuilt gradle tasks after switching back and forth between artifactory and local project.

And that’s it! By simply changing a flag in your config.properties file, you can quickly switch between using your local library and the version stored in artifactory.

--

--

Siju
Siju

No responses yet