Gradle doesn't include test dependencies - android

I'm trying to run my android tests with Gradle but it does not see my dependencies.
In my build.gradle I have these lines:
dependencies {
compile 'com.android.support:appcompat-v7:19.1.0'
compile 'com.android.support:support-v4:19.1.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
// Android annotations
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
// ORMLite
compile "com.j256.ormlite:ormlite-android:$ORMLiteVersion"
compile "com.j256.ormlite:ormlite-core:$ORMLiteVersion"
// Google Guava
compile 'com.google.guava:guava:16.0.1'
// Joda Time
compile 'joda-time:joda-time:2.3'
// Dependencies for the `testLocal` task, make sure to list all your global dependencies here as well
testLocalCompile 'junit:junit:4.11'
testLocalCompile 'com.google.android:android:4.1.1.4'
testLocalCompile 'com.android.support:support-v4:19.1.0'
testLocalCompile 'org.robolectric:robolectric:2.1.+'
// Android Studio doesn't recognize the `testLocal` task, so we define the same dependencies as above for instrumentTest
// which is Android Studio's test task
instrumentTestCompile 'junit:junit:4.11'
instrumentTestCompile 'com.google.android:android:4.1.1.4'
instrumentTestCompile 'com.android.support:support-v4:19.1.0'
instrumentTestCompile 'org.robolectric:robolectric:2.1.+'
}
But in my class: instrumentTest/java/.../Test I cannot import i.e. org.junit
Full build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
// replace with the current version of the Android plugin
classpath 'com.android.tools.build:gradle:0.9.2'
// the latest version of the android-apt plugin
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.1'
}
}
apply plugin: 'android'
apply plugin: 'android-apt'
repositories {
mavenCentral()
mavenLocal()
}
configurations {
apt
}
def AAVersion = '3.0.1'
def ORMLiteVersion = '4.46'
def mainPackage = 'pl.grzeslowski.transport'
def paidPackage = mainPackage + '.paid'
def freePackage = mainPackage + '.free'
def testPath = 'src/instrumentTest/'
sourceSets {
testLocal {
java.srcDir file(testPath + 'java')
resources.srcDir file(testPath + 'resources')
}
instrumentTest {
java.srcDir file(testPath + 'java')
resources.srcDir file(testPath + 'resources')
}
}
//configurations {
// testLocalCompile {
// extendsFrom compile
// }
//}
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
versionCode 1
versionName "1.0"
// tests
testPackageName "pl.grzeslowski.transport.tests"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
testHandleProfiling true
testFunctionalTest true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
signingConfigs {
debug {
storeFile file("../../debug.keystore")
}
}
buildTypes {
release {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
buildConfigField "java.lang.String", "DATABASE_NAME", "\"transporter\""
buildConfigField "int", "DATABASE_VERSION", "1"
buildConfigField "java.lang.String", "PAID_PACKAGE_NAME", "\"$paidPackage\""
buildConfigField "java.lang.String", "FREE_PACKAGE_NAME", "\"$freePackage\""
}
debug.initWith(buildTypes.release)
debug {
debuggable true
runProguard false
signingConfig signingConfigs.debug
}
}
productFlavors {
free {
packageName freePackage
buildConfigField "pl.grzeslowski.transport.product_flavors.MonetizationType", "MONETIAZATION_TYPE", "pl.grzeslowski.transport.product_flavors.MonetizationType.FREE"
}
paid {
packageName paidPackage
buildConfigField "pl.grzeslowski.transport.product_flavors.MonetizationType", "MONETIAZATION_TYPE", "pl.grzeslowski.transport.product_flavors.MonetizationType.PAID"
}
}
// tell Android studio that the instrumentTest source set is located in the unit test source set
sourceSets {
instrumentTest.setRoot(testPath)
main.java.srcDirs += testPath + 'java'
}
}
dependencies {
compile 'com.android.support:appcompat-v7:19.1.0'
compile 'com.android.support:support-v4:19.1.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
// Android annotations
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
// ORMLite
compile "com.j256.ormlite:ormlite-android:$ORMLiteVersion"
compile "com.j256.ormlite:ormlite-core:$ORMLiteVersion"
// Google Guava
compile 'com.google.guava:guava:16.0.1'
// Joda Time
compile 'joda-time:joda-time:2.3'
// Dependencies for the `testLocal` task, make sure to list all your global dependencies here as well
testLocalCompile 'junit:junit:4.11'
testLocalCompile 'com.google.android:android:4.1.1.4'
testLocalCompile 'com.android.support:support-v4:19.1.0'
testLocalCompile 'org.robolectric:robolectric:2.1.+'
// Android Studio doesn't recognize the `testLocal` task, so we define the same dependencies as above for instrumentTest
// which is Android Studio's test task
instrumentTestCompile 'junit:junit:4.11'
instrumentTestCompile 'com.google.android:android:4.1.1.4'
instrumentTestCompile 'com.android.support:support-v4:19.1.0'
instrumentTestCompile 'org.robolectric:robolectric:2.1.+'
}
def getSourceSetName(variant) {
return new File(variant.dirName).getName();
}
android.applicationVariants.all { variant ->
def aptOutputDir = project.file("build/source/apt")
def aptOutput = new File(aptOutputDir, variant.dirName)
println "****************************"
println "variant: ${variant.name}"
println "manifest: ${variant.processResources.manifestFile}"
println "aptOutput: ${aptOutput}"
println "****************************"
android.sourceSets[getSourceSetName(variant)].java.srcDirs += aptOutput.getPath()
variant.javaCompile.options.compilerArgs += [
'-processorpath', configurations.apt.getAsPath(),
'-AandroidManifestFile=' + variant.processResources.manifestFile,
'-AresourcePackageName=' + mainPackage,
'-s', aptOutput
]
variant.javaCompile.source = variant.javaCompile.source.filter { p ->
return !p.getPath().startsWith(aptOutputDir.getPath())
}
variant.javaCompile.doFirst {
aptOutput.mkdirs()
}
}
task localTest(type: Test, dependsOn: assemble) {
testClassesDir = sourceSets.testLocal.output.classesDir
android.sourceSets.main.java.srcDirs.each { dir ->
def buildDir = dir.getAbsolutePath().replaceAll(/\\+/, '/').split('/')
buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')
sourceSets.testLocal.compileClasspath += files(buildDir)
sourceSets.testLocal.runtimeClasspath += files(buildDir)
}
classpath = sourceSets.testLocal.runtimeClasspath
}
check.dependsOn localTest
assembleDebug.finalizedBy testLocalClasses

