Method not found using DigestUtils in Android - android

I am trying to use the library DigestUtils in Android 2.3.1 using JDK 1.6, however I get the following error when executing the app:
Could not find method org.apache.commons.codec.binary.Hex.encodeHexString, referenced from method org.apache.commons.codec.digest.DigestUtils.shaHex
Here you have the stacktrace:
02-03 10:25:45.153: I/dalvikvm(1230): Could not find method org.apache.commons.codec.binary.Hex.encodeHexString, referenced from method org.apache.commons.codec.digest.DigestUtils.shaHex
02-03 10:25:45.153: W/dalvikvm(1230): VFY: unable to resolve static method 329: Lorg/apache/commons/codec/binary/Hex;.encodeHexString ([B)Ljava/lang/String;
02-03 10:25:45.153: D/dalvikvm(1230): VFY: replacing opcode 0x71 at 0x0004
02-03 10:25:45.153: D/dalvikvm(1230): VFY: dead code 0x0007-0008 in Lorg/apache/commons/codec/digest/DigestUtils;.shaHex ([B)Ljava/lang/String;
02-03 10:25:45.163: D/AndroidRuntime(1230): Shutting down VM
02-03 10:25:45.163: W/dalvikvm(1230): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-03 10:25:45.173: E/AndroidRuntime(1230): FATAL EXCEPTION: main
02-03 10:25:45.173: E/AndroidRuntime(1230): java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Hex.encodeHexString
02-03 10:25:45.173: E/AndroidRuntime(1230): at org.apache.commons.codec.digest.DigestUtils.md5Hex(DigestUtils.java:226)
02-03 10:25:45.173: E/AndroidRuntime(1230): at com.caumons.trainingdininghall.ConnectionProfileActivity.onCreate(ConnectionProfileActivity.java:20)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.os.Looper.loop(Looper.java:123)
02-03 10:25:45.173: E/AndroidRuntime(1230): at android.app.ActivityThread.main(ActivityThread.java:3647)
02-03 10:25:45.173: E/AndroidRuntime(1230): at java.lang.reflect.Method.invokeNative(Native Method)
02-03 10:25:45.173: E/AndroidRuntime(1230): at java.lang.reflect.Method.invoke(Method.java:507)
02-03 10:25:45.173: E/AndroidRuntime(1230): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-03 10:25:45.173: E/AndroidRuntime(1230): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-03 10:25:45.173: E/AndroidRuntime(1230): at dalvik.system.NativeStart.main(Native Method)
The line of code which causes the exception is:
String hash = DigestUtils.shaHex("textToHash");
I have executed the same code in a Java class outside Android and it works! So, I do not know why when working with Android it does not work... I put the libraty inside a new libs/ folder in my app and updated the BuildPath to use it. If I try to use md5 instead of sha1 I get the same exception. Any help would be appreciated! Thank you.
UPDATE:
As this is a very active question, I've changed the accepted answer in favour of #DA25, as his solution is straightforward and the high number of upvotes prove that it works.

I ran into the same issue trying to use DigestUtils in my Android app. This was the best answer I could find by searching, but I was reluctant to rebuild the .jar file with the namespace changed. After spending some time on this issue, I found an easier way to solve the problem for my case. The problem statement for my code was
String s = DigestUtils.md5Hex(data);
Replace this statement with the following and it will work:
String s = new String(Hex.encodeHex(DigestUtils.md5(data)));
Similarly, for shaHex exampl, you can change it to
String hash = new String(Hex.encodeHex(DigestUtils.sha("textToHash")));
This works because even though Android does not have encodeHexString(), it does have encodeHex(). Hope this would help others who run into the same issue.

Since there's no clear answer for the root cause of this problem, I'd like to clarify what's happening here.
Why the NoSuchMethodError is thrown in the first place?
According to exception stack trace, the line that causes the fault is 226 in DigestUtils#md5hex method. Let's see what we have there (I'm assuming you have used version 1.4, since this is the only release where Hex#encodeHexString method is being invoked in line 226):
public static String md5Hex(String data) {
return Hex.encodeHexString(md5(data));
}
The exception says java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Hex.encodeHexString.
Let's understand why.
First of all, Android framework already includes the Commons Codec library (except the DigestUtils class). Yes, it is not exposed as part of the Android SDK and you cannot use it directly. But you still want to use it. So what you do? You add Commons Codec library as part of your application. The compiler doesn't complain - from his point of view everything was fine.
But what happens at runtime? Let's follow your exception stack trace:
First, you're calling DigestUtils#md5Hex from your Activity's onCreate method. As I wrote above, the framework doesn't include that class, so DigestUtils (from Commons Codec version 1.4) is loaded from your dex.
Next, md5hex method tries to invoke Hex#encodeHexString method. Hex class is part of the Commons Codec library that included in framework. The thing is that its version is 1.3 (ancient release from July 2004). Hex class exists in boot classpath, which means that the runtime will always favor it instead of the Hex class that packaged inside your dex. You can see warnings about it in your application logs when you start your app (with Dalvik runtime):
D/dalvikvm? DexOpt: 'Lorg/apache/commons/codec/binary/Hex;' has an earlier definition; blocking out
I/dalvikvm? DexOpt: not resolving ambiguous class 'Lorg/apache/commons/codec/binary/Hex;'
D/dalvikvm? DexOpt: not verifying/optimizing 'Lorg/apache/commons/codec/binary/Hex;': multiple definitions
I/dalvikvm? Could not find method org.apache.commons.codec.binary.Hex.encodeHexString, referenced from method org.apache.commons.codec.digest.DigestUtils.md5Hex
Hex#encodeHexString method was introduced in version 1.4 of Commons Codec library and therefore it doesn't exist in framework's Hex class. The runtime can't find this method and thus throws NoSuchMethodError exception.
Why the accepted answer's solution works?
String s = new String(Hex.encodeHex(DigestUtils.md5(data)));
First, DigestUtils#md5 method is called. As I already stated, DigestUtils class that will be used is the one that packaged in your dex. This method doesn't use any other Commons Codec classes, so no problem with it.
Next, Hex#encodeHex will be called. The Hex class that will be used is the framework's one (version 1.3). The encodeHex method (that takes a single parameter - byte array) exists in version 1.3 of Commons Codec library, and therefore this code will work fine.
What would I suggest?
My suggested solution is to rename the classes namespace/package. By doing so I'm explicitly specifying which code is going to execute, and prevent bizarre behavior that may occur because of versioning issues.
You can do it manually (as Caumons wrote in his answer), or automatically with jarjar tool.
See this issue summary and tips for using jarjar in my blogpost.

Finally I get the answer and it works well. As described in No such method error in Apache codec for another type of encrypt (Base64) I tried to reproduce the same issue and I get exactly the same error. So I was in the case of the question attached. As they say, it seems to be an internal name collision with the package name org.apache.commons.codec and as stated by #Don I changed it to com.apache.commons.codec and worked fine! How I did it?
I downloaded the source code and changed the 3 directories org to com. I also replaced all the occurrences of the package name in the files where they appear and also changed the references in the docs to com/apache/commons/codec/. (Do not try to remane them manually or you will spend the hole day). Then I compiled the library and generated the jar with Ant, which I called commons-codec-1.6-android.jar. I put the jar in the libs/ folder of my Android app and added it to the buildpath. Also, I attached the sources as the folder which contains all the files. So now I have the library ready to use with Android!
Hope that it helps someone else!

Thanks #DA25
This is working fine for me
I have add dependency
compile 'commons-codec:commons-codec:1.9'
ref: http://mvnrepository.com/artifact/commons-codec/commons-codec/1.9
my function
public String encode(String key, String data) {
try {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secret_key);
return new String(Hex.encodeHex(sha256_HMAC.doFinal(data.getBytes("UTF-8"))));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}

For me proguard removed the class during obfuscation .Add this to your Proguard rules.
-keep class org.apache.commons.** { *; }
Here's the method I was using of apache package.
Hex.encodeHex(digest)

Add method
public static String byteArrayToHexString(byte[] bytes) {
final char[] toDigits = "0123456789abcdef".toCharArray();
int l = bytes.length;
char[] out = new char[l << 1];
int i = 0; for (int j = 0; i < l; ++i) {
out[(j++)] = toDigits[((0xF0 & bytes[i]) >>> 4)];
out[(j++)] = toDigits[(0xF & bytes[i])];
}
return new String(out);
}

We used below code and it worked :
HmacUtils hmacUtils = new HmacUtils(HmacAlgorithms.HMAC_SHA_256, keyString);
String digest = new String( Hex.encodeHex(hmacUtils.hmac(msg)));

Another way to rename DigestUtils class will be with proguard.
If you are not using proguard you can enable it and add this one line which will obfuscate only DigestUtils class, and leave everything else intact.
-keep class !org.apache.commons.codec.digest.DigestUtils,com.** { *; }
and add this to your app build.gradle
buildTypes {
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Or OPTION 2
Use old version of the library in your code:
implementation("commons-codec:commons-codec:1.3"){
force = true
}
Need to use force = true if common-codec dependency come from third part library otherwise Gradle will resolve by default to higher version.

Related

NoClassDefFoundError when trying to build google api client

today i added the google play services library to my project. I have followed the instructions which are given on http://developer.android.com/google/play-services/setup.html#ensure
Screenshot of my Settings: https://drive.google.com/file/d/0Bwe9sI6XZ6rcZGZzdmlaQjVheDA/edit?usp=sharing
The problem is when I try to execute the following code, a noclassdeffounderror exception raises.
public class MainActivity extends Activity {
private GoogleApiClient mGoogleClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
mGoogleClient = new GoogleApiClient.Builder(this)
.addApi(Games.API)
.addScope(Games.SCOPE_GAMES)
.setGravityForPopups(Gravity.TOP | Gravity.CENTER_HORIZONTAL)
.build();
In the logcat the exception looks like
threadid=1: thread exiting with uncaught exception (group=0x40d02930)
FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.google.android.gms.common.api.GoogleApiClient$Builder
at com.gmail.mhofer4991.at.taptorotate.MainActivity.onCreate(MainActivity.java:104)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2147)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$600(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5074)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Force finishing activity com.example.taptorotate/com.gmail.mhofer4991.at.taptorotate.MainActivity
I also have cleaned and rebuilt the project.
It's driving me crazy that i cannot solve the problem. I know there are already other threads with the same topic and I also tried some solutions which were suggested there but none of them worked for me. Please help me out!
I also had same problem,
java.lang.NoClassDefFoundError:
com.google.android.gms.common.api.GoogleApiClient$Builder
I was using the below dependency on the application build.gradle file.
compile 'com.google.android.gms:play-services:7.8.0'
I changed it to
compile 'com.google.android.gms:play-services-plus:7.8.0'
I updated the Google Play Services (Rev 26) and Google Repository(Rev 20).
After that the exception doesn't occur.
Try to use individual APIs and corresponding build.gradle descriptions as mensioned in the following page,
https://developers.google.com/android/guides/setup#ensure
Try to add obfuscation rules for the Google API's if you have minifyEnabled or ProGuard set to true:
ProGuard Configuration
tryin to work with an implementation of Google Drive in Android i´ve found this error message pointing some classes like:
java.lang.ClassNotFoundException: com.google.android.gms.common.api.Scope
Caused by: java.lang.ClassNotFoundException: Didn't find class
I have found that the real problem is the limit of 64K references when we add dependencies related to GoogleApiClient, so adding multidex support solved my problem:
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
...
...
multiDexEnabled true
}
...
...

No JNI_OnLoad found skipping init > Application shutdown

Folks,
I am working on an android application where I need a third party .so library. I built
this third party library (with ndk-build) as per their instructions and was then looking
to include this .so in to my Android project.
Therefore I followed the steps described in docs/PREBUILTS.html and successfully build the
new .so in the jni/prebuilt directory. Now I tried leveraging the .so facilities by using it in a simple test android app. So what i do is :
static {
Log.i("load so > ","load so");
System.loadLibrary("xyz");
}
/* The native functions */
private static native int openFile(String filename);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
String path = getPathForDownloadDirectoryFile();
Log.i("file path> ", path);
int num= openFile(path);
}catch(Exception e){
Log.e(">", "could not open the file");
}
}
Now when I run my app I get a debug message saying :
No JNI_OnLoad found in /data/data/com.example.myfirstapp/lib/xyz.so 0x411e6738, skipping init
and then the application shuts down.
For More Info, Here is the error log :
No JNI_OnLoad found in /data/data/com.example.mysecondapp/lib/xyz.so 0x411e67a0, skipping init
W/dalvikvm( 570): No implementation found for native Lcom/example/mysecondapp/MainActivity;.openFile:(Ljava/lang/String;)I
D/AndroidRuntime( 570): Shutting down VM
W/dalvikvm( 570): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
E/AndroidRuntime( 570): FATAL EXCEPTION: main
E/AndroidRuntime( 570): java.lang.UnsatisfiedLinkError: Native method not found: com.example.mysecondapp.MainActivity.openFile:(Ljava/lang/String;)I
E/AndroidRuntime( 570): at com.example.mysecondapp.MainActivity.openFile(Native Method)
E/AndroidRuntime( 570): at com.example.mysecondapp.MainActivity.onCreate(MainActivity.java:31)
E/AndroidRuntime( 570): at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime( 570): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime( 570): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
E/AndroidRuntime( 570): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
E/AndroidRuntime( 570): at android.app.ActivityThread.access$600(ActivityThread.java:130)
E/AndroidRuntime( 570): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
E/AndroidRuntime( 570): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 570): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 570): at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime( 570): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 570): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 570): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime( 570): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 570): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 146): Force finishing activity com.example.mysecondapp/.MainActivity
As I could see that native implementation for the openFile() method was not found but the same xyz.so lib worked pretty neat with the original sample app from the third party. I am pretty much a starter with Android-ndk world.
Java-Android-NDK Ninjas ..any guess on what I might be missing ?
I'll highly appreciate any help here :)
As guycole said "No JNI_OnLoad" is just a warning , your problem lies elsewhere .
As you mentioned you successfully compiled your "so" file , the problem may lie in your function signatures inside your c/C ++ code it should be something like this
JNIEXPORT jint JNICALL Java_com_your_package_class_method(JNIEnv *d, jobject e, jstring f)
{
//some action
}
The function signatures comes from the header file which is generated using javah tool.You need to generate header file and use the function signature with your package name. For different package and class names the header file and corresponding function signature will change .
worked pretty neat with the original sample app from the third party
This might be the reason its running on the sample app and not on your app.
refer: https://thenewcircle.com/s/post/49/using_ndk_to_call_c_code_from_android_apps
The "No JNI_OnLoad" message is just a warning. JNI_OnLoad is an optional initialization hook.
I guess your problem is inside the openFile() method. Try commenting out the call from Java and see how far you get.
I have a blog post about JNI and some sample code at http://guycole.blogspot.com/2012/03/yet-another-android-ndk-blog-posting.html - perhaps you will find it useful.
Good luck.
It also comes with this log
??-?? ??:??:??.???: INFO/(): java.lang.UnsatisfiedLinkError: Couldn't load *: findLibrary returned null
right??
I think it's the problem of android.mk files.
1:try to swith to armabi v7.
2:load funciton will call open(). check permission of the so.
As mentioned in the previous answers, No JNI_OnLoad is only a warning.
I had got similar problem, I figured the problem is because of file operations.
My app was not having external storage write permission.After adding the below code in
manifest it was working fine

