Android build tools 1.1.0, unit test folder? - android

I recently installed the latest tools from google to my android project:
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
allprojects {
repositories {
jcenter()
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
applicationId "com.xxx"
minSdkVersion 10
targetSdkVersion 21
versionCode 200
versionName "2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
debug {
...
}
release {
...
}
}
buildTypes {
release {
...
}
debug {
...
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
// ---- Tests with robolectric
testCompile 'com.google.guava:guava:14.0.1'
testCompile 'junit:junit:4.+'
testCompile 'org.robolectric:robolectric:2.4'
testCompile 'org.mockito:mockito-all:2.0.2-beta'
// ---- Tests with Espresso
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.0') {
exclude module: 'hamcrest-core'
}
androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
androidTestCompile ('com.android.support.test:testing-support-lib:0.1') {
exclude module: 'hamcrest-core'
}
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
androidTestCompile('junit:junit-dep:4.10') {
exclude module: 'hamcrest-core'
}
}
Where before that I used to use com.github.jcandksolutions.gradle:android-unit-test:2.1.1 to run my robolectric tests in the jvm.
As google says for their new build tools: "New source folders recognized as unit tests: src/test/java, src/testDebug/java, src/testMyFlavor/java etc."
But as you can see below, my test folder isn't recognised as a source folder. It worked with com.github.jcandksolutions.gradle:android-unit-test:2.1.1, but no more with the new build tools:
What I'm missing here? Thank you

I found the solution which is to switch between Test Artifacts in the left corner of the IDE. On this screen only "Android Instrumentation Tests" is available because I downgraded my android tools but with tools 1.1.0+ you should see different types of test to get the IDE recognize them as source folders.

I had a similar problem the other day, except that I was able to run Robolectric tests ok in Android Studio, but didn't work from the command line. What worked for me is the following.
1) Run ./gradlew clean assembleDebug test (instead of just clean test)
(now, it would find source from main packages, but I would get this problem instead)
2) Added this to the build.gradle file: android.sourceSets.test.java.srcDirs += "build/generated/source/r/debug"

Just follow http://tools.android.com/tech-docs/unit-testing-support to enable the experimental unit test and switch test artifact to unit tests. then your unit test folder will be recognised.
when you have more issues perhaps this will help http://nenick-android.blogspot.de/2015/02/android-studio-110-beta-4-and.html

If it's any use I set up a boiler plate project allowing the use of Unit tests and Espresso tests by the use of switching build variants. You won't need the use of any third party plugins with this.
https://github.com/hitherejoe/Android-Boilerplate

Related

AAR / JAR not get generated with Android Studio 3.0 Canary 7

So, the title pretty much sums up my problem. Anyone know how to fix it? Last time I build apk, it work fine on AS 2.X, but, I'm now developing with Kotlin so, I have to use AS 3.0.
I use AS 3.0 Canary 7 (This project is a fresh project)
This is my dummy class in my library (Stored in "domain".library.mylibrary)
class Test {
fun hello(): String {
return "hello"
}
}
And this is my (mylibrary) gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:26.0.0-beta2'
testImplementation 'junit:junit:4.12'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" //Declared in project level gradle: ext.kotlin_version = '1.1.3-2'
}
My gradlew.bat clean build --info log: https://pastebin.com/bfTZ5s4e
Android Studio does not generate a jar by default, but rather an AAR file. If you want to get a jar output, you need to implement apply plugin: 'maven-publish' on top of your mylibrary/gradle.build file and read the following document: Chapter 36. Maven Publishing (new)
Another note, is that I would suggest you not to use ProGuard directly on your release artifact, but rather provide a consumer ProGuard file and let the users of your library to handle that task, otherwise they will have a hard time referencing obfuscated code. Read my answer on this here
While the build task may generate an output, it is known that the assemble task (or assembleRelease) is the one that will generate your aar output.
Try running the following command:
./gradlew clean assembleRelease
and check the output in /mylibrary/build/outputs/apk/release/ if I am not mistaken.

How to solve duplication error when loading both espresso web and core in build.gradle?

