How to have android { } block outside build.gradle.kts - android

I used to have the following structure before migrating to .kts:
app/build.gradle
...
apply from "secrets.gradle"
android {
...
}
secrets.gradle
android {
signingConfigs {
...
// extra stuff that used to get merged
}
}
I recently converted them to .kts. I get Unresolved reference: android in secrets.gradle.kts.
Commenting out apply(from = "secrets.gradle.kts") from app/build.gradle.kts gets rid of the error, so it seems like android { } block is not allowed in secrets.gradle.kts
Is there a way to have android { } block in secrets.gradle.kts and get the previous merging behaviour?

Related

Extension of type 'LibraryExtension' does not exist

I created a gradle plugin to unify some settings between the various modules of the application. the summary of the error is this:
org.gradle.api.plugins.InvalidPluginException: An exception occurred applying plugin request [id: 'common.plugin']
Caused by: org.gradle.api.UnknownDomainObjectException: Extension of type 'LibraryExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension,....]
This is a summary image of the project architecture:
CommonPluginClass:
class CommonPluginClass : Plugin<Project> {
private val consumerProguardFileName = "consumer-rules.pro"
private val proguardFileName = "proguard-rules.pro"
private val sdkToCompile = 33
override fun apply(project: Project) {
println(">>> Adding sugar to gradle files!")
with(project)
{
applyPlugins(this)
androidConfig(this)
}
println(">>> Sugar added for core Module!")
}
private fun applyPlugins(project: Project) {
println(">>> apply plugins!")
project.pluginManager.run {
apply("com.android.application")
apply("kotlin-android")
apply("kotlin-kapt")
}
println(">>> end apply plugins!")
}
private fun androidConfig(project: Project) {
project.extensions.configure<LibraryExtension>{
defaultConfig.targetSdk = 33
}
}
}
the error occurs inside the androidConfig function when calling configure
I was inspired by now android, dependencies and imports are similar but not build. Can someone unlock it please.
build-logic:convention build.gradle
plugins {
alias(libs.plugins.kotlin.jvm) apply false
id "org.gradle.kotlin.kotlin-dsl" version "2.4.1"
}
dependencies {
compileOnly(libs.android.pluginGradle)
compileOnly(libs.kotlin.pluginGradle)
}
gradlePlugin {
plugins {
commonPlugin {
id = "common.plugin"
implementationClass = "CommonPluginClass"
}
}
}
LITTLE UPDATE:
I've noticed that any module I enter always identifies it to me as ApplicationExtension
I found the solution and as usual it's a stupid thing.
the application extension use plugin
apply("com.android.application")
the library or module use plugin
apply("com.android.library")

How to exclude (ignore) android build variants in gradle kts

During migration build script from groovy to kotlin I met problem with excluding build variants.
In groovy it's pretty easy:
android {
variantFilter { variant ->
if (variant.name == "lorempisum") {
setIgnore(true)
}
}
}
but in kotlin similar thing does not work. It seems to be ok in android studio, but during compilation it throws Unresolved reference: isIgnore
android {
variantFilter {
if (buildType.name == "lorempisum") {
isIgnore = true
}
}
}
from the other side this reports Unresolved reference: setIgnore, but works during compilation
android {
variantFilter {
if (buildType.name == "lorempisum") {
this.setIgnore(true)
}
}
}
Anybody has idea how do it in right way?
I'm using kotlin 1.3.72, android studio 4.0.1 and gradle 6.5.1
---- EDIT ----
I fix example ignore -> isIgnore in second block, it also not works
Soultion is ignore = true with a little deatil.
This works if you keep in top-level build.gradle.kts this line:
classpath("com.android.tools.build:gradle:4.0.1")
and not really only on buildSrc on this:
implementation("com.android.tools.build:gradle:4.0.1")
Use the beforeVariants block:
androidComponents {
beforeVariants { variantBuilder ->
// To check for a certain build type, use variantBuilder.buildType == "<buildType>"
if (variantBuilder.productFlavors.containsAll(listOf("api" to "minApi21", "mode" to "demo"))) {
// Gradle ignores any variants that satisfy the conditions above.
variantBuilder.enabled = false
}
}
}
You should first update to the last version of android studio and the plugins.
And try this
variantFilter {
this.ignore = name == "lorempisum"
}

Fabric Beta release notes and Gradle Kotlin DSL

As stated here: https://docs.fabric.io/android/beta/gradle.html
I used to have this 2 lines in an Android app's build.gradle to upload builds to Fabric Beta with given release notes to given group aliases:
android {
defaultConfig {
ext.betaDistributionReleaseNotesFilePath = 'app/release_notes.txt'
ext.betaDistributionGroupAliasesFilePath = 'app/group_aliases.txt'
}
}
After migrating the build.gradle files to Kotlin Gradle DSL these 2 lines have become:
android {
defaultConfig {
ext.set("betaDistributionReleaseNotesFilePath", "app/release_notes.txt")
ext.set("betaDistributionGroupAliasesFilePath", "app/group_aliases.txt")
}
}
But they no longer work: all builds published to Fabric Beta are with blank release notes and no group aliases.
I've tried several alternative syntaxes to set the extra properties:
val betaDistributionReleaseNotesFilePath by extra { "app/release_notes.txt" }
or
ext["betaDistributionReleaseNotesFilePath"] = "app/release_notes.txt")
or
extra.set("betaDistributionReleaseNotesFilePath", "app/release_notes.txt")
or
project.ext.set("betaDistributionReleaseNotesFilePath", "app/release_notes.txt")
But results don't change.
Adding and running a debug task defined as:
tasks.create("myTask") {
dependsOn("assembleDebug", "crashlyticsUploadDistributionDebug")
doLast {
val a = ext["betaDistributionReleaseNotesFilePath"]
val b = ext["betaDistributionGroupAliasesFilePath"]
println("ReleaseNotes: $a")
println("GroupAliases: $b")
}
}
Prints:
ReleaseNotes: app/release_notes.txt
GroupAliases: app/group_aliases.txt
only if the extra properties have been set using project.ext.set(), but still the uploaded builds have blank release notes and no group aliases.
What am I getting wrong?
you can use:
configure<CrashlyticsExtension> {
betaDistributionReleaseNotes = "xxx"
}

