JAR files not being included with Android Ant build - java

It seems like there are many different answers to this question, none of which work for me!
When I build my android app with eclipse, everything works fine. My JAR file is located in the /libs directory of my project, as recommended.
I am using SDK r15 (the latest android sdk available).
When I run ant debug, the apk file is built. However upon running the file in the emulator, a force quit is thrown. From LogCat, I see errors consistent with the JAR file not being compiled.
Many of the answers I see here, refer to older versions of the SDK. Also many refer to files which no longer exist. For example, android_rules.xml no longer exists in the ../tools/ant/ directory. Instead I see only a build.xml.
Has anyone solved this issue for the latest android.
Not I MUST use ant (not eclipse) for this build..

If you use ant to build your android project, just add your jar to the libs project. Eclipse will need you to add it through the project properties wizard. Maven should have some archteyp for that but I don't know it.
An extra jar file needs to be both added through dex and apkbuilder, that's not that easy to do it buy hand, you would be more confortable using ant tools coming with the android sdk, they are pretty well done and provide intersting features like adding jars, build a project for debug and release, testing,etc..
add your jar to a folder named libs and run ant compile from the project basedir.

Related

Add AWS SDK to IntelliJ project without Maven or Gradle

I'll start this off by saying that I'm not a Java dev, I work in a .NET software house, but somehow we have ended up needing to modify some legacy java code.
So I have a project which I've opened up with IntelliJ IDEA Ultimate 2016.2. As far as I can tell this doesn't use Maven or Gradle or any other dependency management tool, and as I have no idea what I'm doing with Java projects I'm not going to start trying to understand that now.
I'm trying to add the Amazon AWS SDK to my project so I can upload some files to an S3 bucket. I've downloaded the SDK from the AWS site, and extracted the jar files into the lib folder of my project root. I've tried numerous ways of adding these as dependencies, right click the jar files and add as library, go into settings, modules, dependencies, click the plus and add the jars etc. When I build the project it builds fine, and intellisense if picking up the classes and methods when writing the code to utilise the SDK.
However, as soon as I run the app, I get a "java.lang.NoClassDefFound" for the com/amazonaws/AmazonServiceException package as the containing class is instantiated.
This could well be something to do with the "CLASSPATH" that I hear so much about (but don't fully understand), but my understanding was that IntelliJ handled this when adding the jars as a dependency. I've also tried adding all the jars from the "third-party" directory from the downloaded AWS zip file, and adding those as dependencies as well, but it still doesn't want to work.
Incidentally, I'm adding the jars to the lib folder so they are included in source control.
Any suggestions on how to use the AWS SDK without using Maven or Gradle?
Cheers
What I've done is to manually add all the jar files into the External Libraries.
Go to File > Project Structure
In the left window pane, choose Modules under Project Settings
Click on the Dependencies tab
Click on the + sign and select 1 JARs or Directories
Navigate to the folders containing the jar files (include third-party dependecies also) and click OK
See My IntelliJ screenshot

Android Eclipse NoClassDefFoundError for external .jar android files

I am facing Error:java.lang.NoClassDefFoundError when importing a jar and using its classes.
This problem might be thought of a duplicate to this question. But I have a different condition:
I have prepared a .jar file from Eclipse SDK for Android .so it has folder such as /res /assets etc.
I have goggled for solution tried some of the solutions:
Build path-> Configure BuildPath->Add External jar
Copy .jar to /libs directory.
None of them worked!!!
P.S. In a previous project i have already used SQLCipher.jar file by just by adding it to the build path and have faced no problems.Though It had some filename.dat (which i have to add into /assets folder) and i have no idea what so ever if that has any role in accessing methods in the class of the jar.
I strongly recommend using Android Studio, the gradle build system is much easier than adding the libs manually. Just add compile 'net.sqlcipher:sqlcipher-gradle:3.0.1' to the build script, and the library will be downloaded and added with 1 mouse click.
Eclipse is missing powerfull features that Android Studio has
On toppic:
You need to import the jars from the build config dialog, click import external jar and select your jar
The library you added may need some external library eg google play services or something, do check that

Adding plain Java project as a classpath to an eclipse plugin

I have a plain Java project (not a plugin project) which I want to add to a classpath of a eclipse plugin which I am developing. But in web projects I can add that project as a build path and it works fine. But I tried same thing in eclipse plugin, I am able to compile successfully, but at run time I am getting java.lang.ClassNotFoundException.
I know OSGi quite well and I know how to add OSGi into an classpath (using export-packages) but what I want is to add Standard, non-osgi project into an classpath, so that I wont' get runtime errors. Is there anyway I can achieve this?
I can export project as a jar file or make it as a plugin project and it would work fine. But that's not my option currently because, still that API is in pre-alpha stage, and there would be lot of changes going on. So I am trying to avoid pain of exporting it as jar file everytime. Is there any option for me other than this?
I have a similar situation: I want non-OSGi Maven dependencies integrated into the classpath of my plugin. I succeeded with a roundabout solution, which I think is the best I could get.
I have a build step outside of Eclipse where I copy the class files of the dependency into the plugin's lib folder. The lib folder is specified in MANIFEST.MF as an entry in Bundle-ClassPath and (here comes the hack) as a source folder in build.properties. That was the only way to make the plugin work both when launched from within Eclipse and when exported.

Eclipse - External JARs and git

I am currently using eclipse for working with Java. Additionaly I use git to synchronize my project between my laptop and my desktop PC.
The problem is now the following: I added external JARs to the project (Slick-Util, LWJGL).
But the path to each library is another on each device. So everytime I start working on the other device, I have to change the path to the jar files and the javadocs.
The libraries are all stored in my eclipse workspace. So the libraries and the projects are all in the same folder. And this folder is also commited with git.
Is there a way to change the eclipse settings (or do something else) so I do not have to change the path to the libraries and javadocs everytime?
I already googled and searched for it, but I could not find something about it.
Just don't add the libraries' jars to git. There are multiple build tools for java, which manage dependencies for you - you just state the libraries you're going to use, and the build tool downloads it for you at build time.
I would recommend Gradle, but Maven is also a very popular choice.
In gradle, you would create a file build.gradle, and define your dependencies in it:
apply plugin: 'eclipse'
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.lwjgl.lwjgl:lwjgl:2.9.0'
compile 'org.lwjgl.lwjgl:lwjgl_util:2.9.0'
}
Then you would run gradle eclipse from the command line - that would add the libraries you use to the classpath in eclipse. And when you want to compile and package your project you would run gradle build from the command line. You should read about it if you're going to use it, what I describe may not be exactly what you need.
Also, there are instructions for using LWJGL with maven.
add jar files to a lib folder inside your project like this : D:\Workspace\myproject\lib\your-jar-file.jar
then go to your projects build path select libraries tab and click on add jars and NOT add external jars this way your jar files path will be relative to your project
EDIT :
I highly recommend to use a build tool as Kiril Raychev described.
it will look a bit confusing to start with but after a while and after a normal growth in your application that will lead to using different frameworks, controlling and managing dependencies and their conflicts without a build tool will literally kill you.
You can simply use -f flag on add command.
git add -f test.jar
And, then commit and push to your repo.
Up until now i usually use svn so i'm not entirely sure how it works out in git, but have you tried to store the JARs in the lib folder of the project they are used in? (Eclipse displays the lib folder so you can easiely add them to the buildpath with a right click on the library in the package explorer.)
That way the relative location/path of the libraries to the project should stay the same. Furthermore if you plan to pack the project into a JAR later you ship the libraries inside that JAR without having to worry whether the user of that file even has them on his computer.
PS: Looks like i'm a minute too late. Dave basically said the same thing.