In version 0.9.+ of the Android Gradle Plugin
instrumentTestCompile
was changed to
androidTestCompile
Check out this simple template if you need to see a build.gradle file: Deckard (for Gradle)

Related

Could not find method compile() for arguments [directory 'libs']

i imported a project into a workspace in Android studio, and the file structure of the project does not seem in a proper condition as shown in image-1.
Also, when I tried to build and compile the project I received the following error:
Error:(20, 0) Could not find method compile() for arguments [directory 'libs'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Open File
also in the project,the build.gradle(project) exists twice and build.gradle(app) exists once as shown in image-2
please have a look at the below gradle files, and please let me know how to correct this error and why I am getting it.
build.gradle(app):
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '3.2' // '4.2.0'
dependencies {
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
}
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
resourcePackageName 'eu.man.m24wsapp'
}
}
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "eu.man.m24wsapp"
targetSdkVersion 25
minSdkVersion 10
versionCode 1
versionName "1.1.18"
}
signingConfigs {
release {
storeFile file("../wsapp.keystore")
storePassword "7s5zhgknsIXxHgw"
keyAlias "man_mobile24"
keyPassword "manmobile242014"
}
}
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
buildTypes {
release {
minifyEnabled false // 15.05.2017 avk, enables/disables proguard
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
applicationVariants.all { variant ->
variant.outputs.each { output ->
def name = output.outputFile.name.replace("app", "Man");
name = name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk");
output.outputFile = new File(output.outputFile.parent, name)
}
}
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':appcompat')
compile project(':securepreferences')
compile 'com.android.support:support-v4:25.2.0' // .0.1
compile 'com.google.android.gms:play-services:4.3.23'
// compile 'com.google.android.gms:play-services-maps:10.0.1'
// compile 'com.google.android.gms:play-services-location:10.0.1'
// AVK 05.02.2017 compile 'com.github.chrisbanes.photoview:library:1.2.2'
compile 'com.github.chrisbanes.photoview:library:1.2.4'
compile 'com.google.code.gson:gson:2.7' // 2.4
// AVK 5.2.2017 compile 'com.squareup:otto:1.3.6'
compile 'com.squareup:otto:1.3.8'
// AVK 5.2.2017 compile 'com.squareup.okhttp:okhttp:1.5.4'
// AVK 5.2.2017 compile 'com.squareup.okhttp:okhttp:2.5.0'
// AVK 5.2.2017 compile 'com.squareup.okhttp:okhttp:2.6.0' // oder
compile 'com.squareup.okhttp:okhttp:2.2.0' // 7.5' // oder
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0' // 7.5' // oder
// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
// AVK 5.2.2017 compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.6.0'
compile 'com.squareup.picasso:picasso:2.4.0'
//compile 'com.squareup.picasso:picasso:2.2.0'
// AVK 5.3.2017 compile 'com.squareup.picasso:picasso:2.4.0'
// AVK 3.2.2017 compile 'com.squareup.retrofit:retrofit:1.5.1'
// AVK 3.2.2017 compile group: 'com.squareup.retrofit', name: 'retrofit', version: '1.9.0'
compile 'com.squareup.retrofit:retrofit:1.8.0'
// AVK 5.2.2017 compile 'com.squareup.retrofit2:retrofit:2.1.0'
}
**build.gradle(project)**:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7' // 1.8?
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('app/libs/android-viewbadger.jar')
compile files('app/libs/iDappsImagesLib_v0.2.jar')
compile files('app/libs/iDappsToolsLib_v0.1.jar')
compile files('gradle/wrapper/gradle-wrapper.jar')
}
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
dexOptions {
incremental true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
image-1:
image-2:
image-3 project properties:
image-4 gradle path:
Move quoted block from build.gradle(project) to build.gradle(app) as the compile and android comes from the application plugin
build.gradle(project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7' // 1.8?
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
build.gradle(app)
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('app/libs/android-viewbadger.jar')
compile files('app/libs/iDappsImagesLib_v0.2.jar')
compile files('app/libs/iDappsToolsLib_v0.1.jar')
compile files('gradle/wrapper/gradle-wrapper.jar')
}
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
dexOptions {
incremental true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

Android studio sync gradle Error:Cannot invoke method add() on null object

i can't sync android project i add gradle code below. When i sync it's failed and add message: Error:Cannot invoke method add() on null object
apply plugin: 'com.android.application'. I tryed everything, but can't fix it
android {
def ci_pf = new File('ci.properties')
if (ci_pf.canRead()) {
def Properties ci_p = new Properties()
ci_p.load(new FileInputStream(ci_pf))
def bld = ci_p['ci_versionCode'].toInteger()
def bvn = ci_p['ci_versionName']
def csv = ci_p['ci_compileSdkVersion'].toInteger()
def btv = ci_p['ci_buildToolsVersion']
def msv = ci_p['ci_minSdkVersion'].toInteger()
def tsv = ci_p['ci_targetSdkVersion'].toInteger()
def cxf = ci_p['ci_cppFlags']
compileSdkVersion csv
buildToolsVersion btv
defaultConfig {
applicationId "com.nordcurrent.AdSystemTest"
minSdkVersion msv
targetSdkVersion tsv
versionCode bld
versionName bvn
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions -Wno-nonportable-include-path -DDEBUG -D_DEBUG " + cxf
cFlags "-D\"lua_getlocaledecpoint()='.'\" -DLUA_ANSI -DUSE_ANDROID"
arguments "-DANDROID_STL=c++_static", "-DANDROID_ARM_MODE=arm", "-DANDROID_ARM_NEON=TRUE"
}
}
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
abiFilters 'armeabi-v7a', 'x86'
jobs 32
}
}
compileSdkVersion = csv
buildToolsVersion = btv
}
buildTypes {
debug {
debuggable true
}
release {
minifyEnabled = false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')
}
}
}
android.productFlavors {
create("fat") {
ndk.abiFilters.add("armeabi-v7a")
ndk.abiFilters.add("x86")
}
}
android.sources {
main {
jni {
source {
srcDirs 'src/main/jni'
}
}
}
}
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile(name: 'Millennial', ext: 'aar')
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
compile 'com.google.android.gms:play-services-ads:10.2.0'
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:support-annotations:25.2.0'
/** Tune dependency */
compile 'com.android.support:support-v4:23.1.1'
compile 'de.greenrobot:eventbus:2.4.0'
androidTestCompile 'com.android.support:support-annotations:25.2.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
testCompile 'junit:junit:4.12'
compile 'junit:junit:4.12'
}

