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;
}
Related
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"
}
I'm trying to set jvmargs for unit tests using kotlin-dsl and I can't get it to work.
This is so that I can add the "-noverify" argument and allow intellji test runner to collect code coverage info.
Groovy, works:
testOptions {
unitTests.all {
jvmArgs '-noverify'
}
}
Kotlin, doesn't work:
testOptions {
unitTests.all(KotlinClosure1<Any, Test>({
(this as Test).also { jvmArgs("-noverify") }
}, this))
}
This too:
testOptions {
unitTests.all(KotlinClosure1<Any, Test>({
(this as Test).also { jvmArgs = listOf("-noverify") }
}, this))
}
Nothing seems to work, what am I missing?
I was having the same issue. The following snippet works.
tasks.withType<Test>().all {
jvmArgs("-noverify")
}
Ref - https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/Test.html#jvmArgs-java.lang.Object...-
Verified using Gradle-5.1.-all version
This question is a little old but wanted to post an updated Kotlin DSL version for 2021:
testOptions.unitTests.all { it.jvmArgs("-noverify") }
I can add a module dependency using either of
aar dependency
local project module
Right now I am able to accomplish same using below
if (foo(":awesomemodule")) {
implementation 'com.example.app:awesomemodule:1.0'
}
else {
implementation project(':awesomemodule')
}
Now I want to repeat this code for multiple dependencies and want to create a method for same.
I want something like this
customAddImplementation(':awesomemodule')
ext.customAddImplementation = { moduleName ->
if (foo(moduleName)) {
return implmentation('com.example.app' + moduleName + ':1.0')
} else {
return project(path: moduleName)
}
}
But this approach is not working, as implementation() definition is not found when i add my dependencies using customAddImplementation(':awesomemodule')
Something like this should work:
def customModulePath(String moduleName) {
if (foo(moduleName)) {
return "com.example.app:$moduleName:1.0")
} else {
return project(":$moduleName")
}
}
dependencies {
implementation(customModulePath('awesomemodule'))
}
And bonus point: since the function doesn't try to add the custom module path to the implementation dependencies, but simply returns its path, you can ruse it to add dependencies to any other configuration.
I need a very simple way to get Robolectric 3.0 to turn on logging. I want to see the output from robolectric, not just my tests. Everything I tried off the web is not working.
Where do I stick this?
robolectric.logging.enabled = true
I tried the following:
In a robolectric.properties file in test/java/res
In a robolectric.properties file in test/java/resources
In a robolectric.properties file in test/res
In a robolectric.properties file in test/resources
In gradle:
afterEvaluate {
project.tasks.withType(Test) {
systemProperties.put('robolectric.logging.enable', 'true')
}
}
In gradle:
tasks.withType(Test) {
testLogging.exceptionFormat = 'full'
systemProperties.put('robolectric.logging.enable', 'true')
}
You can add to your tests:
#Before
public void init() {
ShadowLog.stream = System.out;
}
then use: gradle test -i
Source: Unable to get log.d or output Robolectrict + gradle
or Add in the build.gradle:
tasks.withType(Test) {
systemProperty "robolectric.logging", "stdout"
}
Source: https://github.com/studyplus/Studyplus-Android-SDK/blob/master/StudyplusAndroidSDK/build.gradle#L41
I would like to create dynamic flavors from the directory tree.
It works great!
But Android Studio uses gradle in its tmp file like:
/home/svirch_n/.IntelliJIdea14/system/compile-server
and my script doesn't work anymore because it uses relative paths like this:
Closure getFlavors = { rootDir, basePackage ->
def result = [:]
new File("$rootDir").eachDir() { dir ->
def name = dir.getName()
if ("$name" != "main")
result.put("$name", "$basePackage.$name")
}
return result
}
// This is an ugly closure.
// If I can get rid of this, my problem will be solved
Closure getSrcPath = {
if (System.getProperty("user.dir").split("/").last() == "app") {
return "src"
} else {
return "app/src"
}
}
android {
...
def myFlavors = getFlavors(getSrcPath(), "com.example.app")
productFlavors {
myFlavors.each { flavorName, flavorPackage ->
"$flavorName" {
applicationId "$flavorPackage"
}
}
}
}
Do you have an idea how to solve this?
Thanks in advance for your help
P.S: I want dynamic flavors cause my git project has public and private repositories and not everyone can have all the flavors but I want them to compile anyway.
Assuming I am in the subproject 'app', I can use:
project(":app").getProjectDir().getPath()