Add OkHttp to my project - java

I am trying to add OkHttp to my project, i download both OkHttp and Okio library and add it to the libs directory.
than i add the compile methods:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I try to Build the App and i get this error:
Error:(27, 1) A problem occurred evaluating root project 'APP'.
> Could not find method compile() for arguments [com.squareup.okhttp3:okhttp:3.4.1] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Any idea what can be the problem? i am using Android Studio om mac OS

Delete this line of your root gradle :
compile 'com.squareup.okhttp3:okhttp:3.4.1'
And Add this to your dependencies's app's module build.gradle

There are basically two build.gradle files in our project.
Top level gradle (can be found in the project directory)
Module level gradle (present inside the app folder of the project)
Here the problem is you are compiling the okHttp
compile 'com.squareup.okhttp3:okhttp:3.4.1'in the Top level gradle file try to place it in your Module level gradle. Also if you are importing a library using gradle you don't have to add it in the lib folder explicitly
Google gradle documentation

Related

Gradle How to add dependencies between subprojects in root project's build.gradle

I am working on a multi-module gradle java project where it has three project modules
1.webapp
2.common
3.data-client
I have four build.gradle files. Three inside these projects and one file in the root folder.
I am trying to add a dependency between webapp and common project inside the build.gradle of root project. Below is my build.gradle file but when I run gradle build. I am getting an error mentioned below. I know I can add the dependency directly in the sub-project but I want to know why this is not working. My gradle version is 7.6
org.gradle.api.GradleScriptException: A problem occurred evaluating root project 'web-app-gradle-local'.
at org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException: Could not find method implementation() for arguments [project ':common'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
at org.gradle.internal.metaobject.AbstractDynamicObject$CustomMissingMethodExecutionFailed.<init>(AbstractDynamicObject.java:190)
at org.gradle.internal.metaobject.AbstractDynamicObject.methodMissingException(AbstractDynamicObject.java:184)
at org.gradle.internal.metaobject.ConfigureDelegate.invokeMethod(ConfigureDelegate.java:93)
Build.gradle
plugins {
id 'application'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'junit:junit:4.13.2'
implementation 'org.springframework.boot:spring-boot-starter-security:3.0.0'
implementation 'com.google.guava:guava:31.1-jre'
}
project(':web-app') {
dependencies {
implementation project (':common')
}
}

Tried importing a library and ended up with Plugin with id 'com.android.application' not found

I just tried importing a library into my android project and ended up with this Plugin with id 'com.android.application' not found. error. I was supposed to import "Filters.jar" file from jhlabs but accidentally imported "gradle-wrapper.jar" of another project . I am stuck here and nothing seems to work. I also accidentally deleted my project level build.gradle file, Which contained the following code :
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I was trying to import the library following this answer,
https://stackoverflow.com/a/35369267/9504498
I messed up apparently I have tried many solutions but nothing is working. Please, help me fix this error I am stuck here.
How can I get the project level build.gradle file back? and how can I fix this Plugin with id 'com.android.application' not found.?
Any help will be appreciated, Thanks.
You need to re-add your build.gradle to your project by following the structure of sample project which you can create from Android Studio.
Usually, the project structure is like the following:
app -> (your app module)
gradle
build.gradle -> (project build.gradle)
gradlew
gradlew.bat
local.properties
settings.gradle
The following error:
Plugin with id 'com.android.application' not found.
is because gradle can't found the plugin from your project build.gradle, so add the following code to your project build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
For adding a jar library, as #daniel-nugent says, try:
How to manually include external aar package using new Gradle Android Build System
Tried many options but nothing really worked and finally, created a new project with exact name as the previous one and the exact package name, then copied the "main" from the previous project to the new project and edited the app level build.gradle by copying from the previous project and things are back to normal now. Will try importing the .jar library by following #daniel-nugent method.

libGdx and facebook

I'm testing facebook login with libgdx project. In my project-level build.gradle file, I added the gdx-facebook dependency from here
project(":android") {
apply plugin: "android"
configurations { natives }
dependencies {
compile project(":core")
......
......
compile "de.tomgrill.gdxfacebook:gdx-facebook-android:1.4.1"
}
}
and added the following dependency in the Module:android build.gradle file.
dependencies {
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
}
There is no error in such condition. But when I remove tomgrill dependency from project-build.gradle file, android studio show me the following alert and the project cannot run.
Gradle project sync failed. Basic functionality(e.g. editing,
debugging) will not work properly.
Why facebook dependency does not work without tomgrill gdx-facebook extension? What the problem is?
Artifact facebook-android-sdk available in mavenCentral repository so you've to add mavenCentral() in your project repositories list in root build.gradle file.
By default, In LibGDX projects mavenCentral() is in repo list.
Some transitive dependency in your project, Internally facebook-android-sdk using many support library like annotations, customtabs, appcompat-v7, support-v4 and more..
These support libraries are available on Google's Maven repository so you need to include Google's Maven repository in your top-level build.gradle file :
allprojects {
repositories {
google()
// If you're using a version of Gradle lower than 4.1, you must instead use:
// maven {
// url 'https://maven.google.com'
// }
// An alternative URL is 'https://dl.google.com/dl/android/maven2/'
}
}

Neokree Navigation drawer: Gradle DSL method not found: 'compile()'

I am trying to Set up a Navigation Drawer by Neokree https://github.com/neokree/MaterialNavigationDrawer on Android Studio with no success.
After adding this to my gradle -> build.gradle file
repositories {
mavenCentral()
}
dependencies {
compile 'it.neokree:MaterialNavigationDrawer:1.3.3'
}
And then i get this Error Saying "Gradle project Sync Failed" and this below
Error:(27, 0) Gradle DSL method not found: 'compile()'
Possible causes: The project 'MaterialNavigationDrawer' may be using a version of Gradle that does not contain the method.
Gradle settings The build file may be missing a Gradle plugin.
Apply Gradle plugin.
This is my gradle folder -> Build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'it.neokree:MaterialNavigationDrawer:1.3.3'
}
I know am doing something wrong for sure, and I can't seem to figure it out.
There is a great library called MaterialDrawer. You can integrate this library in less than 5 minutes in your project (read its README.md and Wiki - a lot of informations is available there!).
Good luck!