I'm trying to use Android Espresso Web for my automation test.
After adding it to the build.gradle file like shown below, I get a "DuplicateFileException" exception.
According to the API, both of the dependancies should be present in the build.gradle file so I don't understand why I get a duplication exception and how to solve it.
Error:Execution failed for task ':CompanyAAA:transformResourcesWithMergeJavaResForStagingDebugAndroidTest'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/maven/com.google.guava/guava/pom.properties
File1: /Users/mayabechler-speicher/CompanyAAA-Android/CompanyAAA/build/intermediates/exploded-aar/com.android.support.test.espresso/espresso-web/2.2.2/jars/classes.jar
File2: /Users/mayabechler-speicher/CompanyAAA-Android/CompanyAAA/build/intermediates/exploded-aar/com.android.support.test.espresso/espresso-core/2.2.2/jars/classes.jar
[4:24]
// Espresso core
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
// Espresso web
androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2'
Any idea how to solve this error?
According to Google's Android Testing Support Library Examples, you don't need to declare both, if your existing app depends on webviews.
Check this configuration:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion rootProject.buildToolsVersion
defaultConfig {
applicationId "com.example.android.testing.espresso.web.BasicSample"
minSdkVersion 10
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
lintOptions {
abortOnError false
}
productFlavors {
}
packagingOptions {
exclude 'META-INF/maven/com.google.guava/guava/pom.properties'
exclude 'META-INF/maven/com.google.guava/guava/pom.xml'
}
}
dependencies {
// App dependencies
compile 'com.android.support:support-annotations:' + rootProject.supportLibVersion;
compile 'com.google.guava:guava:18.0'
// Testing-only dependencies
// Force usage of support annotations in the test app, since it is internally used by the runner module.
androidTestCompile 'com.android.support:support-annotations:' + rootProject.supportLibVersion;
androidTestCompile 'com.android.support.test:runner:' + rootProject.runnerVersion;
androidTestCompile 'com.android.support.test:rules:' + rootProject.rulesVersion;
androidTestCompile 'com.android.support.test.espresso:espresso-web:' + rootProject.espressoVersion;
}
From: https://github.com/googlesamples/android-testing/blob/master/ui/espresso/WebBasicSample/app/build.gradle
If you need both, open your build.gradle file and add:
packagingOptions {
exclude 'META-INF/maven/com.google.guava/guava/pom.properties'
exclude 'META-INF/maven/com.google.guava/guava/pom.xml'
}
It should work

Could not find support-annotations.jar

I'm trying to follow the instructions on android testing step 7
https://codelabs.developers.google.com/codelabs/android-testing/#6
cloned the repo: git clone https://github.com/googlecodelabs/android-testing.git -b step-7
Even though I remove every reference to support-annotations, gradle gives the same error and stops:
Error:A problem occurred configuring project ':app'.
> Could not find support-annotations.jar (com.android.support:support-annotations:23.1.1).
Searched in the following locations: file:/C:/Users/UserName/Documents/android_studio/sdk/extras/android/m2repository/com/android/support/support-annotations/23.1.1/support-annotations-23.1.1.jar
build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "com.example.android.testing.notes"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
debug {
// Run code coverage reports by default on debug builds.
// testCoverageEnabled = true
}
}
// If you need to add more flavors, consider using flavor dimensions.
productFlavors {
mock {
applicationIdSuffix = ".mock"
}
prod {
}
}
// Remove mockRelease as it's not needed.
android.variantFilter { variant ->
if(variant.buildType.name.equals('release')
&& variant.getFlavors().get(0).name.equals('mock')) {
variant.setIgnore(true);
}
}
// Always show the result of every unit test, even if it passes.
testOptions.unitTests.all {
testLogging {
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
}
}
}
/*
Dependency versions are defined in the top level build.gradle file. This helps keeping track of
all versions in a single place. This improves readability and helps managing project complexity.
*/
dependencies {
// App's dependencies, including test
compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:design:$rootProject.supportLibraryVersion"
compile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:support-v4:$rootProject.supportLibraryVersion"
compile "com.google.guava:guava:$rootProject.guavaVersion"
compile "com.github.bumptech.glide:glide:$rootProject.glideVersion"
compile "com.android.support.test.espresso:espresso-idling-resource:$rootProject.ext.espressoVersion"
// Dependencies for local unit tests
testCompile "junit:junit:$rootProject.ext.junitVersion"
testCompile "org.mockito:mockito-all:$rootProject.ext.mockitoVersion"
testCompile "org.hamcrest:hamcrest-all:$rootProject.ext.hamcrestVersion"
testCompile "org.powermock:powermock-module-junit4:$rootProject.ext.powerMockito"
testCompile "org.powermock:powermock-api-mockito:$rootProject.ext.powerMockito"
// Android Testing Support Library's runner and rules
androidTestCompile "com.android.support.test:runner:$rootProject.ext.runnerVersion"
androidTestCompile "com.android.support.test:rules:$rootProject.ext.runnerVersion"
// Espresso UI Testing dependencies.
androidTestCompile "com.android.support.test.espresso:espresso-core:$rootProject.ext.espressoVersion"
androidTestCompile "com.android.support.test.espresso:espresso-contrib:$rootProject.ext.espressoVersion"
androidTestCompile "com.android.support.test.espresso:espresso-intents:$rootProject.ext.espressoVersion"
}
/*
Resolves dependency versions across test and production APKs, specifically, transitive
dependencies. This is required since Espresso internally has a dependency on support-annotations.
*/
configurations.all {
resolutionStrategy.force "com.android.support:support-annotations:$rootProject.supportLibraryVersion"
}
/*
All direct/transitive dependencies shared between your test and production APKs need to be
excluded from the test APK! This is necessary because both APKs will contain the same classes. Not
excluding these dependencies from your test configuration will result in an dex pre-verifier error
at runtime. More info in this tools bug: (https://code.google.com/p/android/issues/detail?id=192497)
*/
configurations.compile.dependencies.each { compileDependency ->
println "Excluding compile dependency: ${compileDependency.getName()}"
configurations.androidTestCompile.dependencies.each { androidTestCompileDependency ->
configurations.androidTestCompile.exclude module: "${compileDependency.getName()}"
}
}
Try to install the dependencies:
echo y | android update sdk --no-ui --all --filter extra-google-google_play_services,extra-google-m2repository
gradle dependencies
update android support repository package

