I'm running Android Studio 0.5.0 with Gradle 1.11. I'm trying to install Espresso library from com.jakewharton.espresso:espresso:1.1-r2. For some reason AS couldn't recognize Espresso classes after project synchronization. So every time I try to import import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView; inside androidTest folder files it marks it as invalid.
Here's my build.gradle:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.0.2'
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.squareup.dagger:dagger-compiler:1.2.1'
compile 'com.squareup.dagger:dagger:1.2.1'
androidTestCompile ('com.jakewharton.espresso:espresso:1.1-r2') {
exclude group: 'com.squareup.dagger'
}
}
External libraries:
So this is basically a bug with Android Studio (i'm guessing).
Reference:
Issue raised in adt-dev groups
Actual bug - #66841
Workaround (until the bug is fixed):
Add a duplicate provided dependency in your gradle file like so:
dependencies {
// ...
provided 'com.jakewharton.espresso:espresso:1.1-r2'
androidTestCompile ('com.jakewharton.espresso:espresso:1.1-r2') {
exclude group: 'com.squareup.dagger'
}
}
This issue was driving me crazy. And seems like it's a known bug in Android Studio. In my case it resolved after I've changed Build Type from Release to Debug for the parent app. Hope this may be helpful for someone
Espresso 2.0
Recently Espresso 2.0 was released making it now part of the Android Support Library. This was announced on the android dev blog.
Setup Guide
With that they also linked an updated setup guide. There you can find instructions to configure from scratch or to update your existing espresso config for 2.0.
Other Tips
Changes are the above 2 links contain all information you need. If not I have listed some common mistakes below
Upgrade Android Studio to 1.0.*
Start by upgrading your android Studio build. You should be able to get at least 1.0 from the stable builds channels (=default). So just use the menu option Android Studio > Check for updates... .
To get the latest from the latest you can also go into Preferences, search for updates and change the channel to canary channel.
Update Android Support Library to v 11+
Espresso was included in the Support library from version 11 so you have to get at least that version. Check for updates using the Android SDK manager. The Support Library is within the Extras tree at the bottom.
New dependencies and namespace
If upgading from an older espresso release you'll have to update the dependencies and the namespace. For new projects just add these to the dependencies in your build.gradle file.
dependencies {
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}
And since the namespace changed you'll have to update all imports:
android.support.test.espresso
Note that it's easier to use static imports. Some commonly used imports as an example:
import static android.support.test.espresso.Espresso.onView;
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;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
For asserts use hamcrest, again some examples:
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;
Instrumentation runner
The test runner needs to be configured in both your build.gradle file within defaultConfig and the run configuration used to launch your tests from Android Studio.
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
And in your run configuration use this as instrumentation runner (full class name only):
android.support.test.runner.AndroidJUnitRunner
Example test case
And an example test case to finish with. Note that MainActivity is your actvitiy you want to test. The tests themselves are public methods that start with test, like testListGoesOverTheFold in the below example.
#LargeTest
public class HelloWorldEspressoTest extends ActivityInstrumentationTestCase2<MainActivity> {
public HelloWorldEspressoTest() {
super(MainActivity.class);
}
#Override
public void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testListGoesOverTheFold() {
onView(withText("Hello world")).check(isDisplayed());
}
}
For more information on writing tests visit the espresso start guide.
Six months later this is still an issue and the bug referenced by the original responder has been reopened: https://code.google.com/p/android/issues/detail?id=66841 and given higher priority. I have not yet been able to get Android Studio to recognize Espresso classes and using the "provided" scope for dependencies didn't work to fix the issue for me. (Using AS 0.8.6 and Gradle 0.12.2)
according to http://tools.android.com/tech-docs/new-build-system/user-guide
only one build type is tested, by default it is debug Build Type.
So check that you are using debug build variant and rebuild the application. On other build types all your androidTest dependencies will not be visible.
If you need to test your current build type you can do something like this:
android {
...
testBuildType "staging"
}
Android Studio 1.5.1 didn't realize onView() or onData() methods in my case. I just did a static import of the Espresso class and all it's methods.
I added the below line and everything worked perfectly.
import static android.support.test.espresso.Espresso.*;
You're not specific about what source file you're seeing the error in, but based on my testing, I think you're trying to access the Espresso classes from one of your main application classes (inside src/main/java/). If so, that won't work, because you've included Espresso via an androidTestCompile dependency include, which makes it accessible only to your test classes, which must be under src/androidTest/java.
I've tried every solution what guys guessed above and still got the class not found error.
I figured out my solution, it saved my day.
So if you open the project tab on left to your project folders in Studio and check the build variants you can see that, your project is set to unit test. You have to re-set it to Android Instrumentation Tests and make sure your test.java file is under
**src\androidTest\java**
try these dependencies:
implementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
I was having this issue and I went through and added every espresso library I could add to my gradle file. Wait for the yellow lightbulb to come on and then click add library and search for the espresso libraries you don't have. I don't know if this will work for everyone but it worked for me.
Related
I'm working on a rect-native project and I'm trying to add a custom component to an already existing library. I want to add my own component to this library. I don't want to fork on the original project and create it there because my component will have different libraries and I think it will be harder to maintain a fork.
So I'm trying to import the project as a gradle dependency on a project of my own to import the original class AbstractMapFeature and extend its features to create my own class. I'm new to gradle and I don't even know if this is possible.
But if so, how do I import the project?
I tried through jipack:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.rnmapbox:maps:+'
}
But got an error stating that it couldn't find the project. The project doesn't compile to jitpack as can be seen here.
I also tried to install it as a node dependency and import it local from there:
maven {
url "$rootDir/node_modules/#react-native-mapbox-gl/maps/android"
}
But I don't know how to import this into my code. Tried with:
import com.mapbox.rctmgl.components.mapview.RCTMGLMapView
Which is the same import statement they use, but got an error saying it couldn't find rctmgl.
So I have two questions. Is it really possible to import the original project's code and use it on my own? And if yes, how do I do this?
Jitpack can't successfully work on any Github repo. If you look at the instructions for that library, there is a specific open source fork of the library called MapLibre, which is what you'll need (assuming this is an Android project, since I otherwise don't see anything Java/Kotlin-specific). The library is published on mavenCentral() so you don't need Jitpack. You should specify the library as
implementation 'org.maplibre.gl:android-sdk:9.4.0'
If you're just creating a subclass of a class in the library, that's just normal usage of a library. If you need to actually swap out code that's in the library, you will have to fork it. If you're forking it, none of the above applies. You need to create a forked repo, make your changes, build it, and use the build from your fork as a dependency in your project.
Due to build tools, my gradle file has to be compatible both with Android Gradle 2.1.3 and the latest Android Gradle version. As the latest Android Gradle Plugin has introduced the new implementation and api configuration, and is planning to remove the compile configuration, I am trying to figure out a way to write script that it should bebe able to support both versions.
The idea would be to use something like
def _api = api
and in the dependencies use _api instead of api.
Later we plan to add add some code like;
if (oldVersion)
_api = compile
I have tried this code as is, but it is invalid code.
Any ideas how it should be coded?
Thanks in advance
The notation:
dependencies {
api "org.example:example:1.0"
}
is really syntactic sugar for
dependencies {
add("api", "org.example:example:1.0")
}
so you could solve that problem by defining String variables, whose value depends on the Gradle version.
After updating Gradle to com.android.tools.build:gradle:3.1.0, in the log I now see:
Folder
E:\WORK\App\car_android\carapp\build\generated\source\kaptKotlin\devRelease
3rd-party Gradle plug-ins may be the cause
If you are receiving the warning:
3rd-party Gradle plug-ins may be the cause
in the build tab, it appears to be a known issue with Android Studio v3.1.x.
According to this report, It might be related to Instant App provision, but even removing that from the run/Debug configuration does not appear to resolve the issue (at least not with my installation of AS v3.1.1).
Please vote up this issue so that it will be given more attention by the Android Studio team and hopefully a timely fix.
This happens because the Kapt annotation processor uses this directory to store Kotlin generated files.
The directory is a new generated source set, the same way you can split your source files into src/main/java and src/main/kotlin
However the Android Gradle plugin does not recognize this directory as a generated source set.
For the most part this is completely harmless as most 3rd party processors are generating .Java files. Kapt writes them to the build/generated/source/kapt directory.
It had worked in my project! ->
Your build.gradle Project should look like the image below:
Note: It could be little bit different if you are not using Realm and some Google Services
Now, Let's start ->Go to you build.gradle App
Platform modules targeting Android
The update of the experimental multiplatform projects feature introduces support for Android platform modules. These modules should apply the corresponding plugin in the Gradle build script and can use the shared code from a common module:
You will copy this inside of your build.gradle ON THE TOP ADD IT-
Kapt diagnostic locations
As of now, kapt, the Kotlin annotation processing tool, can offer links to locations in the original Kotlin code rather than generated Java stubs as it reports errors encountered during annotation processing. You can enable this feature by adding these lines to the Gradle build script (build.gradle):
apply plugin: 'com.android.application'
apply plugin: 'kotlin-platform-android'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
// ...
2. NOW ADD IT:
kapt {
mapDiagnosticLocations = true
}
dataBinding {
enabled = true
}
IntelliJ IDEA plugin improvements
Kotlin 1.2.30 brings various improvements in the IntelliJ IDEA Kotlin plugin, including performance improvements, bug fixes, and new inspections and intentions.
For some projects this is important :
You will copy this inside of your build.gradle-
allprojects {
repositories {
jcenter()
google()
}
It will look like the below image
Now we need to add the implementations inside of dependencies{...// } build.gradle:
// Architecture Component - Room
implementation "android.arch.persistence.room:runtime:1.1.0-beta1"
kapt "android.arch.persistence.room:compiler:1.1.0-beta1"
// Lifecyles, LiveData and ViewModel
kapt 'com.android.databinding:compiler:3.1.0'
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.1.1"
// alternatively, just ViewModel
implementation "android.arch.lifecycle:viewmodel:1.1.1"
// alternatively, just LiveData
implementation "android.arch.lifecycle:livedata:1.1.1"
kapt "android.arch.lifecycle:compiler:1.1.1"
// Room (use 1.1.0-beta1 for latest beta)
implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"
// Paging
implementation "android.arch.paging:runtime:1.0.0-alpha7"
// Test helpers for LiveData
testImplementation "android.arch.core:core-testing:1.1.1"
// Test helpers for Room
testImplementation "android.arch.persistence.room:testing:1.0.0"
*
*
Clean your project
Build it
That's it!
More Info: Android Site
:) Let me know if it works! (If it does not work, I will
try to help you finding a better way)
According to this Android Studio issue, "The root issue with Kapt is that the Kapt resolver in IDEA from JetBrains registers those folders as 'being in a invalid path'. This is a misuse of the API."
As of 6/11/2018, there's a fix in place to double-check those messages and suppress ones about paths which are actually valid.
I delete two folder app\build\generated\source\kaptKotlin\debug and app\build\generated\source\kaptKotlin\release ("debug" and "release") and add
in gradle:
kapt {
mapDiagnosticLocations = true
}
then the project is sync without problem.
Kotlin got rid of package level visibility for module level visibility. I think that was a great idea, except it's difficult to get multiple Kotlin modules working in a Gradle multi-project setup.
I'm using Gradle Wrapper 3.1, with Android Studio 2.2 and Kotlin 1.0.4
Here is my structure:
Project
----myapp (android application)
----lib (android library)
----commons (android library)
// myapp/build.gradle
dependencies {
compile project(':lib')
}
// lib/build.gradle
dependencies {
compile project(':commons')
}
When I try to sync or build I get the following error:
Error:A problem occurred configuring project ':lib'.
Cannot change dependencies of configuration ':lib:default' after it has been included in dependency resolution.
The only thing that has worked is to make sure that the module names have a lexical order that matches the order they need to be built in, which is ridiculous, and not a proper solution.
I know I can publish lib and commons as artifacts, but I'd really prefer to not do that.
Anyone have any suggestions?
Currently I'm developing a mostly native Android application in Android Studio. I would like to unit test my 'business logic' c++ classes and run them together with my java unit tests. My test target should:
1. Create a library with all the code I want to test
2. Compile a test main executable
3. Execute tests
4. Make it a dependency for 'java unit test' target.
I've seen some threads which deploy googletest on target machine and execute on it - but that is not my goal. I want to run tests on host machine. Any testing framework is OK, googletest would be fine.
I started with baby steps, that is - I wanted to create a target to compile a simple executable in existing build.gradle file. I struggled badly, however. I added the following:
apply plugin: 'cpp'
model {
components {
nativeTestSources(NativeExecutableSpec) {
sources.cpp {
source {
srcDir "testdir"
include "**/*.cpp"
}
}
}
}
}
I got the following Error:(65, 0) Cannot add Mutate rule 'org.gradle.language.base.plugins.ComponentModelBasePlugin$Rules#closeSourcesForBinaries(org.gradle.platform.base.BinaryContainer, org.gradle.language.base.ProjectSourceSet)' for model element 'binaries' when element is in state GraphClosed.
Can anyone point me to the right direction? Ideally an existing project cointaining a similar target I could look up to. I'm yet to find one.
I use Android Studio 1.3.1 with Gradle 2.4.