How to add a Hamcrest library in an Android Studio project? - android

I'm working with my first project in Android Studio 2.2. When I try to Sync the project with the Gradle, it shows an error error: package org.hamcrest does not exist
Therefore I had downloaded the hamcrest.jar library and added manually it under myfirstproject\app\libs
But, I got the following error when I add hamcrest.jar to my project.
Could not find method compile() for arguments(file collection) on object of type org.gradle.api.internal.artifacts.DSL.dependencies.DefaultDependencyHandler
This is my build.gradle file
-
Have I missed anything during the installation of my Android Studio?
Or should I need to add any libraries to the project?
If needed how it is to be done?

You added that to the wrong gradle file. Read the comment in that file that says not to add app dependencies there
Open app/build.gradle and you should notice that you already have
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
So, which means you don't have to add anything else to compile any jar files.
You really should not use a Jar file, though.
Add this
testCompile 'org.hamcrest:hamcrest-library:1.3'

Hamcrest library is something that is automatically configured with Android Studio during the installation.
Here the error is due to the incomplete installation of Android Studio. That is, most of the important external packages haven't get downloaded during the installation (including hamcrest.jar).This happened may be due to the loss of network connection at the time of installation.
So the best solution is
Uninstall the Android Studio,
Delete all the belonged files (AndroidStudioProjects,.AndroidStudio2.2 and .android folders ). Don't forget to move your current project to any other folder.
Once again install the Android Studio by making sure that you have a good network connection.
Open your last project from where it was moved
Sync the project with Gradle.
See that everything works fine!

Related

How do I run an github-imported android application on android studio

