Jvm options in android when run gradlew test
Asked Answered
P

3

25

I have a project that using Robolectric for unit test purpose. This project uses Robolectric 3.0 and need to add -ea and -noverify options in Virtual Machine options.

In Android Studio, I created new JUnit configuration in Run > Edit Configurations... and then set VM Options to -ea -noverify. With this way I success to run my unit test. This is image about my configure, view Here

However, for continuous deployment, I need run unit test with command line. So I use ./gradlew test to run unit test. I also add org.gradle.jvmargs=-ea -noverify to gradle.properties file. Unfortunately, it doesn't work. I can run unit test but I got java.lang.VerifyError and I think that gradle.properties was not load.

So, my question is, how to make gradle.properties load or do you know any way to fix my vm options problem?

Pretzel answered 31/8, 2015 at 16:40 Comment(0)
P
9

I found that we can add this block to app's build.gradle to solve this problem

tasks.whenTaskAdded { theTask ->
    def taskName = theTask.name.toString()
    if ("testDevDebug".toString().equals(taskName)) {
        theTask.jvmArgs('-ea', '-noverify')
    }
}

DevDebug is my build variant.

Pretzel answered 1/9, 2015 at 2:26 Comment(1)
Gradle DSL method not found: 'jvmArgs()'Quinonoid
I
45

It is already answered but this may be an easier solution:

In your application modules' build.gradle file in android closure, add this.

android {
  ....

  testOptions {
    unitTests.all {
      jvmArgs '-noverify'
    }
  }
}
Internationalism answered 2/6, 2016 at 13:24 Comment(6)
This one is so much cleaner. "gradle way"Fearnought
Has anybody had any experience getting this working with Android Studio 2.3? I am still seeing tests crash even with this in place.Breathed
this does not work on 2.3.3. the accepted answer works greatDisarticulate
It worked for me. I have the Android plugin v. 3.0.1Expressman
This definitely works with plugin 3.0.1. I remember having this with plugin version 2.x too.Internationalism
This solution works but you still need to specify the -noverify in the test configuration in Android Studio if you want to run them from there.Neighbor
P
9

I found that we can add this block to app's build.gradle to solve this problem

tasks.whenTaskAdded { theTask ->
    def taskName = theTask.name.toString()
    if ("testDevDebug".toString().equals(taskName)) {
        theTask.jvmArgs('-ea', '-noverify')
    }
}

DevDebug is my build variant.

Pretzel answered 1/9, 2015 at 2:26 Comment(1)
Gradle DSL method not found: 'jvmArgs()'Quinonoid
J
0

Maybe this

 ./gradlew -Dorg.gradle.jvmargs="-ea -noverify" test
Julieannjulien answered 31/8, 2015 at 17:46 Comment(2)
I did try it before but still got the java.lang.verifyError.Pretzel
I think it needs a -P to add as a Gradle property instead of -D. As I understand it, -D passes arguments to the JVM, whereas -P passes it to Gradle. In this instance, you're passing a Gradle property to pass arguments again to JVM.Gapeworm

© 2022 - 2024 — McMap. All rights reserved.