I'm playing with Gradle for Android recently... It's powerful but painful at the same time.
Initially, I followed Roboletric sample project, trying to make a gradle-based Android project with Robolectric testing support. After a while, I realise Robolectric doesn't even support API 19. Thus, I remove it from my build.gradle and try to use Junit and Instrumentation tests.
I'm not familiar with either Android or Gradle or Roboletric. I only know that before I removed Robolectirc, whenever I made a build ./gradlew clean build I was able to run all the tests. But now, I have to call ./gradlew connectedAndroidTest in addition to run tests while the build command only calls check but no test is run and thus always show SUCCESSFUL
I have attached my configuration here:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
minSdkVersion 9
targetSdkVersion 19
versionCode 1
versionName "1.0"
packageName "com.example"
testPackageName "com.example.tests"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
productFlavors {
flavorDimensions "AppPackage", "AppEnvironment"
appBasic {
flavorDimension "AppPackage"
}
staging {
flavorDimension "AppEnvironment"
}
production {
flavorDimension "AppEnvironment"
}
}
sourceSets {
androidTest.setRoot('src/test')
androidTestStaging.setRoot('src/testStaging')
androidTestProduction.setRoot('src/testProduction')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:20.0.0'
compile 'com.android.support:appcompat-v7:20.0.0'
}
By default the connectedAndroidTest task don't run when you run the build task.
By default the check task run when you run the build task.
All you have to do is to add an explicit dependency to run the connectedAndroidTest task when the check task run :
check.dependsOn connectedAndroidTest
Related
Upgraded to:
Android Studio 3.0 Canary 2
com.android.tools.build:gradle:3.0.0-alpha2
I have a multi-module project (main app + sub modules)
Inclusion inside the main app:
dependencies {
implementation project(path: ':testlib', configuration: 'default')
}
The testlib is defined as a simple android library project and works normally when included with gradle 2.3.0 and via compile project(path: ':testlib')
I get the following gradle error message:
Could not resolve all dependencies for configuration ':app:devDebug_signedCompileClasspath'.
Selected configuration 'default' on 'project :testlib'
but it can't be used as a project dependency because
it isn't intended for consumption by other components.
What does "isn't intended for consumption by other components" mean in this context? The module is defined as an android library.
Here is the build.gradle of the testlib:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha2'
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
What am I missing?
I also got this error. Android Studio 3.0 Canary 4 just came out. I updated to it, which also updates gradle to 4.0rc1.
The problem went away on it's own.
alter your Top-level build file (aka root gradle)
classpath 'com.android.tools.build:gradle:3.0.0-alpha4'
still not working?
update dist-url (inside gradle-wrapper.properties)
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-rc-1-all.zip
I am learning Gradle for Android recently. For learning impressively, i create all android application files manually.
my project dirs as below
package name : com.wonbin.gradledemo
project build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
}
}
allprojects {
repositories {
jcenter()
}
}
app module build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.wonbin.gradledemo"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "adroid.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
lintOptions {
htmlReport true
htmlOutput file("lint-results.html")
}
}
dependencies {
compile fileTree(dir:'libs',include:['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{
exclude group: 'com.android.support',module: 'support-annotations'})
compile 'com.android.support:appcompat-v7:23.4.0'
testCompile 'junit:junit:4.12'
}
./gradlew build is successful and there are 'intermediates' and 'generated'
dirs , but no outputs dir, i don't know what to do!
Thanks a lot!
You need not only to build, but to assemble it as well.
To do this you can run a gradle task
assembleDebug
Or use Build -> Build APK main menu item
I am trying to add some unit tests to my app. I am developing my app in Android Studio
This is what I did.
Added a new package
Created a class in new package which extends TestCase
Added following method to created class
#SmallTest
public void basicTest() {
assertEquals("abc", "abc");
}
Added following to defaultConfig section in build.gradle
testApplicationId "newly.added.package.name"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
Executed following command in Android Studio Terminal
gradlew.bat connectedAndroidTest
But, when I checked the generated html report, it shows that 0 tests were run.
I tried following, but none of them made a difference to the output of gradlew.bat connectedAndroidTest command.
Added an incorrect package name to testApplicationId in build.gradle.
Disconnected device
Why doesn't my test case get executed? What have I missed?
Following is my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "my.package.name"
minSdkVersion 12
targetSdkVersion 18
testApplicationId "my.package.name.tests"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile project(':my_sub_module1')
compile project(':my_sub_module2')
compile 'com.android.support:support-v4:21.0.2'
compile files('libs/my_dependent_lib-1-7-4.jar')
compile 'com.google.code.gson:gson:2.3'
}
repositories {
mavenCentral()
}
You should use the following testInstrumentationRunner:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Also, click Build Variants on the left side of Android Studio, and select
Android Instrumentation Tests
in the dropdown.
I just built a new laptop and changed from Ubuntu 12.04 to Ubuntu 14.04. I'm running AS 0.8.2 on both machines (old and new). When trying to pick up development on the new machine AS shows Cannot Resolve Symbol errors on all android classes. It builds just fine either through the IDE or through gradle on the command line. But the code completion in the editor shows red all over.
I have the min sdk and the target sdk installed through the SDK Manager. I've built it with gradle 1.10 and gradle 1.12. I have tried backing off the build tools version to 19.1 with no effect. As far as I can see the only difference is that this setup is on 14.04.
build.gradle:
buildscript {
repositories {
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'crashlytics'
repositories {
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
}
android {
signingConfigs {
releaseConfig {
<stuff here>
}
debugConfig {
<stuff here>
}
}
compileSdkVersion 19
buildToolsVersion '20'
defaultConfig {
applicationId 'com.domain'
minSdkVersion 14
targetSdkVersion 19
versionCode 0
versionName '0.1.0'
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.releaseConfig
zipAlign true
}
debug {
signingConfig signingConfigs.debugConfig
zipAlign true
applicationIdSuffix '.debug'
}
}
productFlavors {
freeFlavor {
applicationId 'com.domain.lite'
}
paidFlavor {
applicationId 'com.domain.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:2.2.+'
compile 'com.crashlytics.android:crashlytics:1.+'
compile 'com.android.support:support-v4:21.+'
}
Any thoughts, or anyone else running into this?
EDIT
Interesting. If I copy the android.jar from the sdk into the project's libs directory AS recognizes things just fine. So it's not recognizing the sdk location of its contents. I've checked in the project structure and it is correctly set to /usr/android-studio/sdk
Is is possible to setup robolectric in its own module and add it as a dependency to my project? I can add it to my project module fine but id prefer if it was in its own module. Ive created a javaLibrary module and added the following code to the gradle build script
buildscript {
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependencies {
//Prior to AS 0.5.9: classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.+'
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.0'
//previous plugin >> classpath 'com.novoda.gradle:robolectric-plugin:0.0.1-SNAPSHOT'
}
}
allprojects {
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
}
apply plugin: 'android'
apply plugin: 'android-test' //previously >> apply plugin: 'robolectric'
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "com.irishtimes.newsapp.tests"
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
androidTest.setRoot('src/tests') //note that this is androidTest instead of instrumentTest
}
}
dependencies {
androidTestCompile 'junit:junit:4.10'
//include the stable robolectric 2.3 library
androidTestCompile 'org.robolectric:robolectric:2.3'
androidTestCompile 'com.squareup:fest-android:1.0.+'
}
//only include files that are suffixed with Test.class and set the max heap size
androidTest {
include '**/*Test.class'
maxHeapSize = "2048m"
}
Ive also added a blank manifest file and a dummy test in a src/tests/java. In my previous setup when i run ./gradlew test, my tests would run and id get feedback. Now when i run the same command my build script runs but it does not pick up my tests at all. If anyone has any reference to tutorials or advice or a quick solution that would be great. Thanks!
Yes its possible with a different plugin. Plugin supported by robolectric do not support this.
Plugin can be found here https://github.com/novoda/gradle-android-test-plugin
A ready to use example can be found here https://github.com/nenick/android-gradle-template
If you want to run tests from a separate folder, but not necessarily a separate module, I have had some success with the Android Studio Unit Test Plugin along with this gradle plugin.