Change output folder for Android R.java in Eclipse when using Maven

I have an android project setup using the maven-android plugin. This plugin adheres to maven output folders for the location of the R.java file (i.e. target/generated-sources/r).
However, when using this project in Eclipse, I can't configure aapt to rely on that folder.
Even if I manually go into the project properties and set up the folders manually, the ADT plugin goes in and resets just some of the settings (i.e. adds the /gen folder back as a source folder, and restores the output folder to /bin/classes instead of /target/classes), while leaving the maven /target/generated-sources/r folder and breaking the build of the project.
I would like to use the default Maven folder structure with the Eclipse ADT. Any ideas on how I can do this?
Thanks
Responding to the comments:
Yes, am using the m2e-android plugin.
Because ADT is still generating R.java in /gen, Eclipse is failing to compile if there still exists an R.java under target/generated-sources/r due to duplicated class definitions.
Unfortunately, the Android Connector for M2E does not support moving generated folders like the ADT gen folder to other directories. This is a restriction of the ADT, although there is a ticket to explore a way around this restriction:
https://github.com/rgladwell/m2e-android/issues/68
If you'd like to stay updated, please comment on this ticket.
This seems to be working for me:
Let the ADT keep gen as a source folder, but change the output folder for that source folder to be /target/classes.
I can build in both eclipse and using maven from the command line, and I don't get duplicate R class problems in either case. Essentially, Eclipse generates the R.java file in /gen, while maven generates it in /target/generated-sources/r, but both builds put the R.class file under /target/classes.
Note: at one point I had added /gen as an additional source directory in my pom.xml using build-helper-maven-plugin, and I had to remove that.

Categories

Resources