Jvm options in android when run gradlew test

2019-01-23 15:56发布

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?

3条回答
小情绪 Triste *
2楼-- · 2019-01-23 16:24

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.

查看更多
闹够了就滚
3楼-- · 2019-01-23 16:28

Maybe this

 ./gradlew -Dorg.gradle.jvmargs="-ea -noverify" test
查看更多
劳资没心,怎么记你
4楼-- · 2019-01-23 16:32

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'
    }
  }
}
查看更多
登录 后发表回答