./Gradlew Build is taking 2 hours and 40 minutes - Is there any room for improvement?

The app that we are working on is not very large - most of it is an empty application that gets filled in from various endpoints (server side), but we currently have 18 separate flavors of the app that get built.
The issue is, there will be 78 flavors of the application built and if the build time is linear, we are looking at about 11 hours per ./gradlew build request.
Im not well versed in Gradle at all and I was hoping you guys could help me find some problem areas that might be able to improve our build times
Here is the build.gradle:
apply plugin: 'com.android.application'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
apply plugin: 'io.fabric'
apply from: '../jacoco.gradle'
apply from: '../flavors.gradle'
apply from: '../autoTasks.gradle'
ext {
applicationName = 'OurApp'
supportLibVersion = '25.3.0'
playServicesVersion = '9.6.1'
}
android {
signingConfigs {
debug {
keyAlias 'key'
keyPassword 'pass'
storeFile file('../file.keystore')
storePassword 'pass'
}
release {
keyAlias 'key'
keyPassword 'pass'
storeFile file('../file.jks')
storePassword 'pass'
}
}
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 25
vectorDrawables.useSupportLibrary true
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
multiDexEnabled true
}
dataBinding {
enabled true
}
lintOptions {
abortOnError false
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
buildConfigField 'String', 'DFP_NETWORK_ID', '"id"'
buildConfigField 'String', 'YOUTUBE_API_KEY', '"ourkey"'
}
debug {
signingConfig signingConfigs.debug
testCoverageEnabled = true
buildConfigField 'String', 'DFP_NETWORK_ID', '"id"'
buildConfigField 'String', 'YOUTUBE_API_KEY', '"anotherKey"'
}
debuggable.initWith(buildTypes.debug)
debuggable {
testCoverageEnabled = false
}
}
dexOptions {
javaMaxHeapSize "4g"
}
applicationVariants.all { variant ->
if (variant.buildType.name == 'release') {
createZipProguardTask(variant);
}
updateApkName(variant);
}
}
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
annotationProcessor 'com.bluelinelabs:logansquare-compiler:1.3.7'
annotationProcessor 'com.squareup:javapoet:1.8.0'
annotationProcessor 'com.google.dagger:dagger-compiler:2.7'
compile "com.android.support:appcompat-v7:${supportLibVersion}"
compile "com.android.support:design:${supportLibVersion}"
compile "com.android.support:recyclerview-v7:${supportLibVersion}"
compile "com.android.support:support-v13:${supportLibVersion}"
compile "com.android.support:percent:${supportLibVersion}"
compile "com.android.support:preference-v14:${supportLibVersion}"
compile "com.google.android.gms:play-services-gcm:${playServicesVersion}"
compile "com.google.android.gms:play-services-maps:${playServicesVersion}"
compile "com.google.firebase:firebase-core:${playServicesVersion}"
compile "com.google.firebase:firebase-messaging:${playServicesVersion}"
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.roughike:bottom-bar:2.2.0'
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.l4digital.rxloader:rxloader:2.0.1'
compile 'com.bluelinelabs:logansquare:1.3.7'
compile 'com.github.aurae.retrofit2:converter-logansquare:1.4.1'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
compile 'com.google.dagger:dagger:2.7'
compile 'com.facebook.stetho:stetho:1.4.2'
compile 'com.facebook.stetho:stetho-okhttp3:1.4.2'
compile('com.crashlytics.sdk.android:crashlytics:2.6.5#aar') {
transitive = true
}
compile(name:'googlemediaframework-release', ext:'aar')
compile "com.google.android.gms:play-services-ads:${playServicesVersion}"
compile "com.google.android.gms:play-services-analytics:${playServicesVersion}"
compile 'com.flurry.android:ads:6.2.0'
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.6.0'
compile 'com.google.android.exoplayer:exoplayer:r1.5.14'
compile 'com.android.support:multidex:1.0.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
debugCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
debuggableCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.squareup.okhttp3:okhttp:3.5.0'
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
androidTestCompile('com.android.support.test:rules:0.5') {
exclude module: 'support-annotations'
}
androidTestCompile (name:'cloudtestingscreenshotter_lib', ext:'aar')
androidTestCompile('com.android.support.test:runner:0.5') {
exclude module: 'support-annotations'
}
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
exclude module: 'support-annotations'
}
compile(name:'MapSDK', ext:'aar')
compile('com.twitter.sdk.android:twitter:2.3.2#aar') {
transitive = true;
}
}
def createZipProguardTask(variant) {
def prefix = project.ext.applicationName;
if (project.hasProperty('buildNumber')) {
prefix += '-' + project.getProperties().get('buildNumber');
}
def taskName = variant.flavorName.substring(0, 1).toUpperCase() + variant.flavorName.substring(1);
task("zipProguard${taskName}", type: Zip) {
archiveName = "${prefix}-${variant.flavorName}-proguard.zip";
from "build/outputs/mapping/${variant.flavorName}/release"
destinationDir file('build/outputs/mapping')
}
}
def updateApkName(variant) {
def prefix = project.ext.applicationName;
if (project.hasProperty('buildNumber')) {
prefix += '-' + project.getProperties().get('buildNumber');
}
variant.outputs.each { output ->
def fileName = output.outputFile.name.replace('app', prefix);
def outputFile = new File(output.outputFile.parent.toString(), fileName.toString());
output.outputFile = outputFile;
}
}
Here is what we have in the gradle properties:
org.gradle.jvmargs=-Xmx4608M -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.daemon=true
Any input would be greatly appreciated - like I said, I'm not well versed in gradle at all, so I'm open to anything.
Thank you in advance
I'm sure you figured something out by now, but just for future googlers -
Android Studio gradle takes too long to build improved the speed of my gradle build drastically. It suggests enabling Offline work in Android Studio by going to file>settings>build, execution, deployment>gradle and checking the "Offline work" option.

