Friday, October 20, 2017

Adding a dependency to compileReleaseSources or compileDebugSources

Simply adding a dependency to compileReleaseSources causes a gradle error.

compileReleaseSources.dependsOn 'someTask'

Could not get unknown property 'compileReleaseSources' for project 

The dependency need to be added in whenTaskAdded closure:

tasks.whenTaskAdded { t ->
    if (t.name in ['compileReleaseSources', 'compileDebugSources']) {
        t.shouldRunAfter 'someTask'
        t.dependsOn 'someTask'
    }
}

Note that you need both t. shouldRunAfter (or t.mustRunAfter) and t.dependsOn.

Wednesday, October 18, 2017

Packaging dependencies into an AAR in Android Studio

When assembling a release AAR, proguard processes external dependencies using -libraryjars directive.  This is because the dependencies are not packaged into the resulting AAR.

In order for proguard to process dependencies using -injars directive, the external dependencies need to by converted to local dependencies by simply copying the JAR files to the project's libs directory and removing explicit external dependencies from dependencies section of your build.gradle file. Then local dependencies will be packaged into the AAR and obfuscated.  If you don't use proguard to minimize/obfuscate AAR, the dependencies will be packaged as is.

Just make sure that you have this line in the dependencies section:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    ...
}