How do I easily include Google's GCS package into my code? - java

I want to use the Google Cloud Storage from Java in App Engine. The documentation on how to install this is very thin. I found the source code for com.google.appengine.tools.cloudstorage but when this source is put into the build path (Eclipse) it generates lots of compile errors. I then track down those sources, add them in and then get more compile errors from more dependencies. It seems like I am missing something about how to use this code in a much simpler way.

You can generate the library jar and also get its dependencies using Java Ant and the direction mentioned here or better yet, use maven and include appengine-gcs-client as a dependency.

If you don't use Maven, right-click on your project > Google > Add Google APIs > Cloud Storage JSON API.
This will add all the dependencies that you need. After that, the only jar you need is appengine-gcs-client.

Related

How to download Gradle dependency from java code?

My Java application needs to copy artifacts from Artifactory to AWS S3 temp bucket on demand (application group, name and version will be passed as parameters at runtime). The simplest way would be constructing URL and downloading files directly from Artifactory, but application should support 'latest.integration' and 'latest.relase' versions. So I want to add Gradle binaries to the classpath and use it to download dependencies.
Google results overflown with questions about dependency management using build.gradle.
So far I saw https://discuss.gradle.org/t/execute-gradle-task-from-java-code/21859/4 but looks like it also relies on preexisting build.gradle file.
So the question is: is there any way to use Gradle API from Java application to download certain lib without generating build.gradle file?
You should be able to use Ivy (http://ant.apache.org/ivy/) either as is or as a library itself to accomplish what you need, possibly with a bit of additional code.
(answer created from my comments)

Apply proguard to a Java Library Project

I have a java library project which contains a dependency to guava library. Guava has near 11k methods count, and I expect most of the users would came from the Android community. On Android there is a limit count method, it is 65k...
But the total count methods of my library is about 11.400, so my library's code is under 200 lines.
I was able to download and shrank a guava jar using proguard, reducing the count method number to 1k. But now the project needs to contain a reference to this shrank jar, instead a reference to the remote repository where the guava is hosted. But any jar added to the project would be discard by maven when it were published at any remote repository as an artifact, so the guava dependencies could not be resolved and the application client ultimately would crash.
Guava itself advices to not use proguard if your “application” is actually a library, and leave to the users of your library deal with this situation, using themselves proguard in order to shrank guava. But I don’t like this idea, because I would like to offer an easy configuration solution.
As far as I know, the output that proguard provides is some sort of executable (jar, apk, etc), so, If I shrank my own library, the final output would be a jar, and this jar, again, could not be published as an artifact, because it would be discarded (I tried it several times).
Is there any way of using proguard in my own java library project and pass the resulting output to the build chain in order to be published as a remote repository, not as a jar?
I’m using gradle by the way to build my project, but at this point I would be up to move to a maven one it that solves the problem.
Thanks.
Do one of the followings:
Use your shrank version of guava as separate maven artifact, publish it, and let your lib depend on it as on any other dependency
Do not shrink the library and use multidex build - this is the
method to solve 65K method limiation
http://developer.android.com/tools/building/multidex.html
Anyway as for ease of configuration you should not use progruard on your library for a simple reason: User will have to add dependency to its project anyway. And what if in some cases users will start having ClassNotFound exception because you have truncated some of the code that you didnt expect it can be used?
If somebody is programing for Android he will sooner or later bang into ProGuard, and I think sooner is better.
So as for ease of configuration, I would rather suggest in the documentation, that if user wants to avoid 65k limitation because your library is already exceeding it, he can use proguard in provided example configuration.

Android Library Project without sources

I've created Android Library Project which depends on three libraries : google-play-services_lib, android-support-v7-library and pull-to-refresh ( https://github.com/chrisbanes/Android-PullToRefresh/blob/master/library/src/com/handmark/pulltorefresh/library/PullToRefreshBase.java )
I marked my project as library and want to create jar which can be used as jars are used is google_play_services and android-support library. My question is what I have to do to be sure that libraries that my project depends on are not included in my jar. I want 3rd person - who will use my library - to add them with my projectes as dependencies and get it work.
Unchecked export options in java build path is enough?
I don't know exactly if you have used Gradle or not. But if it's the former, then you don't have to do nothing. If you have include your dependencies in your library project like this:
dependencies {
compile 'whatever:library'
}
Then it's ok.
Note: If your project includes resources (like layouts and more) it can not be compiled as a jar!
Note2: If you didn't use Gradle, notify me in the comment and I'll update this post explaining how to achieve that.
UPDATE 1:
First of all, let me link you to another related answer I did the last week. It gives you useful links to learn Gradle in a hurry (at least all the basics).
Second, do you use Android Studio?
UPDATE 2:
It's just a matter of taste ;)! But with Android Studio (Intellij) you don't need to configure the extra step to get Gradle working.
Well, after you have read those links I've put before, the next step is to create a library project (in Android Studio it will generate the required folders/files automatically for you).
In your lib or library folder (where you put your source code of your library) in your build.gradle put the following:
dependencies {
compile 'com.google.android.gms:play-services:4.4.52'
compile 'com.android.support:gridlayout-v7:19.1.0'
compile 'it.sephiroth.android.library.fork.actionbarpulltorefresh:actionbarpulltorefresh:0.9.92'
}
And with that you can generate your library in aar format.
NOTE: Android-PullToRefresh library is not Gradelized, so I had to search for another alternative that has been uploaded to MavenCentral (like sephiroth's one). Remember to use Gradle, please! To search already Gradelized libs!

Why I can't use a library in my Android project from Maven repository? How to write a correct pom.xml for this?

I want to use Jackson JSON parser library in my android project. I saw this library in Maven repository, but I don't know how to use it. I've downloaded sources from the Maven repository and Jackson jars and attached sources to jar, but in the logcat I saw error message NoClassDefFoundError. When googling I' ve read that I have to declare Jackson dependencies in pom.xml file.I' m a newbie in Java development so I don't know what all these means. And have some questions:
1.How to write pom.xml for the Jackson library
2.Where to put this pom.xml
3. Do I really need to install Maven if I just want to use the library.
4. What else I need to begin work with the library?
No, you do not need to write a pom file, unless you are using Maven for building (in which case you need it regardless of Jackson).
What you need are just Jackson jars -- there is more than one, since some projects only need some pieces. This page:
http://wiki.fasterxml.com/JacksonDownload
should show what you need, and where to get them from. If you are starting from scratch, I would strongly recommend using Jackson 2.1 (not 1.9). And then you most likely need 3 jars (jackson-annotations, jackson-databind, jackson-core) -- although minimal is just jackson-core, if you use so-called "streaming API" (low-level, highest performance, but more work).
The benefit of using Maven would be just that you can define logical depenendency (group and artifact id of jar), and Maven would resolve it to physical jar, as well as references to other jars.

Where to check out SLF4J Simple?

I'm trying to check out slf4j-simple-1.6.2 from a trusted repository (preferably, SLF4J's official repo) and pull it down into an Eclipse project. I'm doing this because I need to tweak SLF4J Simple's code so that it binds to my own logging implementation.
I'm hoping there is a way to do this without having to use Maven, because I've never used Maven before and feel much more comfortable running Ant builds.
Nevertheless, I've searched SLF4J's site high and low and cannot find any trusted links to their repository.
Even once I get the project imported into Eclipse, I still need to figure out how to get it building with Ant.
Could someone please help me:
Find the repo
Confirm whether an Ant build is possible
Thanks in advance!
The zip download here also contains the sources.
The official source code repository is hosted on GitHub. However, I believe you are doing it the wrong way.
The idea of SLF4J is to have a dependency on slf4j-api and let the developer to add exactly one binding. Instead of tweaking original bindings just write your own one. Of course you can use simple binding a starting point, but modifying existing open source libraries and maintaining patched versions is a lot of work.
As you said, slf4j is present in the official Maven repository.
So basically, you have 2 simple solutions without using Maven:
Download the JAR / sources / javadocs from this Maven repository, and copy them in your own project directory.
Use Ivy. This is an extension of Ant to give a better dependencies management. It can connect to Maven repositories. So you will be able to retrieve your slf4j dependency without having to use Maven.

Categories