Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

android can't find class from external jar

I am trying to create a simple test app that basically extends the Android Hello World tutorial app by invoking some simple functionality from an external JAR. However, when I run the app, it can't find the class from the JAR. What am I doing wrong?

Here's entire source of the JAR:

package com.mytests.pow;

public class power2 {

private double d;   

public power2()
{
    d = 0.0;
}   

public String stp2(double dd) 
{
    d = dd*dd;
    return String.format("%e", d);
}

}

And here's the "Hello World ++" source:

package com.myLuceneTests.namespace;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.mytests.pow.*;

public class AndroidSimpleSIH_EclipseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    power2 pp = new power2();
    String iout = pp.stp2(12.0);

    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);
    tv.setText(iout);
    setContentView(tv);

}
}

When I run the app, I get this in logcat:

11-22 12:24:52.697  2963  2963 E dalvikvm: Could not find class 'com.mytests.pow.power2', referenced from method com.myLuceneTests.namespace

.AndroidSimpleSIH_EclipseActivity.onCreate

and then

11-22 12:24:52.713  2963  2963 E AndroidRuntime: java.lang.NoClassDefFoundError: com.mytests.pow.power2

What am I doing wrong? Thanks!

By the way, my actual goal is to use a real JAR (rather than this toy one) in an Android app. I might have access to the code for that JAR and might be able to rebuild it but it's a big piece of Java code and I am likely to encounter problems when rebuilding it so I am trying to use the pre-built JAR.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's no need to build the external jar. Try removing it from your build path and follow these steps:

  1. Create a folder in the root of your Android project called libs.
  2. Add the jar to that folder.
  3. Right-click the jar and click to add to build path.
  4. Clean your project and try it again.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...