Adding external .jar to androidstudio project

I've added the external library bsh-2.0b4.jar to an android project in android-studio by going into Project structure -> Modules -> myProject -> Tab-Dependencies -> + Sign and then add the .jar file.
I also tried to copy the file into the /libs directory and then rightclick in studio and add as library... (both methods independently!!). I inserted the following code as a test
import bsh.Interpreter;
...
Interpreter interpreter = new Interpreter();
interpreter.eval("result = (7+21*6)/(32-27)");
return interpreter.get("result").toString();
I compile with the buildin button in android.
The build.gradle looks like:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 17
}
}
dependencies {
compile 'com.android.support:support-v4:13.0.+'
}
But when I compile everything I receive the error
Gradle: error: package bsh does not exist
Gradle: error: cannot find symbol class Interpreter
Can anyone help me pls?
Try this...
Create libs folder under your application folder.
Add .jar files to libs folder.
Then add .jar files to app's build.gradle dependency.
Finally Sync project with Gradle files.
1.Create libs folder:
2.Add .jar to libs folder:
3.Edit app's build.gradle dependency:
Open app/build.gradle
4.Sync project with Gradle files:
Finally add .jar files to your application.
Change your dependencies like that
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile 'org.beanshell:bsh:2.0b4'
}
You can now remove manually downloaded dependencies in libs directory.
What you did was adding libraries to Android Studio project only. You should always add them to Gradle build files as only this is interpreted by Android Build Tools.
There is also new version of build tools 18.0.1, you can install them and change version in you build.gradle. As far as I know they can handle aar dependencies better.

Categories