NoSuchMethod exception while creating and formatting .doc file using Apache poi-3.8 beta hwpf

I am getting following Exception when trying to run this code.
java.lang.NoSuchMethodError: org.apache.poi.POIDocument.< init >
Code Snippet:
try {
File file = new File(externalPath + "/abc.doc");
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
CharacterRun run = range.insertAfter("Hello World!");
run.setFontSize(2 * 18);
run.setBold(true);
run.setItalic(true);
run.setCapitalized(true);
OutputStream out = new FileOutputStream(new File(externalPath + "/agnew.doc"));
doc.write(out);
out.flush();
out.close();
} catch (Exception ex) {
Log.e("Exception==","=="+ex.toString());
ex.printStackTrace();
}
Logcat:
Logcat : FATAL EXCEPTION: main : java.lang.NoSuchMethodError: org.apache.poi.POIDocument. :
at org.apache.poi.hwpf.HWPFDocumentCore.(HWPFDocumentCore.java:145) :
at org.apache.poi.hwpf.HWPFDocument.(HWPFDocument.java:218) :
at org.apache.poi.hwpf.HWPFDocument.(HWPFDocument.java:186) :
at com.vikas.prudent.CreateDocument.onCreate(CreateDocument.java:45) :
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) :
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) :
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) :
at android.app.ActivityThread.access$2300(ActivityThread.java:125) :
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) :
at android.os.Handler.dispatchMessage(Handler.java:99) :
at android.os.Looper.loop(Looper.java:123) :
at android.app.ActivityThread.main(ActivityThread.java:4627) :
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:868) :
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) :
at dalvik.system.NativeStart.main(Native Method)
Just looking at the exception, it appears to be lib version mis-match. Looks like HWPFDocumentCore.java:145 is trying to construct a new POIDocument but cannot find the expected constructor to call. I would take a look at line 145 of HWPFDocumentCore.java and see what constructor it is expecting. Then look for a POI library which has such a constructor in the POIDocument.
It sounds like you have two copies of Apache POI on your classpath, and old one and a new one. My hunch is that your HWPF jar (Scratchpad) is new, but it's picking up an old core POI jar, which is why you're getting the exception.
What you need to do is review all the jars on your classpath, and identify the POI related jars, then ensure you have a consistent set of them.
The POI FAQ has and entry on this very problem, along with some Java code you can use to print out which jar the POI classes come from. If you can't spot the wrong jars directly, try porting something like the code shown there to your android code to help you find the older jar.