androidTestCompile not working in Android Studio 2.1.2

I followed the instruction here to add an instrumentation test, but unfortunately Android Studio complains about the classes from the test runner package (com.android.support.test:runner:0.5) or test rules or espresso-core (cannot find symbol). If I change the dependency type from androidTestCompile to compile, the errors disappear. I created an instrumentation run config, and currently that run config is selected.
EDIT:
Here is our Gradle build file:
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'realm-android'
apply plugin: 'pmd'
buildscript {
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
mavenCentral()
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.google.gms:google-services:2.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'me.tatarka:gradle-retrolambda:3.2.5'
classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
classpath 'io.realm:realm-gradle-plugin:1.0.0'
classpath 'io.fabric.tools:gradle:1.21.6'
}
configurations.classpath.exclude group: 'com.android.tools.external.lombok'
}
repositories {
jcenter()
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' }
maven { url 'https://repo.commonsware.com.s3.amazonaws.com' }
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
// compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:24.0.0'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.android.support:support-v13:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:percent:24.0.0'
compile 'com.google.android.gms:play-services-auth:9.0.2'
compile 'com.google.android.gms:play-services-gcm:9.0.2'
compile "com.mixpanel.android:mixpanel-android:4.8.6"
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.2.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
compile 'com.squareup:otto:1.3.8'
compile 'com.squareup.dagger:dagger:1.2.2'
apt 'com.squareup.dagger:dagger-compiler:1.2.2'
compile('com.facebook.android:facebook-android-sdk:4.13.1') {
exclude module: 'bolts-android'
exclude module: 'bolts-applinks'
exclude module: 'bolts-tasks'
}
compile 'com.facebook.fresco:fresco:0.11.0'
compile 'me.relex:photodraweeview:1.0.0'
compile 'com.jakewharton.rxrelay:rxrelay:1.0.0'
compile 'com.jakewharton:butterknife:8.1.0'
apt 'com.jakewharton:butterknife-compiler:8.1.0'
compile 'com.jakewharton.timber:timber:4.0.1'
compile 'javax.annotation:javax.annotation-api:1.2'
compile 'com.cloudinary:cloudinary-android:1.2.2:'
compile 'com.soundcloud.android:android-crop:1.0.1#aar'
compile 'com.rockerhieu.emojicon:library:1.3.3'
compile 'io.reactivex:rxandroid:1.2.0'
compile 'com.trello:rxlifecycle:0.5.0'
compile 'com.trello:rxlifecycle-components:0.5.0'
compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
compile 'com.jakewharton.rxbinding:rxbinding-recyclerview-v7:0.4.0'
compile 'com.jakewharton.rxbinding:rxbinding-support-v4:0.4.0'
compile 'com.jakewharton.rxbinding:rxbinding-design:0.4.0'
compile 'com.github.hotchemi:permissionsdispatcher:2.1.1'
apt 'com.github.hotchemi:permissionsdispatcher-processor:2.1.1'
compile 'net.sourceforge.streamsupport:streamsupport:1.4.3'
compile('com.crashlytics.sdk.android:crashlytics:2.5.5#aar') {
transitive = true;
}
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'junit:junit:4.12'
}
android {
// General configuration goes here
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
def buildNumber = computeVersionCode()
applicationId 'com.xxx.xxx'
minSdkVersion 19
targetSdkVersion 23
versionCode = buildNumber
versionName '0.2.6'
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
dexOptions {
incremental true
javaMaxHeapSize "6g"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
signingConfigs {
debug {
storeFile rootProject.file('debug.keystore')
storePassword 'xxx'
keyAlias 'xxx'
keyPassword 'xxx'
}
beta {
String password = getKeyStorePassword()
println("password::$password")
if (password) {
storeFile file("beta_release.keystore")
storePassword xxx
keyAlias "xxx"
keyPassword xxx
} else {
println "Environment variable BETA_KEYSTORE_PASSWORD needs to be set"
}
}
}
buildTypes {
localDebug {
minifyEnabled false
debuggable true;
signingConfig signingConfigs.debug
buildConfigField "String", "BACKEND_ADDRESS", "\"http://xxx.xxx.dev/\""
buildConfigField "boolean", "MYVAR2", "false"
buildConfigField "String", "MYVAR3", "\"value\""
resValue "string", "BUILD_TYPE_DISPLAY", "Debug(Local)"
applicationIdSuffix ".debug"
}
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'META-INF/services/javax.annotation.processing.Processor'
}
lintOptions {
abortOnError true
}
splits {
abi {
enable true
reset()
include 'x86', 'x86_64', 'arm64-v8a', 'armeabi-v7a', 'armeabi'
universalApk false
}
}
}
task pmd(type: Pmd) {
ruleSetFiles = files("${project.rootDir}/pmd-ruleset.xml")
ignoreFailures = false
ruleSets = []
source 'src'
include '**/*.java'
exclude '**/gen/**'
reports {
xml.enabled = false
html.enabled = true
xml {
destination "$project.buildDir/reports/pmd/pmd.xml"
}
html {
destination "$project.buildDir/reports/pmd/pmd.html"
}
}
}
def computeVersionCode() {
def buildNumber = System.getenv('BUILD_NUMBER')
if (buildNumber == null || buildNumber.isEmpty()) {
println "No BUILD_NUMBER in environment, using 1 as revision"
return 1
}
buildNumber = buildNumber.toInteger()
println "New revision is ${buildNumber}"
return buildNumber
}
configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:24.0.0'
force 'com.android.support:support-v4:24.0.0'
force 'com.android.support:appcompat-v7:24.0.0'
force 'com.android.support:design:24.0.0'
force 'com.android.support:recyclerview-v7:24.0.0'
}
}
I've spent about a day on this myself and finally figured it out. This is an Android Studio feature - termed a feature, but I'd consider it a bug.
To get instrumentation tests working, you need to set your Build Variants to the following:
Test Artifact: Android Instrumentation Tests
Build Variant: debug
See here for more details
I personally think is makes no sense; it's not like you're using androidTestCompileDebug, and running gradle <app_name>:dependencies repeatedly shows pulling in androidTestCompile dependencies, regardless of the build variant. But for some reason, they only resolve in debug.
I hope this helps.

