I have taken the following steps to migrate to Android Studio but right now I don't know what is wrong.
In Eclipse I exported the gradle file. I did this because importing in Android Studio is not an option since it create a copy of the project and my project is under git.
In Android Studio, I did "Open an existing Android Studio Project" and choose the gradle file.
Since my original project used Maven for Maven dependencies, I add all of these to the gradle file.
Results: Most of the content in the build.gradle file is grayed out, telling me it cannot resolve symbol [name]. When I build, I have more than 100 errors, all of them related to dependencies not present (gradle not doing it's job). Also, I have been using gradle project in the last couple of days and the file is normally always asking to sync and it build the gradle file before building your project. In my case, nothing of that. It's like my project don't know he needs to use gradle or gradle is not configured the right way. Here is my gradle file and some screenshot to help you understand my problem. Any help is appreciated.
build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 15
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "XXXXXX"
minSdkVersion 15
targetSdkVersion 15
testApplicationId "XXXXXXX"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
}
packagingOptions {
exclude 'META-INF/notice.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/java']
aidl.srcDirs = ['src/main/java']
renderscript.srcDirs = ['src/main/java']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
dependencies {
compile group: 'com.google.android', name: 'android', version: '4.1.1.4' //provided
compile group: 'org.springframework.android', name: 'spring-android-core', version: '1.0.1.RELEASE'
compile group: 'commons-io', name: 'commons-io', version: '1.4'
compile group: 'org.springframework.android', name: 'spring-android-rest-template', version: '1.0.1.RELEASE'
compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.13'
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.13'
compile group: 'com.google.code.gson', name: 'gson', version: '2.3'
compile group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.3.0'
compile group: 'joda-time', name: 'joda-time', version: '2.3'
compile group: 'net.erdfelt.android', name: 'apk-parser', version: '1.0.2'
compile group: 'org.apache.commons', name: 'commons-compress', version: '1.9'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-core', version: '1.10.8'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '1.6.1'
testCompile group: 'org.powermock', name: 'powermock-api-mockito', version: '1.6.1'
}
Screenshot of what it looks like in android studio:
No gradle subfolder in Android View? Is it normal?!
Thank you!
EDIT
I did like suggested in the comment. I seem to be a step further but I'm having the message: "Gradle project sync failed. Basic functionality will not work properly". In the message below, I have this. What can I do?
Error:Unable to load class 'org.codehaus.groovy.runtime.typehandling.ShortTypeHandling'.
Possible causes for this unexpected error include:<ul><li>You are using JDK version 'java version "1.7.0_75"'. Some versions of JDK 1.7 (e.g. 1.7.0_10) may cause class loading errors in Gradle.
Please update to a newer version (e.g. 1.7.0_67).
Open JDK Settings</li><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)</li><li>The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)</li></ul>In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
If this can help anyone out there, here are the steps I followed to do the migration.
Exportation of the build.gradle file
Go in the Eclipse ADT project and click on File/Export. Choose the
"Android" folder and the option "Generate Gradle build files".
Follow the wizard and only select the project you want to export.
You should have your original project with a new build.gradle file at
the root.
Converting Maven dependencies to gradle
If you are using Maven dependencies, all of your dependencies looks like that:
<dependency>
<groupId>org.springframework.android</groupId>
<artifactId>spring-android-core</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
You need to convert it to this format:
dependencies {
compile group: 'com.google.android', name: 'android', version: '4.1.1.4'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
As you can see, if the dependency is needed to compile, put the keyword "compile" in front of it. If it's for the testing, use the keyword "testCompile".
Keep that in a temporary file for the moment.
Installing Android Studio and configuring it
Install Android Studio from this location: http://developer.android.com/sdk/index.html
While it download/install, open your previously generated build.gradle file and change the following things. Replace
apply plugin: 'android'
With
apply plugin: 'com.android.application'
Be sure that your "buildScript" code-block looks like that
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
Finally, add the repositories and dependencies information outside of the android code block.
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.android:android:4.1.1.4'
...
}
Now that Android Studio is installed, open it. Choose "Import Project (Eclipse ADT, Gradle, etc)" and navigate to the build.gradle file.
Android Studio should now create all the metadata and file needed to run the project. If there is an error regarding the version of the tool on your compute, go back to the intro screen of Android Studio and choose "Configure/SDK Manager". Download whatever is necessary for your project to work.
When in the project, Go in "File/Project Structure". In the project tab, be sure that the gradle version is 2.2.1 and that the android plugin is 1.1.0.
The next step should be to create a configuration and run it on the USB device available or Emulator.
You are good to go!
Related
I am trying to build a java project using gradle6.6 and open jre 11.0.2, in eclipse 03-2020.
I am facing compilation issue, while building my project which uses the classes from jrt-fs.jar; e.g.'sun.net.www.protocol.http.Handler', which is in open jre 11.
public class Manager extends sun.net.www.protocol.https.Handler{
(package sun.net.www.protocol.https is declared in module java.base, which
does not export it)
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
Following is my build.gradle:
apply plugin: 'java'
apply plugin: 'application'
allprojects {
apply plugin: 'java'
sourceCompatibility = '11.0.2'
targetCompatibility = JavaVersion.VERSION_11
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'lib', include: '*.jar')
compile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
compile group: 'junit', name: 'junit', version: '4.10'
compile group: 'jmock', name: 'jmock', version: '1.0.1'
compile group: 'com.google.code.findbugs',name: 'findbugs', version: '1.3.9'
compile group: 'cglib', name: 'cglib', version: '2.1'
testCompile group: 'junit', name: 'junit', version: '4.10'
}
also, how can I avoid building the test classes, through gradle?
As #dave_thompson_085 suggested, Issue is due to modular restriction (JDK-1.9 onwards) on sun.net.* packages , as they are internal to JDK. The above packages are accessible in JDK -1.8. We need to fix our code
If you are using gradle, add this compilerArgs in build.gradle
options.encoding = "UTF-8"
options.incremental = true
options.compilerArgs.addAll([
"--add-exports",
"java.base/sun.net.www.protocol.http=ALL-UNNAMED"
])
}
I have a Gradle project with some third party dependencies.
My jar has been working fine until I added SQLServer dependency.
Here is a snapshot of build.gradle:
group 'MyApp'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.amazonaws:aws-java-sdk:1.11.60'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'
compile files('mylibraries/ojdbc7.jar')
compile files('mylibraries/postgresql-42.1.4.jar')
compile files('mylibraries/mssql-jdbc-6.2.1.jre8.jar')
}
jar {
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Main-Class': 'MainLauncher'
}
}
Everything breaks down after compile files('mylibraries/mssql-jdbc-6.2.1.jre8.jar') has been added to dependencies. The error I get:
Error: Could not find or load main class MainLauncher
What could be a potential problem? Thank you!
Today i faced exactly same issue and i reached to this page to get the resolution
I was running jar with below dependency
group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '6.2.2.jre8'
and i was facing exactly same issue,
Error: Could not find or load main class
later i changed dependency with older version and it run fine after this change
group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version:
'6.2.1.jre7'
I've had the same problem some time ago. I just imported the project and wanted to do a gradle clean build. I had exactly the same error.
I could solve this by just making sure I had at least one migration. It may sound silly but just try to create 1 migration for your database.
At least it solved my problem and I surely hope it solves yours too!
tl;dr; adding adding dependencies to build.gradle downloads it fine but doesn't add it to the classpath/external libraries in idea.
Hi guys
Im new to developing webapps in java, and im trying to depend on a few jars on mvnrepository.com, the only time the dependencies are downloaded into the external libraries and added to the classpath is when i import the project as a gradle project, as in, each time i have a project up and running and i add a new dependency i would have to import the whole project into intellij again.
my build.gradle file looks like this:
group 'project_name'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/com.google.inject/guice
compile group: 'com.google.inject', name: 'guice', version: '3.0'
// https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core
compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '9.0.0.M9'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-core
compile group: 'com.sun.jersey', name: 'jersey-core', version: '1.19.1'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-json
compile group: 'com.sun.jersey', name: 'jersey-json', version: '1.19.1'
// https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client
compile group: 'org.glassfish.jersey.core', name: 'jersey-client', version: '2.23.2'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-servlet
compile group: 'com.sun.jersey', name: 'jersey-servlet', version: '1.19.1'
// https://mvnrepository.com/artifact/com.sun.jersey/jersey-server
compile group: 'com.sun.jersey', name: 'jersey-server', version: '1.19.1'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.5'
}
When i add a new dependency to the list, and run ./gradlew build, with or without the --refresh-dependencies option it does download the new dependencies but it doesn't add the downloaded files to the external libraries/classpath so i can't import them into the java code. I saw a question similar to this one, where they accepted answers like running:
./gradlew idea
In my case this doesn't help at all, it just adds some autogenerated files in the directory with no clear difference to behavior.
Then they accepted importing the project as a gradle project aswell, which i have done - which works, but adding new dependencies doesn't work.
FYI I am using the gradle 2.5 wrapper and IDEA community 16.2
Okay. I solved/figured it out, Apparently it didn't help to just run build,
inside of intellij i had to go to View --> Tool Windows --> Gradle, it then opens the gradle window, where i could click the refresh button, which downloads the dependencies.
Thanks to anyone who looked it over :)
I'm trying to configure gradle to use lombok to compile my project but I don't want the classes appearing in my jar. On the other side I need the mysql-connector dependency packages in the jar, but it's not needed for compiling. This is my build.gradle file:
group 'de.albritter'
version '1.0-SNAPSHOT'
apply plugin: 'java'
jar {
manifest {
attributes 'Main-Class': 'de.albritter.main.Main'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
sourceCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.39'
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'mysql', name: 'mysql-connector-java', version: '5.1.39'
classpath group: 'org.projectlombok', name: 'lombok', version: '1.16.8'
}
}
I've seen some solutions using compileOnly but if i try to use it I just gete an error that this method is not known.
My gradle version is 2.9
How do I tell gradle that I don't need lombok in my jar?
What you're asking for is variously known as compileOnly or in the maven world, provided dependency. The compileOnly configuration was introduced in gradle in version 2.12. I would strongly recommend moving to the latest version of gradle (2.14 at the time of writing this).
If you need to stick to the older version, there are some workarounds you can find by looking for "gradle provided dependency". One way to do this is to declare your own configuration, lets call it provided and adding its dependencies to compile time classpath. So in your build.gradle:
configurations{
provided
}
sourceSets {
main.compileClasspath += configurations.provided
test.compileClasspath += configurations.provided
test.runtimeClasspath += configurations.provided
}
dependencies {
...
provided 'group:module:version'
...
}
Or alternatively you can use the prodeps plugin which does most of this work for you.
This question already has an answer here:
How to use Buildship with existing Gradle projects
(1 answer)
Closed 6 years ago.
I need to use eclipse on a project and gradle for my testing. orginally my testing was done with testng and maven. I first did the gradle init and it all seems to work fine. I normally use intellij and got no problem running my project. But when I import my project in eclipse it keeps saying "the import .... could not be resolved" on every import I do.
this is my build.gradle. Keep in mind I only did the gradle init so if I'm doing something wrong at this point I just don't know
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
group = 'be.test'
version = '1.1-SNAPSHOT'
description = """"""
repositories {
maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version:'2.52.0'
compile group: 'org.uncommons', name: 'reportng', version:'1.1.4'
compile group: 'org.apache.velocity', name: 'velocity', version:'1.7'
compile group: 'com.google.inject', name: 'guice', version:'4.0'
testCompile group: 'junit', name: 'junit', version:'3.8.1'
testCompile group: 'org.testng', name: 'testng', version:'6.9.4'
}
Is it a gradle problem, or am I just missing something in eclipse what is automatically done in intellij?
I found out after looking closer into the tasks of gradle you can do
gradle cleanEclipse
and after that
gradle eclipse
After that I reloaded the project and all was fine