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/'
}
}
Related
I am working on a project which should run on Android as well as on desktop (JavaSE). For this purpose I've separated it into 3 java modules:
AndroidApp
CommonCore
DesktopApp
Why I am using Android Studio (AS) is obvious. I've got almost everything to work but the "Run" button for the JavaSE module. The problem is simple: incomplete classpath. The module compiles, but it doesn't run. When I build it into JAR it runs fine. JAR includes all required dependencies. The only problem is the damn "Run" button when used on the JavaSE module.
top-level build.gradle: (pretty much generated by AS)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
CommonCore build.gradle:
apply plugin: 'java-library'
targetCompatibility="1.8"
sourceCompatibility="1.8"
dependencies {
api 'org.nanohttpd:nanohttpd:2.3.1'
api 'com.google.code.gson:gson:2.8.1'
api 'org.apache.commons:commons-text:1.1'
api files('src/main/resources')
}
DesktopApp build.gradle:
apply plugin: 'java'
targetCompatibility="1.8"
sourceCompatibility="1.8"
dependencies {
implementation project(':CommonCore')
implementation 'org.xerial:sqlite-jdbc:3.21.0.1'
}
jar {
manifest {
attributes 'Main-Class': 'myproject.Main'
}
from {
configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
AndroidApp build.gradle: (skipped the actual android settings to keep it short)
apply plugin: 'com.android.application'
dependencies {
implementation 'com.android.support:appcompat-v7:26.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation project(':CommonCore')
}
android { ... }
When I run it I get:
Exception in thread "main" java.lang.NoClassDefFoundError: fi/iki/elonen/NanoHTTPD
Apparently JVM could not find the dependency 'org.nanohttpd:nanohttpd:2.3.1' from CommonCore. After examining classpath I see:
/Applications/Android Studio.app/Contents/jre/jdk/<a ton of unused JARs>
/Users/bamboo/AndroidStudioProjects/MyProject/DesktopApp/build/classes/java/main
/Users/bamboo/AndroidStudioProjects/MyProject/CommonCore/build/classes/java/main
Missing entries for Gradle managed dependencies.
What have I done wrong? AS must see them because autocomplete works well.
Place the dependency inside runtime() in the gradle, for example:
dependencies {
runtime(
[group: 'org.nanohttpd', name: 'nanohttpd', version: '2.3.1'],
)
}
Reference
One way to fix it is to use NetBeans with Gradle plugin for JavaSE development.
I am writing a Java library and I would like to build the library with Gradle and then test it from a local test project.
I would prefer using Gradle 3.3 for my objective.
The library should be built for Java5 and higher.
So far my build.gradle looks like this:
plugins {
id 'jvm-component'
id 'java-lang'
}
repositories {
mavenCentral()
}
model {
components {
main(JvmLibrarySpec) {
sources {
java {
dependencies {
module 'commons-codec:commons-codec:1.10'
module 'org.apache.httpcomponents:httpcore:4.4.6'
module 'org.apache.httpcomponents:httpclient:4.5.3'
}
}
}
api {
exports 'io.simplepush'
}
targetPlatform 'java5'
}
}
}
The source code of the library is located in src/main/java/io/simplepush/Notification.java and depends on the dependencies stated in the build.gradle file.
Building the library with ./gradlew build works fine and generates build/jars/main/jar/main.jar.
However when I run a test project from IntelliJ (after including main.jar into the test project), I get the following runtime error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpEntity.
It seems like the test project does not know about the runtime dependencies needed by my library.
I am not sure on what is the correct way to tell the test project about the dependencies of my library.
I do not want a fat jar which includes all dependencies.
Listing all dependencies in the test project itself is also not an option.
Preferably I want the library itself to tell the test project about which dependencies it needs.
The library jar which you have created does not contain any dependency information which the IDE/Gradle can then resolve to be able to compile/run the test project. I see that you are using the maven central repository so what you need to do is to publish your library to your local maven repository and in the test project just add a dependency information (no just plain jar file).
So in both library and test project build.gradle add a maven local repository config.
repositories {
mavenLocal()
mavenCentral()
}
And now you need to publish the library to local repository. As you are using the gradle 3.3 you can use the Maven Publishing.
So in the library build.gradle add a maven publishing information.
publishing {
publications {
maven(MavenPublication) {
groupId 'io.simplepush'
artifactId 'project1-sample'
version '1.1'
from components.java
}
}
}
Gradle “maven-publish” plugin makes this easy to publish to local repository automatically creating a PublishToMavenLocal task.
So you can just run
gradle publishToMavenLocal
Which will publish your library with all the dependency information into local maven repository.
And then you just need to add a library information to you test projects build.gradle
dependencies {
// other dependencies .....
module 'io.simplepush:project1-sample:1.1'
}
I solved it by changing several things.
Thanks to #Babl for pointing me in the right direction.
My new library build.gradle looks like this:
plugins {
id 'java'
id 'maven-publish'
}
sourceCompatibility = 1.5
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'commons-codec:commons-codec:1.10'
compile 'org.apache.httpcomponents:httpcore:4.4.6'
compile 'org.apache.httpcomponents:httpclient:4.5.3'
}
publishing {
publications {
maven(MavenPublication) {
groupId 'io.simplepush'
artifactId 'project1-sample'
version '1.1'
from components.java
}
}
}
Now I can push the library to the local maven repository with ./gradlew publishToMavenLocal.
The build.gradle of the test project uses the application plugin and defines a main class (which is Hello in my case). Then I can run ./gradlew installDist to generate an executable file (see Application plugin docs) which puts all dependencies in the classpath and runs just fine.
group 'com.test'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'application'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'io.simplepush:project1-sample:1.1'
}
mainClassName = "Hello"
This specify what repositories to check to fetch the dependencies from
repositories {
mavenCentral()
}
Therefore, anything that is in the dependecies{} will be fetched from those above.
If the test project is not coupled with the library project, (#RaGe example) new test project needs to know where to take the dependency from - you need to publish it, using preferred method.
After that, your new test project needs to specify the library with the preferred configuration (compile...runtime etc) in the build.gradle dependencies{}
After that depending on IDE you need to refresh the classpath and download the dependency from the specified before repository, the transitive dependencies specified in the library dependency (in this case) will get fetched from test projects repositories{}
Library build.gradle
repositories {
mavenCentral()
}
dependencies {
module 'commons-codec:commons-codec:1.10'
module 'org.apache.httpcomponents:httpcore:4.4.6'
module 'org.apache.httpcomponents:httpclient:4.5.3'
}
test project build.gradle
repositories {
mavenCentral() repository to fetch transitives
mavenLocal() or any other repo that you published the library to
}
dependencies {
pref-conf librarygroup:name:version
}
You can use idea or eclipse plugin in gradle for gradle idea or gradle eclipseClasspath tasks to refresh it with your freshly added dependencies.
With this solution, you should not need to pack the transitive dependencies within the library,
PS. I am just confused after you said you want executable jar.
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
I have just spent hours trying to find out how to configure the AdobeCreative sdk for my android app because I wanted to add photo editing to my application.
The documentation was pretty good but they missed two crucial parts in there documentation which will give plenty of developers headaches. I'm going to answer my question below.
First follow all the documentation from https://creativesdk.adobe.com/docs/android/#/articles/gettingstarted/index.html
However, for your Project gradle.build, the documentation says:
Your Android Studio project contains by default two build.gradle files. In the Project build.gradle file, replace the allprojects block with the following code:
allprojects {
repositories {
jcenter()
maven {
url "${project.rootDir}/creativesdk-repo/release" // Location of the CSDK repo
}
}
}
Be sure to sync your project with the Gradle files after making any edits.
But you should really replace your files with:
allprojects {
apply plugin: 'maven'
repositories {
jcenter()
mavenCentral() //ADD THIS
maven {
url "${project.rootDir}/creativesdk-repo/release"
}
}
}
Notice I added mavenCentral() //ADD THIS.
Without this I received the following errors when doing my gradle build, and I was not able to import any of the classes needed to complete my gradle build:
Error:(38, 13) Failed to resolve: com.adobe.creativesdk.foundation:auth:0.7.329
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!