Gradle dependency causing error "Invalid Magic Number"

I have a project on GitHub that I work on both in the office at home. For about 2 months it was working fine on both machines. Then two weeks ago, it stopped running on my home PC, but still works fine on my work PC.
This is the error I get:
:app:shrinkDebugMultiDexComponents FAILED
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:shrinkDebugMultiDexComponents'.
java.io.IOException: Can't read [D:\dev\gitRepo\app\android\app\build\intermediates\multi-dex\debug\allclasses.jar] (Can't process class [__MACOSX/com/stripe/android/._BuildConfig.class] (Invalid magic number [51607] in class))
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
The stripe package that is giving me errors is a 3rd party library that you can find here. I list it as a dependency in my build.gradle file exactly as they say to.
compile 'com.stripe:stripe-android:+'
I have commented out all code pertaining the stripe and the app runs completely fine, so I do know it has something to do with how I am handling that package.
Unfortunately I don't remember exactly what I had done to make this stop working. I do think that the week before this happened I upgraded Android Studio, and spent a considerable amount of time messing with ProGuard configurations.
What I have tried:
Working on the master branch where no ProGuard changes have been made.
Uninstalling and Reinstalling Android Studio
Re-cloning the git repo
Installing API 17 (stripe for eclipse requires this. Not Studio but I gave it a shot).
Contacting stripe customer support but they had no clue.
This stack overflow post. However, there is no Mac computer that has touched the project nor have I personally zipped anything related to stripe.
From here, converted the magic number from Hex to ASCII. The result was Q` which I do not recognize.
I think it may have something to do with something I did for ProGuard, but I don't understand how. I'm on a completely different branch than any Proguard work, with a clean AndroidStudio install, with a clean repository clone, and the project still works fine when I'm in the office.
EDIT
I am running this on the debug BuildType. These are my 3 gradle files. The first is for the entire Project, the second is for the Application Module, and the third is for a local android library Module.
Project build.gradle:
buildscript {
repositories {
jcenter()
maven { url 'http://download.crashlytics.com/maven' }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
}
}
allprojects {
repositories {
jcenter()
maven{ url 'http://download.crashlytics.com/maven' }
}
}
Android Application Module build.gradle
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.app.android"
renderscriptTargetApi 19
renderscriptSupportModeEnabled true
minSdkVersion 16
targetSdkVersion 22
multiDexEnabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
buildConfigField 'Boolean', 'enableCrashlytics', 'true'
}
debug {
buildConfigField 'Boolean', 'enableCrashlytics', 'false'
}
adhoc {
debuggable true
signingConfig signingConfigs.debug
buildConfigField 'Boolean', 'enableCrashlytics', 'true'
}
}
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'
}
}
dependencies {
compile project(':localLibrary')
compile 'com.facebook.android:facebook-android-sdk:3.21.1'
compile 'commons-io:commons-io:2.4'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.android.support:support-v4:22.0.1'
compile 'com.google.android.gms:play-services-identity:8.1.0'
compile 'com.google.android.gms:play-services-plus:8.1.0'
compile 'com.google.android.gms:play-services-maps:8.1.0'
compile 'com.android.support:multidex:1.0.1'
compile 'io.card:android-sdk:5.0.1'
compile 'com.stripe:stripe-android:+'
compile('com.crashlytics.sdk.android:crashlytics:2.5.2#aar') {
transitive = true;
}
}
Local Android Library Module build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 22
multiDexEnabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:22.1.0'
compile 'com.google.code.gson:gson:2.2.2'
compile 'com.android.support:multidex:1.0.0'
compile group: 'org.apache.httpcomponents' , name: 'httpmime' , version: '4.3.5'
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5'
}
This problem is probably related to the version of Java that you are running. I had a similar problem and discovered that Java 8 was used for the build. When I changed to Java 7, this build problem was fixed.
In Android Studio go to
File -> Project Structure -> SDK Location
The JDK Location should be Java 1.7.x (Java 7)
Yeah verify both machines have the same Java version, and indeed the same version of Android Studio. The only other thing I can think of is that maybe Stripe is using the build tools bundled with AS to build your apk, and the ones you have installed by default differ slightly and are missing something - see this post I made on a similar problem I had.

Jacoco code coverage for android application using mockito library

My android application uses the following config:
Gradle - 0.12.+
Contents of build.gradle file
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
repositories {
mavenLocal()
mavenCentral()
}
apply plugin: 'com.android.library'
apply plugin: "jacoco"
dependencies {
compile 'commons-collections:commons-collections:3.2.1'
compile 'org.slf4j:slf4j-android:1.6.1-RC1'
// dependency injection
compile('org.roboguice:roboguice:2.0') {
exclude module: 'cglib'
exclude module: 'aopalliance'
exclude module: 'guice'
}
compile files('libs/guice-3.0-no_aop.jar')
compile 'javax.inject:javax.inject:1'
/*
* Test dependencies.
*/
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0'
}
android {
buildToolsVersion "20.0"
compileSdkVersion 19
buildTypes {
debug {
runProguard false
testCoverageEnabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 100
versionName "1.0.0"
}
/*
* Workaround for packaging bug in Android Gradle plugin regarding duplicate files.
*/
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'LICENSE.txt'
}
}
All my tests run successfully when property testCoverageEnabled is set to false. On setting it to true, the following exception is thrown when running the tests
Caused by: java.lang.VerifyError: *** Some class ***
at dalvik.system.DexFile.defineClass(Native Method)
at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:211)
at dalvik.system.DexPathList.findClass(DexPathList.java:313)
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:51)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
The error happens on the line when mocks are initialised within the tests.
Has anyone managed to generate code coverage metrics for android application which uses mockito library for testing?
The following link was very useful in explaining the problem I encountered: http://www.androidpuzzles.com/168_17620080/
I subsequently switched the source and target compatibility settings to Java 1.5 and I was able to run the unit and UI tests (which used both mockito and espresso) and generate code coverage report using Jacoco.
If I had to retain Java 1.7 settings, the workaround would have been to change the scope of private methods in the class being tested to either protected or public scope. This would have then allowed me to generate the code coverage report (overcoming the issue as identified in the link included).
Jacoco coverage support has been added to the gradle plugin since 0.10.0. See http://tools.android.com/tech-docs/new-build-system.
Not quite sure I understand why you have such a complex gradle file.
You need to compile against Java 1.5 version.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_5
targetCompatibility JavaVersion.VERSION_1_5
}
https://code.google.com/p/android/issues/detail?id=69174
This issue has been fixed in build tools version 21+
android {
buildToolsVersion "21.1.2"
compileSdkVersion 19
...
}
Upgrade build tools to 21+ to retain Java 1.7 compatibility.

Categories

Resources