So before I say anything, I am a total noob to android, but I am way to curious to figure out how to run this open source application that was built for a hackathon. ( https://github.com/android-fanatic/CosmosBrowserAndroid )
They dont have an APK, so I couldn't try it, but I really want to try it out. But every time I try to import the application into android studios, I get this message:
Error:(16, 0) Gradle DSL method not found: 'runProguard()'
Possible causes:
The project 'CosmosBrowserAndroid-master' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper file
The build file may be missing a Gradle plugin.
Apply Gradle plugin
And thus, I can't try the application out. Could anyone give me a step by step solution?
Unzip the github project to a folder. Open Android Studio. Go to File->Import Project. Then choose the specific project you want to import and then click Next->Finish. It will build the Gradle automatically and'll be ready for you to use.
P.S: In some versions of Android Studio a certain error occurs-
error:package android.support.v4.app does not exist.
To fix it go to Gradle Scripts->build.gradle(Module:app) and the add the dependecies:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
Then start an emulator and run your app
For more information:
https://developer.android.com/tools/building/building-studio.html

Android Could not find class 'org.apache.commons.scxml.env.jexl.JexlContext'

I'm using apache commons SCXML to define a state machine my Android app. In my Android Studio project, I've added the following in the dependencies section of my build.gradle file:
compile 'commons-scxml:commons-scxml:0.9'
compile 'commons-logging:commons-logging:1.1.3'
compile 'org.apache.commons:commons-jexl:2.1.1'
Unlike the answer to this question, I cannot add xalan as it tries to overwrite Android system files, leading to this error.
I then clicked the 'Sync Project with Gradle Files' button, and the commons-scxml and commons-jexl files showed up in the "external dependencies" section of my project.
However, when I run my app, I still get this error:
Could not find class 'org.apache.commons.scxml.env.jexl.JexlContext', referenced from method org.apache.commons.scxml.env.AbstractStateMachine
You also need org.apache.commons:commons-jexl:1.1 as a dependency.

Adding a jar library for Android project in Android Studio 0.4.2

I'm a newbie to Android Studio IDE. I used to implement Android application, using Eclipse IDE. As a beginner in Android Studio, I tried this lesson from YouTube to include a external jar file to my project and use it. But unfortunately I'm unable to use that library even though the Gradle or Android build doesn't throw any error/exception when compile and run.
I've seen related questions from SO too. As an example, I've tried out things that discuss in this question.
In the build.gradle file, I used
compile files('libs/jsoup-1.7.3.jar')
and next time used
compile fileTree(dir: 'libs', include: '*.jar')
Neither work for me. After editing that build.gradle file, I used Sync Project With Gradle Files tools and also command line clean
gradlew clean
too. No error/exceptions, but when I try to write Docu... in my MainActivity class it doesn't show the import option org.jsoup.nodes, but show other imports options. What could be the mistake I've done.
There were some bug in 0.4.2 related to dependencies management, do upgrade your studio to 0.4.3 will solve your problem .

Gradle error: package com.facebook does not exists for android studio

I'm again lost in this facebook login part for my app. Well what happen is that I downloaded the facebook SDK from the facebook site itself then used eclipse to export the project with gradle to be used for android studio. Now in the project stucture of my main app in android I imported the Module of the facebook that I converted then on dependencies I add the module dependency of facebook in my main app. Now I tried to import the facebook package and it just works fine in my MainActivity. Now after I tried to run it errors appears see screenshot:
I tried using the ./gradle clean for both projects and it cleans just fine but I get an error for the build part. I think something went really wrong after I exported the facebook project in gradle structure. How can I solve this error? Anyone encountered this?
worked for me:
drag and drop the jar file into libs of the project (click ok to copy etc..).
then right click on the jar file -> Add as library.
Then open gradle.build and change:
Before:
dependencies {
compile files('libs/android-support-v4.jar')
}
After:
dependencies {
//compile files('libs/android-support-v4.jar')
compile fileTree(dir: 'libs', include: '*.jar')
}
We encountered the exact same problem at roughly the same time, and this is how I went about resolving it in my case.
Move the Facebook SDK project to some remote location.
Create a new Android library module in the parent Gradle project. It should be setup for Gradle. Call it 'Facebook', say. Check that its Android facet is indeed a library module.
Add all the necessary source, resource, manifest, and property files to this new module in the appropriate locations.
Modify the settings.gradle file of the parent project, so that it contains include ':Facebook', ...
Add the dependency on Facebook to your main project, 'Echo', by opening its build.gradle file and adding compile project(':Facebook') to the dependencies.
Both Echo and Facebook might depend on the android support library by now; remove this dependency from your Echo project. For instance, compile fileTree(dir: 'libs', include: '*.jar', exclude: 'android-support-v4.jar'). Check the Facebook.iml file, you want the exported dependency <orderEntry type="library" exported="" name="android-support-v4" level="project" />.
Run ./gradlew clean && ./gradlew build from the parent path.
After this clean up the dependencies in Android Studio, set the absolute apk path for your Echo project in Android Studio. This might not solve everything for you but I think these were the key steps that helped me, especially step 4. All being well you'll be much closer to solving this.
You can try importing Facebook's ant build.xml some where instead and report how you get on, but I found I was getting target redeclaration errors when I already had this error to resolve.

Proper way to add global library in android-studio/gradle

First of all, I know how to add a local library to the build.gradle file, it was discussed in several questions here already (which are all basically the same), see here, here and here. But you have to hardcode the paths in the compile files('/path/to/lib.jar') statements in the build.gradle file, which isn't nice, not redistributable, etc, IF you use a library not within the project's folder structure. I prefer to maintain this library for all my projects in the same place (so it is always up to date for all projects etc.). So I would like to know how to add a library, which is not available via Maven, to an Android-Studio project using gradle, in a sane way, given that the library is added as a global library in AS's preferences.
What I have done so far:
I use Google's new Android-Studio, which uses gradle for the build management, to build an Xposed framework module. For that, I have to include an external library, XposedLibrary, which I downloaded from the respective Github repository to keep it up-to-date.
It contains the jar XposedLibrary/XposedBridgeApi.jar, which I added in AS as a global library (Ctrl+Shift+Alt+S -> Global Libraries -> green plus to add the folder XposedLibrary). The compilation failed, complaining that it doesn't know the imported classes. So I had to manually add the library to the build.gradle file, adding the respective line in the dependencies like so:
dependencies {
compile files('libs/android-support-v4.jar')
compile files('/home/sebastian/dev/android/XposedMods/XposedLibrary/XposedBridgeApi.jar')
}
I tried out to just add compile files('XposedBridgeApi.jar') or compile files('XposedLibrary/XposedBridgeApi.jar') but this didn't work.
So, what is a nice way to add an AS global library to the dependencies without using full paths? (I don't like the idea of symlinking to the jar file from within the lib/ folder ;) )
when referencing a file via
files("relative/path/to/a.jar")
the relative path is evaluated relative to the buildscript this snippet is in. so when your build.gradle file is located in let's say '/a/project/build.gradle' then the jar should be in '/a/project/relative/path/to/a.jar'. In a multiproject gradle build you can put the the jar in a folder relative to the root - project and reference it in all subprojects via
rootProject.files("relative/to/root/a.jar")
hope that helps,
cheers,
René
This post describes how to get XposedBridgeApi.jar working with Gradle in Android Sudio: http://forum.xda-developers.com/showpost.php?p=41904291&postcount=1570
I think here is the proper way:
Import Xposed in Android Studio
Edit the /app/build.gradle like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
provided fileTree(dir: 'deps', include: ['*.jar'])
}
The best way is to use "provided files('src/XposedBridgeApi-54.jar')" as the lib isn't allowed to be included in the module, because the XposedBridge is already installed on the phone.
With Android Studio, you have to first understand that the IDE uses the same model for a project that your command line build (gradle) uses. That is why the Project Structure dialog has a pop up that says edits here will have no effect. So adding a global library will also have no effect.
The correct way to fix such issues is to edit your gradle build scripts so that the command line gradle build works properly. Then you should just have to click on "Tools | Android | Sync Project with Gradle files" menu item to refresh the project structure in the IDE.
Finally, if your dependencies are not going to be in Maven Central, then you'd have to create a local maven repository. Read the thread here: https://groups.google.com/d/msg/adt-dev/eCvbCCZwZjs/vGfg-4vNy9MJ for background.

Categories

Resources