jeudi 8 octobre 2015

How to add test folders to an older Android Studio Project

I'm having some problems adding the project structure for testing, to an older Android project in Android Studio.

When you create a new project in Android Studio, you get the directories for testing created from start.

src/test/java - for unit testing 
src/androidTest/java - for Android/UI testing. 

But our project didn't have those, since it's an older project we have migrated from Eclipse.

I have succesfully added the directory (test) and java folder (test/java) for unit testing to our project, where the java directory is recognized as a java directory, so I can add new packages and java files.

When I look at the packages in the Android Project view, they're listed as:

com.ourcompany.ourapp
com.ourcompany.ourapp (test) - And here I can see my testclasses. 

All good so far.

But I cannot seem add the structure for androidTest.

If I add the directory androidTest and a javafolder called java under it, the java folder is marked as green, and I can add classes and packages. But the testpackage is not marked as a test.

I get this result in the android project view.

com.ourcompany.ourapp
com.ourcompany.ourapp - And here is the testclass, MainActivityTest.java
com.ourcompany.ourapp (test)

When I added the androidTest/java folder, this line appeared in the build.gradle file:

android { sourceSets { main { java.srcDirs = ['src/main/java', 'src/androidTest/java/'] } } }

If I remove that line, then the androidTest/java folder is no longer recognized as a javafolder, and I cannot add classes/packages there anymore. And in the Android Project View, the MainActivityTest class is no longer visible at all.

What am I missing, when it comes to adding the androidTest folders, for it to be recognized as a test folder structure?

Is there something I need to add to the gradles file?

android {
    compileSdkVersion 21
    buildToolsVersion '22.0.1'

    defaultConfig {
        applicationId "com.ourcompany.ourapp"
        minSdkVersion 15
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
        }
    }

    lintOptions {
        checkReleaseBuilds true
        abortOnError false
    }

    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:22.2.1'
    }

}

dependencies {
    compile 'com.android.support:support-v4:22.1.1'
    compile 'com.android.support:design:22.2.1'
    compile 'com.android.support:appcompat-v7:22.1.1'
    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.+"
}

android {
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
}

Aucun commentaire:

Enregistrer un commentaire