Problem: "Extracting" JSON using lift-json in an Android (Scala) application

I want to deserialize a JSON-String using the lift-json library. In my Android application I'm using Scala 2.9.0 and lift-json_2.9.0_2.4-M1.
I took a simple example from lift-json readme, but everytime I try to extract values from the JSON-String I get a net.liftweb.json.MappingException when calling the Activity. It seems there are no args delivered to "extract".
Here is my Activity:
import _root_.android.app.Activity
import _root_.android.os.Bundle
import net.liftweb.json._
class JsonTest extends Activity {
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.mainactivity)
implicit val formats = DefaultFormats
case class NumList(numbers: List[Int])
val json = parse(""" { "numbers" : [1, 2, 3, 4] } """)
json.extract[NumList]
}
}
and here the exception I get:
06-29 12:09:31.548: ERROR/AndroidRuntime(405): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.fhs.spirit/de.fhs.spirit.JsonTest}: net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor
args=
arg types=
constructor=public de.fhs.spirit.JsonTest$NumList$2(de.fhs.spirit.JsonTest,scala.collection.immutable.List)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
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:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor
args=
arg types=
constructor=public de.fhs.spirit.JsonTest$NumList$2(de.fhs.spirit.JsonTest,scala.collection.immutable.List)
at net.liftweb.json.Meta$.fail(Meta.scala:185)
at net.liftweb.json.Extraction$.instantiate$1(Extraction.scala:257)
at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:280)
at net.liftweb.json.Extraction$.build$1(Extraction.scala:298)
at net.liftweb.json.Extraction$.extract0(Extraction.scala:345)
at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$extract0(Extraction.scala:194)
at net.liftweb.json.Extraction$.extract(Extraction.scala:42)
at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:290)
at de.fhs.spirit.JsonTest.onCreate(JsonTest.scala:16)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
Would be great if you have an idea how to solve my problem.
Thank you!
Greetings,
Illaz
I suspect Lift uses reflection in this mode. It might not work at all in Android, or it may require that some stuff that got removed not be removed. I suggest you just try to use some of other alternatives instead.
See also this question -- my suspicion comes from the remark that it doesn't work on REPL. Not that REPL doesn't have reflection, but its package structure might be confusing to the Lift library.

