I recently decided to switch from Eclipse to Android Studio. While i was able to import my android project, i keep having problems setting up and transfering my unit tests. For testing purposes i made a directory in src folder (java folder and test package). For enabling robotium i followed another stack topic by adding androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.2.1'. Eventually i added my test java file from Eclipse. And when i try to run tests, i keep getting "Cannot resolve symbol Solo".
I have little understanding about how AS and gradle works, so probably i am missing something else.
The contents of my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.colormindapps.work_rest__scheduler"
minSdkVersion 8
targetSdkVersion 21
testApplicationId "com.colormindapps.work_rest__scheduler.test"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets {
main {
java.srcDirs = ['src/main/java', 'src/tests/java']
}
}
}
dependencies {
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.2.1'
compile 'com.android.support:support-v4:19.0.+'
compile 'com.android.support:appcompat-v7:19.0.+'
}
I think the easiest way to make it work is to install Robotium Recorder for Android Studio. After you record a test you can see how Robotium Recorder sets up the gradle files etc.
http://robotium.com/pages/installation-android-studio
You must use separate source set for you android-specific tests, i.e.:
sourceSets {
main {
java.srcDirs = ['src/main/java']
}
androidTest {
java.srcDirs = ['src/tests/java']
}
}
Use androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.5.4' which resolve all test cases issues
Related
How do you debug JUnit test case in Android studio? I placed a breakpoint and used "Debug Test case" but it just runs the code and the breakpoint is ignored.
Update:
This is content of my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.****.mancala"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
}
Depending on the Run/Debug Configuration used, the same code with the same breakpoint will stop or not.
For a configuration with Test kind: All in package, the execution
will not stop at breakpoint
For a configuration with Test kind: Class or Test kind: Method, the execution will stop at breakpoint
For a configuration with Test kind: All in directory with Directory: [myProjectPath]/app/src/test, the execution will stop at breakpoint
Make sure that in your build.gradle file (one with all your dependencies) that you have debuggable true within the debug build type.
Also just for good measure make sure its a valid breakpoint (not an empty line etc.)
I use compile project(':pickphotos') to include a library named pickphotos.
The R in pickphotos is red though I have me.crosswall.photo.pick.R.I can build and run the app normally.
When I use Alt+Enter , it shows some solution:
When I use Move 'R' in pickphotos, it shows some R:
But if I pick me.crosswall.photo.pick.R,I will get repeat R error in my next build.
Below is the build.gradle of pickphotos:
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
dependencies {
}
I can also find it app/build/generated/source/r:
There is no Mark as item in menu in Android Studio:
I finally resloved it.
Just Make Project.
R is created when Make Project.
I think when we Run Project or Build Project,Android Studio doesn't Make Project for some reason.
Going to Build > Rebuild Project and/or Clean Project seems to be the goto first attempt at fixing any mindboggling red errors. Better to do that then spend 45 minutes staring at code trying to figure out what's wrong when the answer turns out to be 'nothing'...
A few months ago, I decided to take some custom methods and classes that I was using in more than one project and put them in their own project called "FarmSoftLibraries" that all of my projects could reference. This allowed me to make changes or additions once. It has been working very well up until yesterday (8/23/2015) when i started getting an odd error on a new method that Gradle could not find. However Android Studio had no problem finding this method. See: https://stackoverflow.com/questions/32167734/cannot-find-symbol-method-for-custom-static-class-method. However older, non-new methods continued to work.
Today the entire build system stopped working and I'm getting hundreds of Gradle errors saying that packages and methods in FarmSoftLibraries do not exist. Still, Android Studio has no problem locating these.
D:\Scott\Android\Studio\SpellingTutor\app\src\main\java\com\farmsoft\spellingtutor\Learn.java:12: error: package com.farmsoft.farmsoftlibraries.Utils does not exist
import com.farmsoft.farmsoftlibraries.Utils.FarmUtils;
^
D:\Scott\Android\Studio\SpellingTutor\app\src\main\java\com\farmsoft\spellingtutor\Learn.java:14: error: package com.farmsoft.farmsoftlibraries.Utils does not exist
import com.farmsoft.farmsoftlibraries.Utils.Logg;
^
D:\Scott\Android\Studio\SpellingTutor\app\src\main\java\com\farmsoft\spellingtutor\utils\KeyValueDB.java:6: error: package com.farmsoft.farmsoftlibraries.Utils does not exist
import com.farmsoft.farmsoftlibraries.Utils.CsvUtil;
etc. etc. etc...
I want to stress that I've changed NOTHING in the gradle files. I have been upgrading Android Studio on the Canary path - I'm on 1.4 Preview 3 now. When this issue began I was on 1.4 Preview 2. I have no idea if this is related to this version of Studio.
My gradle scripts:
project settings.gradle:
include ':app'
include ':farmsoftlibs'
project(':farmsoftlibs').projectDir = new File('../FarmSoftLibraries/app')
app build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.farmsoft.spellingtutor"
minSdkVersion 15
targetSdkVersion 22
versionCode 28
versionName "1.15"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
applicationIdSuffix '.debug'
versionNameSuffix '.debug'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.android.gms:play-services-gcm:7.8.0'
compile project(':farmsoftlibs')
}
farmsoftlibs build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/gson-2.3.1.jar')
compile 'com.google.android.gms:play-services-vision:7.8.0'
}
I do want to admit I pretty much am ignorant about gradle - I follow the instructions for how to set it up in Android Studio, but I really don't know what I'm doing and have gotten lost in the Gradle documentation I tried to read. So this is incredibly frustrating and more so knowing that this is probably due to some stupid mistake I've made out of ignorance.
EDIT:
This seems to have something to do with ProGuard - if I change the library's release settings to say minifyEnabled false, then the error goes away... for now. I'd like to know why.
Information
So I have this project structure that looks like this:
Project root:
| Resources-module (uses the packagename: com.derk.application.resources)
-+ src
---+ main
-----+ res
| Core-module (uses the packagename: com.derk.application)
-+ src
---+ main
-----+ src
| Brand-module (uses whatever packagename of the customer domain)
Resources-module contains the res files.
Core contains a git-module that contains code for a code-base we keep updated.
Brand contains build.gradle where I setup the packagename of our customer and bind the main/res directory to the resource-module main/res, and then the main/src to the core-module main/src folder.
Like this:
android.sourceSets.main {
manifest.srcFile "src/main/AndroidManifest.xml"
res.srcDirs = ["$rootDir/resources-module/src/main/res"]
java.srcDirs = ["$rootDir/core-module/src/main/java", "src/main/java"]
}
}
I do not wish to alter the Core-module sources under any circumstances without it being pushed up to the master repository, meaing I only make global changes/fixes for all projects that uses core-module. This is why i tried this structure out.
The sourcefiles in the core-module will loads the imports
import com.derk.application.resources.R;
import com.derk.application.resources.BuildConfig;
to handle the resource generated content from gradle/idea
Since Brand-module is due to having packagename changes, I have to use some sort of middlemodule that holds the R and BuildConfig for easy deployment, so that the core-sources indeed never have local modifications.
When i try to refresh gradle for the brand-module, i do not get any issues, and android studio seems to find the R.java and BuildConfig.java just fine in the com.derk.application.resources when I check out the linkage in Android Studio
HOWEVER
When I try to run Brand-module, i get:
"Execution failed for task ':core-module:compileReleaseJava'."
and it now instead shows me:
Error:(20, 39) error: package com.derk.application.resources does not exist
even thought I have added
dependencies {
compile project(':resources-module')
}
to the build.gradle of core-module.
So the question is:
How do I setup gradle to handle this kind of cross-module dependency?
Keep in mind, I do not wish to alter the packagename for the core-module imports for each new project I setup, because we get local changes made to a gitmodule that is used for several projects.
/.ps
Currently i can without problem run module resources-module and have the app running, with the static packagename i've chosen for it. But that is also the problem, I want to keep it static, and hence that is why i introduced the third module.
You do that in the wrong way. What you are trying to achive is implementing a normal library module (which you use for every customer). Then you can create a new Module from the type application which uses the shared module. In that case you don't need to mess around with the path of the shared module.
For the case that your shared module is in another directory than the project root you can use this settings.gradle:
include ':CustomerX', ':SharedModule'
project(':SharedModule').projectDir = new File('../../some/where/else')
If not you can omit the last line.
When you keep a old directory structure you should try using this build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 9
buildToolsVersion '21.0.1'
defaultConfig {
minSdkVersion 9
targetSdkVersion 21
}
sourceSets {
main {
assets.srcDirs = ['assets']
res.srcDirs = ['res']
aidl.srcDirs = ['src']
resources.srcDirs = ['src']
renderscript.srcDirs = ['src']
java.srcDirs = ['src']
manifest.srcFile 'AndroidManifest.xml'
}
}
}
dependencies {
// just as example
compile 'com.android.support:support-v4:21.0.2'
}
You customers build.gradle should look e.g. like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion '21.0.1'
defaultConfig {
minSdkVersion 9
targetSdkVersion 21
signingConfigs {
release {
storeFile file("your.keystore")
storePassword 'pwd1'
keyAlias "alias"
keyPassword 'pwd2'
}
}
buildTypes {
debug {
debuggable true
minifyEnabled false
shrinkResources false
}
release {
debuggable false
jniDebuggable false
signingConfig signingConfigs.release
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dependencies {
// dependencies of the main project
compile 'com.android.support:support-v4:21.0.0'
compile 'com.android.support:appcompat-v7:21.0.0'
compile project(':SharedModule')
}
So I've set up a gradle project with android and tried to get some tests to run. Unfortunately they don't seem to. It's possible that I'm missing something obvious but here goes...
I am running gradle 1.11 and as I understand the documentation that's the new folder (since 0.9 I believe?) that should be used for tests.
So I have my testclass ::
package se.coinhunter.multigradle.test;
import org.junit.Test;
import static org.junit.Assert.*;
public class HelloAndroidTests {
#Test
public void testHelper() {
assertEquals(1,1);
}
}
}
That lives in src/androidTest and here is my build.gradle:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':MultiGradleSubmodule')
}
This is a multi-project build and the submodule mentioned in the dependencies block is a plain java project that has its' own scource and unit tests running quite smoothly. I was able to specify that it should tell me when it runs its' tests and give me some feedback and that works fine. That was achieved for that project using
test {
testLogging {
events 'started', 'passed'
}
}
in its' build.gradle. I havn't come across anything like this for android projects. The whole project builds and runs, but I either can't get the tests to run, or they're running but I'm not getting any output.
You're using jUnit 4 (package name "org.junit" with #Test annotation). Android gradle only works with jUnit 3 (package name "junit.framework" with no annotations).
Android tests run in the Dalvik virtual machine on a device or emulator so your test class should also extend "AndroidTestCase" (or one of the other junit subclasses in Android - depending on what you're testing).
UPDATED: also add the following to default config:
testInstrumentationRunner "android.test.InstrumentationTestRunner"
testFunctionalTest true
Run the test using
gradle connectedCheck