Android Studio project setup for Espresso tests - java

About a week ago I asked this question
Why is library module android.support.test not visible in add dependency
After much head scratching and project setup morphing I discovered that this app/build.gradle dependency configuration gets me so much further than ever before in that at least the project compiles but does not build when attempting to run:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion '22.0.1'
defaultConfig {
applicationId "com.my.package.name"
minSdkVersion 8
targetSdkVersion 22
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets { main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } }
packagingOptions {
exclude 'LICENSE.txt'
}
}
dependencies {
compile 'com.android.support:appcompat-v7:22.1.0'
compile 'com.android.support:support-v4:22.1.0'
compile 'com.android.support.test.espresso:espresso-core:2.1'
compile 'com.android.support.test:testing-support-lib:0.1'
}
Notice compile instead of androidTestCompile on the testing support dependencies. Using androidTestCompile results in the problems noted in my previous question identified above.
Also notice the two srcDirs in the sourceSets declaration. That declaration was automatically set when I manually created the androidTest/java directory. Should this be declared differently?
Also too, getDefaultProguardFile is underlined in the ide with the tool tip "cannot resolve symbol 'getDefaultProguardFile'". In case that is crucial.
This new revelation, switching androidTestCompile to compile, leads me to believe there is something wrong with the way I set up the project structure for the unit test. Here is a picture of my project structure:
I should mention that this project/module structure was automatically created when migrating from Eclipse. By the icon decorations it looks like it at least recognizes my test class as a test class.
But now I am stuck at this error:
Error:Execution failed for task ':mymodule:dexDebug'.
com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Users\myuser\AppData\Local\Android\sdk\build-tools\22.0.1\dx.bat --dex --no-optimize --output C:\Users\myuser\AndroidstudioProjects\myproject\mymodule\build\intermediates\dex\debug --input-list=C:\Users\myuser\AndroidstudioProjects\myproject\mymodule\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/test/BuildConfig;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.Main.main(Main.java:106)
Not sure if this is coming from my project setup or if it is coming from the libraries due to using compile on the support.test libraries.
Any advice on why androidTestCompile doesn't work is appreciated.
Also I just 2 days ago took all the latest updates from SDK Manager to 22.0.1 to no avail.
Another thing I noticed is the only jar I seem to find containing package com.google.common.* is in the espresso library. (com.google.common.* is a dependency of the testing libraries.) But when I inspect the espresso library jar with winzip all of the com.google.common.* package folder structure is there but there are no .class files in the package folders nor subfolders. Just mentioning that in case it lends a clue. Perhaps it is somewhere else also that I had missed.
Thanks!
Edit:
my source sets now look like this
sourceSets {
main {
java.srcDirs = ['src/main/java']
}
}
and dependencies
dependencies {
compile 'com.android.support:appcompat-v7:22.1.0'
compile 'com.android.support:support-v4:22.1.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}
I still get can't resolve android.support.test in my test class (test is colored red on these lines):
import android.support.test.InstrumentationRegistry;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
shouldn't I see a testing-support, espresso-core and espresso-contrib in my External libraries?

Below is my working Espresso gradle setup based on Android Studio 1.1.0 + Espresso 2.0 + Support Library v11 + gradle environment. It looks likely that you missed androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0' library.
dependencies {
// ...
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}
android {
defaultConfig {
minSdkVersion 10
targetSdkVersion 21
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets {
packagingOptions {
//...
exclude 'META-INF/LICENSE.txt'
exclude 'LICENSE.txt'
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src/main/java']
res.srcDirs = ['res']
}
}
}
About the directory, yes. just maintain androidTest and main under src folder. But, your setting does not make IDE to recognize androidTest folder as a test project folder. For that, two projects with same package name causes multi dex error I think. Use androidTestCompile to espresso-related lines in gradle dependencies and clean/rebuild project.
Note that you have to set all referenced projects gradle to same testInstrumentationRunner.
I also spent times to make it work. I wrote Korean tutorial to setup and run Espresso. Sorry about the language but screenshots might be helpful or you can try Google translate to read it.
EDIT. APPENDED BELOW
I thought why your IDE did not show green color in androidTest folder (means Test Project), and found your gradle setup seems to be wrong.
The line
{ main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } } make IDE to recognize androidTest is a Project folder not Test Project folder. remove second one. androidTest will be automatically recognized as a Test Project.

Related

How to Add and Use an AAR in AndroidStudio Project

I am trying to use a custom aar in my android project. I found dozens of examples in StackOverflow and the Web. Many failed at build, none worked. The clearest was at
http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project/
That came closest to working.
Here's what I did
Successfully created a very simple AAR (Ref.aar) from Ref.java
// Ref.java
package com.ramrod.Ref;
public class Ref {
// Square an integer
public static int
square(int val) {
return (val * val);
}
}
Created a test project (RefTest)
Created folder 'libs' under RefTest/app
Added Ref.aar to libs
File->New->New Module->Import .JAR/.AAR Package.
Selected Ref.jar as filename->Finish (appeared successful).
Modified build.gradle
// build.gradle (Module: app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "com.ramrod.RefTest"
minSdkVersion 11
targetSdkVersion 15
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile( name:'Ref', ext:'aar' )
}
}
Sync build.gradle (all)
Added reference to Ref.aar method (square) to onCreate in RefTest main activity.
int sq = Ref.square( 2 );
Build->Clean then Build->Rebuild.
This produced error: cannot find symbol variable Ref
I'm sure I'm doing something naive or just plain dumb, but I can't see it.
Any help appreciated.
You should:
1) create aar library and just put it in libs directory ( without "File->New->New Module->Import .JAR/.AAR Package" )
2) add to build.gradle (Module: app)
dependencies {
...
implementation fileTree(include: ['*.aar'], dir: 'libs')
...
}
After that you can use Ref.square(int);
Your apk will contain after build:
When you import an AAR from built in helper tools using Import aar/jar option,
studio creates a module with this aar.
So at this state you can see something similar to the state mentioned below.
When display panel is Android,
Change your panel mode to Project and open your testaar , you can actually see a build.gradle file for your module and the corresponding aar.
That is why your statement
compile( name:'Ref', ext:'aar' )
was not working.
To add this aar to your project(after using import aar/jar), what you can do is to first add the module to the settings.gradle (Project settings file)
include ':app', ':testaar'
then directly add to your application level build.gradle file
implementation project(':testaar')
2)Another way is to
Right-click on your Application Module ->Select Open Module Settings -> Select the Module -> Go to Dependencies tab
P.S you can also open this window from Build->Edit Libraries and Dependencies
You will come across a window as below
Click on the small + icon, then Module option and finally add the required module(testaar)
Sync your code and voila it will start working now.

