Android Studio: JavaSE app - java

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.

Related

Java jar file won't recognize external libraries added from gradle

Yo folks basically I'm using gradle in java project and can't export the libraries in jar file that I'm using.
Tried a few solutions but nothing worked.
Do you know what I'm missing in the gradle file or I need to specify some things when I'm exporting. I'm using Eclipse
Thanks, here is my gradle file
enter code here
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:27.0.1-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
implementation "redis.clients:jedis:3.0.1"
implementation 'org.pixsee.java-fcm:Java-fcm:1.0.0'
implementation 'com.google.firebase:firebase-admin:6.10.0'
compile "org.slf4j:slf4j-api:1.6.1"
implementation 'org.slf4j:slf4j-simple:1.7.25'
implementation "com.google.maps:google-maps-services:0.9.4"
implementation 'io.vertx:vertx-core:3.8.1'
}
sourceCompatibility = 1.8
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'GeofenceServer',
'Implementation-Version': version
}
}
apply plugin: "eclipse"
Finally solved it , the answer from Sterconium got me on the right track
answer but the problem was when I try to create the fatJar it says cannot find the main class ,the reason was because my files are in src/test/java instead of src/main/java and somehow when I tried to run fatJar it compiled It but could not find still the dependencies, so I change the implementation to compile in build.gradle file and now it works .So here is my final build.gradle file how it looks like .
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* User Manual available at https://docs.gradle.org/5.4/userguide/java_library_plugin.html
*/
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}
apply plugin: "java"
apply plugin: "eclipse"
version = '1.0'
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'Server.Test'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:27.0.1-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
implementation "redis.clients:jedis:3.0.1"
implementation 'com.google.firebase:firebase-admin:6.10.0'
implementation 'org.slf4j:slf4j-simple:1.7.25'
implementation 'com.google.maps:google-maps-services:0.10.0'
compile 'io.vertx:vertx-core:3.8.1'
implementation 'com.google.code.gson:gson:2.8.5'
}

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/'
}
}

Gradle: Both Java and Android Subprojects on Same Project

TL;DR: IS the only way to develop both pure Java and Android applications, is on completely different Gradle projects?
I am developing a project which includes an Android application and a Java backend (along with some other common APIs).
As an IDE I am using IntelliJ Idea.
I would like to have a single Gradle project (which will be opened on a single Idea instance), that contains all the applications as subprojects.
My problem is that in order to allow the Android plugin, I need to set it in the buildScript section in build.gradle file:
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
}
This forces the android plugin in the entire Gradle build process, and causes errors on the pure Java subprojects (they are automatically set to be built with the Android SDK instead of the Java SDK).
A workaround I have thought of is creating two separate projects (one for Android and one for Java), export the common JARs into a local Maven repository and import it from there into the Android project.
Is there a better solution that will allow me to have all the code base in the same place?
What you are asking for is possible: you just need to move the buildscript declaration from the project to the android module.
The PROJECT build.gradle may look like this:
buildscript {
// nothing for the project
}
allprojects {
repositories {
jcenter() // you could also move this to each module
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
And the Android MODULE build.gradle may look like this:
apply plugin: 'com.android.application'
// other pluguins
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
// other dependencies for the build (apt, retrolambda...)
}
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "my.android.app"
// additional config...
}
}
dependencies {
// your app dependencies here
}
Other modules, such as backend or web, may declare their own buildscripts with their own pluguins, repositories and dependencies.
Each project has it's own buildscript classpath
root/build.gradle
project(':java-project') {
apply plugin: 'java'
}
project(':andoid-project') {
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
}
}
apply plugin: 'android'
}
I'm not an android user but perhaps you need to tweak the JavaCompile tasks on the java only projects such that the compiler is forked and uses a different javac
allprojects {
if (plugins.hasPlugin('java') && !plugins.hasPlugin('android')) {
tasks.withType(JavaCompile) {
options.fork = true
options.forkOptions.executable = '/path/to/javac'
}
}
}

Error:Configuration with name 'default' not found when building for Sony Smart Glass

I am trying to develop an app for Sony Smart Glass in Android Studio 2.1.2. I tried to add the Sony Smart Glass sample library functions. I am getting an error like that
Error:Configuration with name 'default' not found.
My settings.gradle:
include ':app'
include ':libraries:mylib'
My 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:2.1.2'
// 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
}
The best starting place to create a SmartEyeglass app is with one of the samples. They already have all of the necessary dependencies declared for you. As a basic example try starting with the "HelloWorld" sample. In the end though your settings.gradle file should include this:
includeFlat 'SmartExtensionAPI'
includeFlat 'SmartExtensionUtils'
includeFlat 'SmartEyeglassAPI'
And your build.gradle file should include this
compile project(':SmartEyeglassAPI')
compile project(':SmartExtensionAPI')
compile project(':SmartExtensionUtils')
Please let me know if that helps.

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!

Categories