Android "java.lang.noclassdeffounderror" exception

I have a android webservice client application. I am trying to use the java standard WS library support. I have stripped the application down to the minimum, as shown below, to try and isolate the issue. Below is the application,
package fau.edu.cse;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ClassMap extends Activity {
TextView displayObject;
#Override
public void onCreate(Bundle savedInstanceState) {
// Build Screen Display String
String screenString = "Program Started\n\n";
// Set up the display
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
displayObject = (TextView)findViewById(R.id.TextView01);
screenString = screenString + "Inflate Disaplay\n\n";
try {
// Set up Soap Service
TempConvertSoap service = new TempConvert().getTempConvertSoap();
// Successful Soap Object Build
screenString = screenString + "SOAP Object Correctly Build\n\n";
// Display Response
displayObject.setText(screenString);
}
catch(Throwable e){
e.printStackTrace();
displayObject.setText(screenString +"Try Error...\n" + e.toString());
}
}
}
The classes tempConvert and tempConvertSoap are in the package fau.edu.cse. I have included the java SE javax libraries in the java build pasth. When the android application tries to create the "service" object I get a "java.lang.noclassdeffounderror" exception. The two classes tempConvertSoap and TempConvet() are generated by wsimport. I am also using several libraries from javax.jws.. and javax.xml.ws.. Of course the application compiles without error and loads correctly. I know the application is running becouse my "try/catch" routine is successfully catching the error and printing it out. Here is what is in the logcat says (notice that it cannot find TempConvert),
06-12 22:58:39.340: WARN/dalvikvm(200): Unable to resolve superclass of Lfau/edu/cse/TempConvert; (53)
06-12 22:58:39.340: WARN/dalvikvm(200): Link of class 'Lfau/edu/cse/TempConvert;' failed
06-12 22:58:39.340: ERROR/dalvikvm(200): Could not find class 'fau.edu.cse.TempConvert', referenced from method fau.edu.cse.ClassMap.onCreate
06-12 22:58:39.340: WARN/dalvikvm(200): VFY: unable to resolve new-instance 21 (Lfau/edu/cse/TempConvert;) in Lfau/edu/cse/ClassMap;
06-12 22:58:39.340: DEBUG/dalvikvm(200): VFY: replacing opcode 0x22 at 0x0027
06-12 22:58:39.340: DEBUG/dalvikvm(200): Making a copy of Lfau/edu/cse/ClassMap;.onCreate code (252 bytes)
06-12 22:58:39.490: DEBUG/dalvikvm(30): GC freed 2 objects / 48 bytes in 273ms
06-12 22:58:39.530: DEBUG/ddm-heap(119): Got feature list request
06-12 22:58:39.620: WARN/Resources(200): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f050000}
06-12 22:58:39.620: WARN/System.err(200): java.lang.NoClassDefFoundError: fau.edu.cse.TempConvert
06-12 22:58:39.830: WARN/System.err(200): at fau.edu.cse.ClassMap.onCreate(ClassMap.java:26)
06-12 22:58:39.830: WARN/System.err(200): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-12 22:58:39.830: WARN/System.err(200): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-12 22:58:39.830: WARN/System.err(200): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-12 22:58:39.830: WARN/System.err(200): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-12 22:58:39.880: WARN/System.err(200): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-12 22:58:39.880: WARN/System.err(200): at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 22:58:39.880: WARN/System.err(200): at android.os.Looper.loop(Looper.java:123)
06-12 22:58:39.880: WARN/System.err(200): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-12 22:58:39.880: WARN/System.err(200): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 22:58:39.880: WARN/System.err(200): at java.lang.reflect.Method.invoke(Method.java:521)
06-12 22:58:39.880: WARN/System.err(200): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-12 22:58:39.880: WARN/System.err(200): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-12 22:58:39.880: WARN/System.err(200): at dalvik.system.NativeStart.main(Native Method)
...bla...bla...bla
It would be great if someone just had an answer, however I am looking at debug strategies. I have taken this same application and created a standard java client application and it works fine -- of course with all of the android stuff taken out. What would be a good debug strategy? What methods and techniques would you recommend I try and isolate the problem? I am thinking that there is some sort of Dalvik VM incompatibility that is causing the TempConvert class not to load. TempConvert is an interface class that references a lot of very tricky webservice attributes. Any help with debug strategies would be gladly appreciated.
Thanks for the help,
Steve
If your are using an external library, for example in case of external library of Goolge map you have to add
<uses-library android:name="com.google.android.maps" />
inside the <Application> tag in Android Manifest.xml
Here's your problem:
java.lang.NoClassDefFoundError: fau.edu.cse.TempConvert
From the javadocs:
Thrown if the Java Virtual Machine or
a ClassLoader instance tries to load
in the definition of a class (as part
of a normal method call or as part of
creating a new instance using the new
expression) and no definition of the
class could be found.
The searched-for class definition
existed when the currently executing
class was compiled, but the definition
can no longer be found.
Sounds like you have an Android deployment or packaging issue. It can't find your class, despite your assumptions.
When you observe behavior that counters your assumptions, you should put them aside and check everything from the beginning. Assuming that everything is correct, in spite of the evidence you have before your eyes, gets in the way of finding a solution.
I resolved this by moving <uses-library android:name="com.google.android.maps" /> inside of the <application> tags in my AndroidManifest.xml

Categories

Resources