Cannot resolve symbol AndroidJUnit4

I'm trying to add loginfacebook for my app. But when I added a repository that is need in doing this. It caused an error. The AndroidJUnit4 cannot resolve now.
ExampleInstrumentedTest.java
package com.example.user.enyatravelbataan;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* Instrumentation test, which will execute on an Android device.
*
* #see Testing documentation
*/
#RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
#Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.example.user.enyatravelbataan",
appContext.getPackageName());
}
}
and this is my build:gradle(app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
useLibrary 'org.apache.http.legacy'
repositories {
mavenCentral()
}
defaultConfig {
applicationId "com.example.user.enyatravelbataan"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile(name: 'wikitudesdk', ext: 'aar')
// compile 'org.apache.httpcomponents:httpclient:4.5'
// compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile files('libs/MD5Simply.jar')
compile files('libs/GenAsync.1.2.jar')
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:multidex:1.0.0'
compile 'com.google.android.gms:play-services:9.8.0'
compile 'com.google.android.gms:play-services-location:9.8.0'
compile 'com.google.android.gms:play-services-appindexing:9.8.0'
compile 'com.android.support:cardview-v7:24.2.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:design:24.2.1'
compile 'com.android.volley:volley:1.0.0'
compile 'com.android.support:support-v4:24.2.1'
testCompile 'junit:junit:4.12'
}
repositories {
flatDir {
dirs 'libs'
}
}
apply plugin: 'com.google.gms.google-services'
Try
androidTestImplementation 'com.android.support:support-annotations:23.1.0'
androidTestImplementation 'com.android.support.test:runner:0.4.1'
androidTestImplementation 'com.android.support.test:rules:0.4.1'
Add following above dependencies section
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:23.1.0'
}
In addition to the above answer please ensure you follow these steps strictly:
I was banging my head against the wall and/or any object i see in front of me in order to make my SQLite function unit tested. No matter what I do, how strictly follow the suggestions provided my many wise people all over the internet did not work.
I then had to go to preschool and start over to realize my stupid mistake. I learned that AndroidJUnit4 can only be used with Instrumentation Test and JUnit must be used for local tests. That being said, the folder must be src/androidTest/java. I had my test class directly under androidTest folder, hence I had to face that nasty error. However, the moment I moved it under src/androidTest/java everything went very clear like "I can see clearly now the rain is gone".
Take a look at this article which says...
Run Instrumented Unit Tests To run your instrumented tests, follow
these steps:
Be sure your project is synchronized with Gradle by clicking Sync
Project in the toolbar. Run your test in one of the following ways:
To run a single test, open the Project window, and then right-click a
test and click Run . To test all methods in a class, right-click a
class or method in the test file and click Run . To run all tests in a
directory, right-click on the directory and select Run tests . The
Android Plugin for Gradle compiles the instrumented test code located
in the default directory (src/androidTest/java/), builds a test APK
and production APK, installs both APKs on the connected device or
emulator, and runs the tests. Android Studio then displays the results
of the instrumented test execution in the Run window.
Therefore folks, for instrumentation test the folder must be (do not forget the case)
src/androidTest/java
and for local tests the folder must be
src/test/java
You can then have your package folder(s) to match your app package
Hope, this helps for the community!
If this issue is coming after migrating to AndroidX. Do this:-
1. Correct imports
import androidx.test.runner.AndroidJUnit4
instead of
import android.support.test.runner.AndroidJUnit4
2. Correct method
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
instead of
Context appContext = InstrumentationRegistry.getTargetContext()
Another important thing (that has surfaced this issue for me) is to check if you have changed the build type for tests - that would be the testBuildType option in your module's build.gradle.
If you did change it (like I have), then before editing any Android tests:
Go to your Android Studio build/flavors tab
Select the same build type you specified in your testBuildType Gradle property
Sync Android Studio with Gradle
To run the app normally again, you would obviously need to switch back to 'debug' I suppose.
Note: Even though this fixes my core issue as described by the question here, I'm still looking for a way to support both custom build types for Android tests, but also allow debug build types; my ultimate goal is to have CI running the tests with a special build type, but let developers run them in 'debug' mode. So if anyone has an idea on how to accomplish that, the comments section is yours. :)
Just add compile "com.android.support.test:runner:1.0.1" to your Gradle.
For me it was because some of the androidTestImplementation imports were not up to date in build.gradle. After I updated them all to the newest version, the error was gone.
Note:
My project had multiple modules and I had to update them in every module.
Please try the latest API like following
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-ore:3.0.2'
still, No luck then try "implementation" instead of testImplementation, androidTestImplementation and androidTestImplementation and sync in Gradle.
the solution that worked for me is changing the emulator (one that uses the api you are using)
To do that click on the symbol of the emulatr next to the Run button (ex : Pixel 2)
then go down to Open AVD Manager (add the api u need (ex API 29))
Thats all