Gradle execution failed for task

Once a while while building my project i keep getting:
Error:Gradle: Execution failed for task ':app:packageProductionDebug'.
> value (115422) > 0x0000ffff
This happens for 30%-50% attempts to run my project regardless of device.
I've tried to clean my project but still no luck.
The value (115422)seems to be changing but the hex value remains the same.
EDIT
My gradle file
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' }
}
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.myapp"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "0.1"
buildConfigField 'String', 'BUILD_DIR', "\"${project.buildDir}\""
}
dexOptions {
javaMaxHeapSize "4g"
}
testOptions {
unitTests.returnDefaultValues = true
}
/* set file name depending on build variant*/
applicationVariants.all { variant ->
variant.outputs.each { output ->
def filename = applicationId
if (variant.buildType.isDebuggable()) {
filename += '-debug';
} else {
filename += '-vc-' + variant.versionCode
}
filename += '.apk'
output.outputFile = new File(
output.outputFile.parent,
filename
)
}
}
productFlavors{
staging{
applicationId = 'com.myapp.beta'
manifestPlaceholders = [activityLabel:"myapp.STAGING"]
multiDexEnabled true
}
production {
applicationId = 'com.myapp'
manifestPlaceholders = [activityLabel:"myapp"]
multiDexEnabled true
}
instrTest {
applicationId = 'com.myapp'
manifestPlaceholders = [activityLabel:"myapp"]
multiDexEnabled true
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules-release.pro'
}
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules-debug.pro'
}
}
sourceSets {
main{
java.srcDirs = ['src/main/java']
assets.srcDirs = ['src/main/assets']
}
productionDebug{
assets.srcDirs = ['src/main/assets', 'src/test/assets']
}
stagingDebug{
assets.srcDirs = ['src/main/assets', 'src/test/assets']
}
staging {
java.srcDirs = ['src/staging/java']
}
production{
java.srcDirs = ['src/production/java']
}
instrTest{
assets.srcDirs = ['src/main/assets', 'src/test/assets']
java.srcDirs = ['src/instrTest/java']
manifest.srcFile 'src/instrTest/AndroidManifest.xml'
res.srcDirs = ['src/instrTest/','src/instrTest/res']
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:support-v4:23.+'
compile 'com.android.support:recyclerview-v7:23.+'
compile 'com.android.support:support-v4:23.+'
compile 'com.android.support:cardview-v7:23.+'
compile 'com.android.support:support-annotations:22.2.0'
compile 'com.android.support:design:22.2.1'
compile 'com.google.android.gms:play-services-analytics:8.1.0'
compile 'com.google.android.gms:play-services-gcm:8.1.0'
compile 'com.google.guava:guava:19.0-rc1'
compile 'com.urbanairship.android:urbanairship-sdk:6.3.+'
compile 'com.googlecode.libphonenumber:libphonenumber:7.0.10'
compile 'com.mcxiaoke.volley:library:1.0.18'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'biz.source_code:base64coder:2010-09-21'
compile 'org.iban4j:iban4j:3.0.4'
compile 'com.romainpiel.shimmer:library:1.4.0#aar'
compile 'org.apmem.tools:layouts:1.9#aar'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.tsums.androidcookiejar:androidcookiejar:1.0#aar'
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.7.1'
compile 'com.google.code.gson:gson:2.6.2'
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.android.support:multidex:1.0.1'
compile 'me.dm7.barcodescanner:zxing:1.8.4'
testCompile 'junit:junit:4.12'
testCompile 'org.easytesting:fest:1.0.16'
testCompile ('com.squareup:fest-android:1.0.8'){
exclude module: 'support-v4'
}
testCompile 'org.robolectric:robolectric:3.0'
testCompile "org.mockito:mockito-core:1.+"
compile project(':cropimage')
compile project(':viewpagerindicator')
compile('com.crashlytics.sdk.android:crashlytics:2.5.5#aar') {
transitive = true;
}
}
I've also tried to run the tasks from terminal with --stack option and got:
Caused by: java.lang.IllegalArgumentException: value (172858) > 0x0000ffff
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:148)
at com.android.builder.internal.packaging.zip.ZipField.write(ZipField.java:228)
at com.android.builder.internal.packaging.zip.StoredEntry.toHeaderData(StoredEntry.java:651)
at com.android.builder.internal.packaging.zip.ZFile.writeEntry(ZFile.java:944)
at com.android.builder.internal.packaging.zip.ZFile.update(ZFile.java:858)
at com.android.builder.internal.packaging.zip.ZFile.close(ZFile.java:900)
at com.android.builder.internal.packaging.zfile.ApkZFileCreator.close(ApkZFileCreator.java:128)
at com.google.common.io.Closer.close(Closer.java:214)
at com.android.builder.internal.packaging.IncrementalPackager.close(IncrementalPackager.java:343)
at com.google.common.io.Closer.close(Closer.java:214)
at com.android.build.gradle.tasks.PackageAndroidArtifact.doTask(PackageAndroidArtifact.java:448)
at com.android.build.gradle.tasks.PackageAndroidArtifact.doIncrementalTaskAction(PackageAndroidArtifact.java:580)
at com.android.build.gradle.tasks.PackageApplication.doIncrementalTaskAction(PackageApplication.java:82)
at com.android.build.gradle.internal.tasks.IncrementalTask.taskAction(IncrementalTask.java:108)
at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$IncrementalTaskAction.doExecute(AnnotationProcessingTaskFactory.java:244)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:220)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$IncrementalTaskAction.execute(AnnotationProcessingTaskFactory.java:231)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:209)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
... 14 more
I am not certain but I'm pretty sure it's a bug in Android Studio 2.2, and do not have enough reputation to make a comment. Killing Java(.exe) fixes it.

Categories

Resources