I am trying to access a raw resource called test.txt located in androidTest/res/raw. I have setup my project according to the documentation found here.
My project structure:
src
main
java
res
androidTest
java
res
raw
test.txt
Here is my test class:
package something.traveltracker;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import java.io.InputStream;
import something.traveltracker.models.RuterStop;
public class RuterStopAndroidTest extends InstrumentationTestCase {
#SmallTest
public void testCreateRuterStopFromJSON() {
InputStream is = getInstrumentation().getTargetContext().getResources().openRawResource(R.raw.test);
RuterStop ruterStop = RuterStop.createFromJSON(is);
assertSomething...
}
}
The problem is that R.raw. only lists up the resources in the main/res/raw folder. I read somewhere that I had to add .test when importing R;
import something.traveltracker.test.R
However this is not working for me. I am using Android studio 1.0.2 with gradle 2.2.1.
Heres my androidManifest.xml
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "something.traveltracker"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
}
Question:
How can I access this test resource? I need the resource only when running this unit test, and I dont want it to be included in the final apk.
EDIT: I am now able to import something.traveltracker.test.R in my class by adding the following line to my build.gradle file:
sourceSets {
androidTest.res.srcDirs = ["src/androidTest/res"]
}
This is quite an old post, but since there are no answers and in case someone like me wonders how to do this:
Create the folder src/androidTest/resources
Inside the folder, add your resources under the package of the test where you will load them (so if accessing from com.test.MyTest, put the files in src/androidTest/resources/com/test/test.txt)
Load the file using getClass().getResourceAsStream('test.txt')
Related
I'm just starting to play around with Android and I'm having trouble with some androidTest examples that I've come across. Basically, I get a Cannot resolve symbol 'test' error message from Android Studio in the import statements for the InstrumentationRegistry and AndroidJUnit4 classes of my instrumented test file:
ExampleInstrumentedTest.java
package com.example.androidtestexample;
import android.content.Context;
import android.support.test.platform.app.InstrumentationRegistry;
import android.support.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
#RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
#Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.example.androidtestexample", appContext.getPackageName());
}
}
As a consequence, the AndroidJUnit4 symbol of the #RunWith statement can't be resolved either and when I try to run the instrumentation test I get an 'Edit configuration' window with an 'Instrumentation runner class not specified' error.
I'm using Android Studio 3.5.3, but this example uses 'com.android.tools.build:gradle:2.2.3'. The app gradle.build is as follows (I had to add the aaptOptions statements as suggested here to get the minimum reproducible example working):
build.gradle (Module: app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.example.androidtestexample"
minSdkVersion 22
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit:4.12'
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
As suggested in this question, I have double-checked that the debug build variant was selected and that the instrumentation test source file was in the src/androidTest/java/ folder.
I understand that this example is quite outdated and, from another of the answers to the aforementioned question and from this page, that the android.support.test package is deprecated and AndroidX should be used instead. However, from this page I also understand that it should still be possible to use the Android Support library for historical artifacts, hence my question:
Is there a way for me to fix this problem and run such old examples with the android.support.test package without needing to migrate them to AndroidX?
From one day to the other I opened the Android project that I'm currently working on and I got this gradle error, also happens when I try to create a new project:
Gradle sync failed: Illegal UTF8 string in constant pool in class file com/android/build/gradle/tasks/RenderscriptCompile$_ConfigAction_execute_closure7
Also I noticed that the arrangement of all the app packages has changed to this configuration and it is not able to import AppCompactActivity:
Error print screen
I had been trying to find something on the web but so far I found nothing related to this specific case. I tried a few things myself but I think it only made it even worse..
Anyone out there that can help me?
Thanks!
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "my.app.*****.musicpad"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
}
For anyone facing this error i have solved it by deleting the temp files.
Control panel-->Java-->Settings-->delete files and restore defaults.
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import code.studio.v.R;
import code.studio.v.volley.RequestQueue;
Android studio is showing an error . Can not resolve symbol volley. I have cloned volley and added it as a module. My package name is code.studio.v
The following is my build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "code.studio.v"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}
the following is settings.gradle file .
include ':app', ':volley'
remove module dependencies and add library dependencies..using following steps
Right Click on project - Module Setting
Go to dependencies- click plus(+) symbol
type volley and enter.. select the4thfloor dependencies
Finally sync gradle and run
If you have imported volley library as a separate module then you have to add dependency compile project(':yourVolleyModule') into your app's build.gradle. Now, Sync your gradle and i hope it will work
I'm having issues when I try to use Bindroid in my Android Studio Project. I've set up a boilerplate app to create a HelloWorld example using Bindroid. I cloned the Bindroid source into my /libs folder. But when I go to run my app, I get a package Bindroid does not exist error.
I need to know how to import this code and use it with my project. For example, where should I place this code? How do I configure my project to use it? Here's my project structure:
EDIT
So I've added Bindroid according to the instructions, here is the resulting structure:
And here is my apps build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "com.sg.spencergardner.finance5"
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile project(':Bindroid:Bindroid')
}
And my project's settings.gradle:
include ':app'
include ":Bindroid"
You need to copy all the contents of this https://github.com/depoll/bindroid/tree/master/Bindroid under the folder name "Bindroid".
In your app's build.gradle, add.
compile project(':Bindroid:Bindroid')
:Bindroid:Bindroid corresponds to "Project Folder":"App/Library Folder". See https://github.com/depoll/bindroid/blob/master/BindroidSample/build.gradle#L5.
Also in your settings.gradle, you need to add ":app" and ":Bindroid".
I'm trying to configure a android app to run tests with robolectric.
I'm new to gradle, and I can't find out what the problem is.
When I run ./gradlew robolectric on the command line, I get a cannot find symbol error.
The error I get is:
MainActivityTest.java:18: error: cannot find symbol
Robolectric.buildActivity(MainActivity.class).get().getResources();
^
symbol: class MainActivity
location: class MainActivityTest
This is my build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'robolectric'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.app.id"
minSdkVersion 16
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
sourceSets{
main.setRoot('src/main')
androidTest.setRoot('src/test')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile 'org.robolectric:robolectric:2.3'
androidTestCompile 'junit:junit:4.11'
}
So, the problem seems to be that I cannot access the source file when I run the tests, but I couldn't find a way to fix it.
Can anyone tell me what i'm doing wrong?
Thanks!
main.setRoot('src/main') => Is it really a main folder / package in your project?
You can test again by using the fully file directory, ex: src/helloword/mainActivity.java