How can I set up a simple gradle project that uses sqlite4java?

I'm starting a simple java test project using sqlite4java and building using java.
I can get the core sqlite4java library downloaded easily, but I'm not sure what the best (any!) way to get gradle to download the native libraries and put them in the right place.
This is my build.gradle file:
apply plugin: 'java'
/* We use Java 1.7 */
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'
repositories {
mavenCentral()
}
sourceSets {
main {
java.srcDir 'src'
output.classesDir = 'build/main'
}
test {
java.srcDir 'test'
output.classesDir = 'build/test'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile "com.almworks.sqlite4java:sqlite4java:1.0.392"
compile "com.almworks.sqlite4java:libsqlite4java-osx:1.0.392"
}
But when I run a simple test I get:
TestTest > testSqlite4Basic FAILED
com.almworks.sqlite4java.SQLiteException at TestTest.java:15
Caused by: java.lang.UnsatisfiedLinkError at TestTest.java:15
(I'm building from within IntelliJ, but am using the gradle build options - so I don't think it's ItelliJ stuffing up the class path when running the tests...)
I'm pretty sure the first time I tried to build I got some message about being unable to unpack the libsqlite4java-osx ZIP file, (not surprisingly as maven central says its a dylib file).
What do I need to do to make gradle do the right thing?
ASIDE: I'm able to get the code to run by manually copying the downloaded .dylib from the maven cache into somewhere listed in my java.library.path (Think I used $HOME/Library/Java/Extensions on my mac). But this seems contrary to the whole point of packaging all setup and dependencies in the .gradle file, and wont let me distribute anything easily later.
I suppose, you need to use additional gradle plugin to handle native libraries or make your own specific tasks, to upload and put native libs in right place, in order to they could be found and linked.
At the moment I know only about one such a plugin, hope it can solve your problem https://github.com/cjstehno/gradle-natives
Edit:
The problem with plugin in your case is the fact, that your dependecny "com.almworks.sqlite4java:libsqlite4java-osx:1.0.392" is native lib by itself, not a jar with included native lib as I supposed. So, in that case, you can simply add this dependency in dependencies par of build script, as it'a already done, and then create a custom copy task, to put it in any place you need. Tried to do it with gradle 2.6 on Win7, look like:
task copyNtiveDeps(type: Copy) {
from (configurations.compile+configurations.testCompile) {
include "libsqlite4java-osx-1.0.392.dylib"
}
into "c:\\tmp"
}
In your case, you just need to set "into" property to some path from java.library.path. And the second, you can make this task runs automaticaly with gradle task properties dependsOn and mustRunAfter.
Here's a complete answer based on Stanislav's comments.
apply plugin: 'java'
/* We use Java 1.8 */
sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '1.0'
repositories { mavenCentral() }
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile "com.almworks.sqlite4java:sqlite4java:1.0.392"
compile "com.almworks.sqlite4java:libsqlite4java-osx:1.0.392"
}
sourceSets {
main {
java.srcDir 'src'
output.classesDir = 'build/main'
}
test {
java.srcDir 'test'
output.classesDir = 'build/test'
}
}
/* Copy the native files */
task copyNativeDeps(type: Copy) {
from (configurations.compile+configurations.testCompile) {
include "*.dylib"
}
into 'build/libs'
}
/* Make sure we setup the tests to actually copy
* the native files and set the paths correctly. */
test {
dependsOn copyNativeDeps
systemProperty "java.library.path", 'build/libs'
}
And example test source to run for it:
import com.almworks.sqlite4java.SQLiteConnection;
import com.almworks.sqlite4java.SQLiteStatement;
import org.junit.Test;
import java.io.File;
public class SqliteTest {
#Test public void aTest() throws Exception {
SQLiteConnection db = new SQLiteConnection(new File("/tmp/database"));
db.open(true);
SQLiteStatement st = db.prepare("SELECT name FROM dummy");
try {
while(st.step()) {
System.err.printf("name = %s\n", st.columnString(1));
}
} finally {
st.dispose();
}
}
}
For compiling your own sqlite3 libraries, you can check github android-sqlite3
For the current Android Studio (3.3.1), you can simply add the .so files inside app/build.gradle like below:
android {
...
sourceSets {
main {
jniLibs.srcDirs += ['<your-path>/your-libs']
}
}
...
}
Explanation
jniLibs.srcDirs is the gradle path pointing at jni libraries, operator += denotes the additional jni libraries besides the default ones inside app/src/main/jniLibs as below:
app/
├──libs/
| └── *.jar <-- java libs
├──src/
└── main/
├── AndroidManifest.xml
├── java/
└── jniLibs/ <-- default directory for jni libs, i.e. <ANDROID_ABI>/**.so
And please note that the jni libraries have to be organized according to android ABI, i.e.
your-libs/
├── arm64-v8a/ <-- ARM 64bit
│ └── lib-your-abc.so
├── armeabi-v7a/ <-- ARM 32bit
│ └── lib-your-abc.so
├── x86_64/ <-- Intel 64bit
│ └── lib-your-abc.so
└── x86/ <-- Intel 32bit
└── lib-your-abc.so
I used this method to setup sqlite4java in anroid studio 3.1.4.
First download the library from this link:
https://bitbucket.org/almworks/sqlite4java
The download will contain a zip file containing a folder named android and other .jar , .so and .dll files.
Copy the three folders in android i.e 'armeabi', 'armeabi-v7a' and 'x86' into a folder named 'jniLibs'.
Copy the above folder i.e 'jniLibs' into yourproject/app/src/main/ folder.
Then implement the gradle dependencies for java4sqlite:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "your.project.name"
minSdkVersion 15
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'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:26.1.0'
implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'
implementation "com.almworks.sqlite4java:sqlite4java:1.0.392"
implementation "com.almworks.sqlite4java:libsqlite4java-osx:1.0.392"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Android Studio wearable imports "not working"

Long story short I have decided to play around with the new Google wearable stuff, so I opened up android studio and clicked the import from sample project. (mind you it does not matter what project I chose to import and have tried many of them)
I open the project and Everything is wrong because Android studio can't find the wearable imports... BUT it CAN build it to the watch just fine. So basically if I type
mApiClient = new GoogleApiClient.Builder(this);
The GoogleApiClient is made red because it "Cannot find the object"
import com.google.android.gms.common.ConnectionResult; <-- cannot find symbol common
import com.google.android.gms.common.api.GoogleApiClient; <-- cannot find symbol common
import com.google.android.gms.common.api.ResultCallback; <-- cannot find symbol common
import com.google.android.gms.wearable.MessageApi; <-- cannot find symbol MessageApi
import com.google.android.gms.wearable.MessageEvent; <-- cannot find MessageEvent
import com.google.android.gms.wearable.Node; <-- Cannot find Node
import com.google.android.gms.wearable.NodeApi; <-- Cannot find NodeApi
import com.google.android.gms.wearable.Wearable; <-- Cannot find Wearable
Now mind you... All of this builds... It just renders the editor useless due to the fact that it gives me no code assistance and is always telling me there are 100s of errors in the project. It is worse then notepad!
Things I have tried:
Uninstalling and reinstalling Android Studio (3 times now)
Uninstalling and reinstalling ALL of the android SDK's and extra
tools
Clicking the button to Sync Project with Gradle (1000s of times)
Checking all Gradle files for the right packages (again, these are
the sample code and it does build... so it has to be getting the
right stuff...)
Threatening my PC with installing Hackintosh on it and then burning
it. (at least 6 times now)
I have tried looking around but so far have not found anything that fixed it. any help would be much appreciated.
Thanks
EDIT:
(Delayed Application wearable sample code!)
gradle files:
Module Application:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'com.android.application'
repositories {
jcenter()
}
dependencies {
compile "com.android.support:support-v4:21.0.2"
compile "com.android.support:support-v13:21.0.2"
compile "com.android.support:cardview-v7:21.0.2"
compile 'com.google.android.gms:play-services-wearable:6.5.+'
compile 'com.android.support:support-v13:21.0.+'
wearApp project(':Wearable')
}
// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
'main', // main sample code; look here for the interesting stuff.
'common', // components that are reused by multiple samples
'template'] // boilerplate code that is generated by the sample template process
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 18
targetSdkVersion 21
}
sourceSets {
main {
dirs.each { dir ->
java.srcDirs "src/${dir}/java"
res.srcDirs "src/${dir}/res"
}
}
androidTest.setRoot('tests')
androidTest.java.srcDirs = ['tests/src']
}
}
Module: Wearable
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'com.android.application'
dependencies {
compile 'com.google.android.gms:play-services-wearable:6.5.+'
compile 'com.android.support:support-v13:21.0.+'
compile 'com.google.android.support:wearable:1.1.+'
}
// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
'main', // main sample code; look here for the interesting stuff.
'common', // components that are reused by multiple samples
'template'] // boilerplate code that is generated by the sample template process
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt')
}
}
sourceSets {
main {
dirs.each { dir ->
java.srcDirs "src/${dir}/java"
res.srcDirs "src/${dir}/res"
}
}
androidTest.setRoot('tests')
androidTest.java.srcDirs = ['tests/src']
}
}
Hope this helps! hopefully I'm missing something stupid. On my mac I was able to import the sample code and run it (on my windows I can run it... just editing is going to be terrible.)
Sounds like it could just be a problem with your settings file somehow being corrupted. I would try reseting android studio to a default state.
See this question for steps - How to reset Android Studio

Android Studio not detecting support libraries on Compilation

Since Android Studio is going to be default IDE for Android Development, I decided to migrate my existing project into Android-studio. The project stucture seems different and the hierarchy of folders in my Project is as follows:
Complete Project
->.idea
-> build
-> Facebook SDK
-> MainProject
-> ... (Other Libraries)
build.gradle
local.properties
settings.gradle
...
External Libraries
-> Android API 8 Platform
-> Android API 18 Platform
-> Android API 19 Platform
-> 1.7 Java
-> support-v4-19.1.0
My MainProject has a libs folder which contains different jars used within the project. It surprisingly does not contain the android-support-v4 jar which was present in my eclipse project. So, it seems that the external Libraries folder at the root must take care of it.
But after import, when I tried to compile the project started throwing "Symbol not found error" for Certain Classes all relating to android support library.
For Eg: The auto complete in Android Studio gives me suggestion for NotificaitonCompat from android.support.v4.app.NotificationCompat, but when I try to compile my Project Module it says
Error:(17, 30) error: cannot find symbol class NotificationCompat Error:Execution failed for task ':app:compileDebugJava'.> Compilation failed; see the compiler error output for details.
This happens in many other classes too for the same support library. I tried to insert a jar and changed the same in the build.gradle for the MainProject, but the error persists.
I even tried restarting and building the project again, but nothing changed.
EDIT:
I am attaching the Gradle file inside the MainProject
build.gradle in MainProject Module
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "package.app"
minSdkVersion 8
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
dependencies {
compile project(':facebookSDK')
compile project(':library')
compile project(':volley')
compile 'com.google.android.gms:play-services:+'
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
compile 'com.android.support:support-v4:19.1.0'
compile files('libs/FlurryAnalytics_3.3.3.jar')
compile files('libs/universal-image-loader-1.8.4.jar')
....
}
This part of your build file:
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
is telling the build system to ignore support-v4, which is why it isn't compiling. Remove that.
In your build file, you have this, which is the correct way to include support:
compile 'com.android.support:support-v4:19.1.0'
If you have the support library jar file in the libs directory of any of your modules, remove it and make sure you refer to it this way -- if you include the library as a jar, you're likely to run into a problem where the jar is included multiple times which will result in a dex error.
If you have jar files in a 'libs' directory, you can specify it in you build.gradle file :
dependencies{
compile fileTree(dir: 'libs', include: ['*.jar']
compile 'com.android.support:support-v4:21.0.3' //to automatically add up-to-date support lib
}
Hope this helps!

Categories

Resources