I have long build.gradle
file which have functions I want to move into separate .gradle
file to keep logic clean. The docs suggests to use external build scripts for this case. I have next function at build.gradle
file:
android{
buildTypes {
debug {
signingConfig = loadFromPropertiesFile("DEBUG_KEY_PROPERTIES")
}
}
}
import com.android.builder.signing.DefaultSigningConfig
import com.android.builder.model.SigningConfig
SigningConfig loadFromPropertiesFile(keyProperty) {
// Load signing config from singning properties file
println keyProperty
println ("${keyProperty}")
if (project.hasProperty(keyProperty)) {
File releasePropsFile = new File(project.property(keyProperty))
println("Loading config from properties file: ${releasePropsFile}")
if (releasePropsFile.exists()) {
Properties releaseProps = new Properties()
releaseProps.load(new FileInputStream(releasePropsFile))
println releaseProps
def signingConfig = new DefaultSigningConfig(keyProperty)
signingConfig.storeFile = file(releasePropsFile.getParent() + "/" + releaseProps['keystore.file'])
signingConfig.storePassword = releaseProps['keystore.password']
//signingConfig.storeType = 'PKCS12'
signingConfig.keyAlias = releaseProps['alias.name']
signingConfig.keyPassword = releaseProps['alias.password']
return signingConfig
} else {
println("Can't read configuration file: ${releasePropsFile}")
return null
}
} else {
println("Project has not define configuration file: ${keyProperty}")
return null
}
}
The code logic doesn't matter, it is working fine when it is placed at build.gradle
file. But it fails when I move this method to the external file and include it with:
apply from: "$rootDir/gradle/android-signing.gradle"
I got next error:
Cannot cast object 'DefaultSigningConfig{..}' with class
com.android.builder.signing.DefaultSigningConfig' to class
'com.android.builder.model.SigningConfig'
Basically it says that it can not cast implementation to the interface. Because DefaultSigningConfig implements SigningConfig see here. Which makes no sense, until I see next answer.
Two classes are treated as entirely different classes, even if they have the same package and name (and even implementation/fields/methods) when loaded by different classloaders. Which is the case when you are using plugin or external build script.
But then, how can I split up methods from build.gradle
into modular separate files?