Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google Guava with Android 1.6

I'm using the Guava r09 library in an Android app but I'm getting the following error when I use it with 1.6. It's a bit strange because it works fine in 1.5. Also, the method that is "not found" is very clearly there. I also tried including the jsr305.jar file but this still doesn't work.

Appreciate any help, doing I/O in Java is so painful without Guava.

This is the code:

String timestamp = CharStreams.toString(
    CharStreams.newReaderSupplier(timestampFile, Charset.forName("UTF-8")));

This is the exception:

05-08 12:16:41.163: ERROR/dalvikvm(335): Could not find method com.google.common.io.LineReader.<init>, referenced from method com.google.common.io.CharStreams.readFirstLine
05-08 12:16:41.163: WARN/dalvikvm(335): VFY: unable to resolve direct method 6798: Lcom/google/common/io/LineReader;.<init> (Ljava/lang/Readable;)V
05-08 12:16:41.163: WARN/dalvikvm(335): VFY:  rejecting opcode 0x70 at 0x0009
05-08 12:16:41.163: WARN/dalvikvm(335): VFY:  rejected Lcom/google/common/io/CharStreams;.readFirstLine (Lcom/google/common/io/InputSupplier;)Ljava/lang/String;
05-08 12:16:41.163: WARN/dalvikvm(335): Verifier rejected class Lcom/google/common/io/CharStreams;
05-08 12:16:41.163: DEBUG/AndroidRuntime(335): Shutting down VM
05-08 12:16:41.163: WARN/dalvikvm(335): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
05-08 12:16:41.173: ERROR/AndroidRuntime(335): Uncaught handler: thread main exiting due to uncaught exception
05-08 12:16:41.183: ERROR/AndroidRuntime(335): java.lang.VerifyError: com.google.common.io.CharStreams
        at com.triposo.droidguide.rometest.LocationStoreInstaller.install(LocationStoreInstaller.java:33)
        at com.triposo.droidguide.rometest.SplashActivity.onStart(SplashActivity.java:58)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1205)
        at android.app.Activity.performStart(Activity.java:3520)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2373)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
        at android.app.ActivityThread.access$2100(ActivityThread.java:116)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4203)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
        at dalvik.system.NativeStart.main(Native Method)
like image 312
Jon Tirsen Avatar asked May 08 '11 02:05

Jon Tirsen


People also ask

What is guava JRE version?

Guava provides two different “flavors”: one for use on a (Java 8+) JRE and one for use on Android or Java 7 or by any library that wants to be compatible with either of those. These flavors are specified in the Maven version field as either 31.1-jre or 31.1-android .

What is guava library Android?

Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multiset), immutable collections, a graph library, and utilities for concurrency, I/O, hashing, caching, primitives, strings, and more!

What is Google Guava used for?

Guava is an open-source “Collection Library” library for Java, developed by Google. It provides utilities for working with Java collections. As you dive deep into Guava you'll notice how it reduces coding errors, facilitates standard coding practices and boost productivity by making code concise and easy to read.

What is Google guava dependency?

Guava is a suite of core and expanded libraries that include utility classes, google's collections, io classes, and much much more. Guava has only one code dependency - javax.


2 Answers

I managed to solve this in the end. If I use jarjar to rename com.google.common.io.LineReader to another package name (e.g. com.triposo.vendor.com.google.common.io.LineReader) it works! My guess is that LineReader is bundled in Android but with a slightly different (incompatible) version.

like image 128
Jon Tirsen Avatar answered Oct 31 '22 09:10

Jon Tirsen


This is why using undocumented Android API calls is bad. CharStreams was never part of the Android API (you don't see its removal being mentioned in the 1.6 changelog).

Undocumented classes or methods could disappear without notice from any OS revision, like it happened here.

like image 37
EboMike Avatar answered Oct 31 '22 09:10

EboMike