Unresolved reference kotlintest while using gradle kotlin dsl

Here is my build.gradle.kts :
import org.jetbrains.kotlin.config.KotlinCompilerVersion
plugins {
...
}
android {
compileSdkVersion(27)
defaultConfig {
...
}
buildTypes {
...
}
sourceSets {
getByName("main").java.srcDirs("src/main/java", "src/main/kotlin")
getByName("test").java.srcDirs("src/test/java", "src/test/kotlin")
getByName("androidTest").java.srcDirs("src/androidTest/java", "src/androidTest/kotlin")
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
dependencies {
implementation(kotlin("stdlib-jdk7", KotlinCompilerVersion.VERSION))
implementation("com.android.support:appcompat-v7:27.1.1")
...
//Test
testImplementation("org.junit.jupiter:junit-jupiter-api:5.1.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.1.1")
androidTestImplementation("com.android.support.test:runner:1.0.2")
androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.2")
androidTestImplementation("android.arch.core:core-testing:1.1.1")
testImplementation("io.kotlintest:kotlintest-runner-junit5:3.1.10")
testImplementation("io.mockk:mockk:1.8.7")
}
When building it with ./gradlew build no error occurs but if I use the specified codefrom the doc :
val test by tasks.getting(Test::class) {
useJUnitPlatform { }
}
I get the following error : Task with name 'test' not found in project ':app'.
My settings.gradle.kts:
include(":app")
Does anyone know what's going on here? It's weird AS can suggest me autocompletion and all but while compiling, I get this unresolved reference.
Looks like your issue is with this block,
tasks.withType<Test> {
useJUnitPlatform()
}
If you're not using test task then you can simply remove that
code and that error will be gone !
Edit:
If you want to use test task. try like this in project level build.gradle,
tasks.register("test", Test::class) {
useJUnitPlatform()
}

No such property: betaDistributionApkFilePath for class: java.lang.String

I have found little glitch in crashlytics/fabric setup in gradle which took 2 hours of my poor life.
first of all i did everything they said in this guide:
https://fabric.io/kits/android/crashlytics/install
and here is the piece of code which everything caused:
First try
dependencies {
...
compile('com.crashlytics.sdk.android:crashlytics:2.9.4#aar') {
transitive = true;
}
}
every library version in dependencies{} block iam using ext{} block in appname/app/build.gradle so in our case
ext {
crashlytics = '2.9.4#aar'
}
so in the end it will be like this only change implementation which should be there in my opinion because it will be deprecated at end of 2018. implementations doenst change any behavior.
implementation("com.crashlytics.sdk.android:crashlytics:$crashlytics"){
transitive = true
}
but if u try to build this u gonna get:
No such property: betaDistributionApkFilePath for class: java.lang.String
wow. i have no idea why.. but lets investigate this. we will try different setup so forget everyting you saw above.
Second try
in our appname/app/build.gradle in dependencies{} change our lane to this so we have no ext{} for version
implementation("com.crashlytics.sdk.android:crashlytics:$rootProject.ext.crashlytics") {
transitive = true
}
and now in our root gradle appname/build.gradle
buildscript {
ext.fabric_gradle = '2.9.4#aar'
repositories {
...
}
dependencies {
...
}
}
Build is OK you can continue to work. But what? Its should be completely the same...
Third try
I dont know why but in our first try u just CANT in appname/app/build.gradle ext{} block name crashlytics variable it have to very everything else except crashlytics for instance crashlytics_version so lets make first try to working state.
ext {
crashlytics_version = '2.9.4#aar'
}
implementation("com.crashlytics.sdk.android:crashlytics:$crashlytics_version"){
transitive = true
}
Build is OK. Magic.
or just do it in normal way and dont try to make smart things.... As first block of code in this long investigation which works also completely fine:
compile('com.crashlytics.sdk.android:crashlytics:2.9.4#aar') {
transitive = true;
}
You just CANT in appname/app/build.gradle ext{} block name your variable crashlytics it have to be everything else except crashlytics for instance crashlytics_version so:
ext {
crashlytics_version = '2.9.4#aar'
}
implementation("com.crashlytics.sdk.android:crashlytics:$crashlytics_version"){
transitive = true
}
Build is OK.
or just do it in normal way:
compile('com.crashlytics.sdk.android:crashlytics:2.9.4#aar') {
transitive = true;
}

Categories

Resources