Android/Eclipse:如何嵌入 .jar 文件?

Posted

技术标签:

【中文标题】Android/Eclipse:如何嵌入 .jar 文件?【英文标题】:Android/Eclipse: How to embed .jar file? 【发布时间】:2012-10-04 03:06:27 【问题描述】:

我无法将库(在我的情况下为 ksoap)嵌入到我的项目中。运行时出现错误“找不到从方法...引用的类 'org.ksoap2.serialization.SoapObject'”。当然,这个类在 ksoap 库中可用,无需提及我的代码中有相应的“import ...”行。我已经尝试以不同的方式嵌入它,我在 *** 和其他地方找到了这些方式。我通过属性 => 构建路径 => 添加外部 jar 并通过导入 jar、属性 => 构建路径 => 添加 jar 进行了尝试。它没有任何区别,其他库也会发生同样的情况。我还尝试选择 Google API 或 android 作为构建目标 - 但是,它没有改变。

我正在使用 Eclipse Juno,该项目是 Android 2.2 项目。基本上,我一直在关注http://www.youtube.com/watch?v=v9EowBVgwSo的指南

请参阅下面的代码和错误消息的完整列表。

package com.example.mytest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.widget.TextView;

public class FirstScreen extends Activity 
    private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
    private static final String METHOD_NAME = "CelsiusToFahrenheit";
    private static final String NAMESPACE = "http://tempuri.org";
    private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
    TextView tv;        
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_screen);
        tv = (TextView)findViewById(R.id.textView01);
        tv.setText("Mein Test");

        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
        Request.addProperty("Celsius", "32");

        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(Request);

        HttpTransportSE aht = new HttpTransportSE(URL);

        try
        
            aht.call(SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
            tv.setText("Status: " + resultString);

        
        catch(Exception e)
        
            e.printStackTrace();
        

    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        getMenuInflater().inflate(R.menu.activity_first_screen, menu);
        return true;
    

错误信息:

10-13 12:59:00.734: E/dalvikvm(6567): Could not find class 'org.ksoap2.serialization.SoapObject', referenced from method com.example.mytest.FirstScreen.onCreate
10-13 12:59:00.734: W/dalvikvm(6567): VFY: unable to resolve new-instance 501 (Lorg/ksoap2/serialization/SoapObject;) in Lcom/example/mytest/FirstScreen;
10-13 12:59:00.734: D/dalvikvm(6567): VFY: replacing opcode 0x22 at 0x0012
10-13 12:59:00.734: D/dalvikvm(6567): VFY: dead code 0x0014-005a in Lcom/example/mytest/FirstScreen;.onCreate (Landroid/os/Bundle;)V
10-13 12:59:00.875: D/AndroidRuntime(6567): Shutting down VM
10-13 12:59:00.875: W/dalvikvm(6567): threadid=1: thread exiting with uncaught exception (group=0x40015578)
10-13 12:59:00.875: E/AndroidRuntime(6567): FATAL EXCEPTION: main
10-13 12:59:00.875: E/AndroidRuntime(6567): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
10-13 12:59:00.875: E/AndroidRuntime(6567):     at com.example.mytest.FirstScreen.onCreate(FirstScreen.java:25)
10-13 12:59:00.875: E/AndroidRuntime(6567):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-13 12:59:00.875: E/AndroidRuntime(6567):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-13 12:59:00.875: E/AndroidRuntime(6567):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-13 12:59:00.875: E/AndroidRuntime(6567):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-13 12:59:00.875: E/AndroidRuntime(6567):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-13 12:59:00.875: E/AndroidRuntime(6567):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 12:59:00.875: E/AndroidRuntime(6567):     at android.os.Looper.loop(Looper.java:130)
10-13 12:59:00.875: E/AndroidRuntime(6567):     at android.app.ActivityThread.main(ActivityThread.java:3687)
10-13 12:59:00.875: E/AndroidRuntime(6567):     at java.lang.reflect.Method.invokeNative(Native Method)
10-13 12:59:00.875: E/AndroidRuntime(6567):     at java.lang.reflect.Method.invoke(Method.java:507)
10-13 12:59:00.875: E/AndroidRuntime(6567):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-13 12:59:00.875: E/AndroidRuntime(6567):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-13 12:59:00.875: E/AndroidRuntime(6567):     at dalvik.system.NativeStart.main(Native Method)
10-13 12:59:00.878: I/Process(6567): Sending signal. PID: 6567 SIG: 9
10-13 13:03:01.222: E/dalvikvm(6870): Could not find class 'org.ksoap2.serialization.SoapObject', referenced from method com.example.mytest.FirstScreen.onCreate
10-13 13:03:01.222: W/dalvikvm(6870): VFY: unable to resolve new-instance 33 (Lorg/ksoap2/serialization/SoapObject;) in Lcom/example/mytest/FirstScreen;
10-13 13:03:01.222: D/dalvikvm(6870): VFY: replacing opcode 0x22 at 0x0012
10-13 13:03:01.222: D/dalvikvm(6870): VFY: dead code 0x0014-005a in Lcom/example/mytest/FirstScreen;.onCreate (Landroid/os/Bundle;)V
10-13 13:03:01.472: D/AndroidRuntime(6870): Shutting down VM
10-13 13:03:01.472: W/dalvikvm(6870): threadid=1: thread exiting with uncaught exception (group=0x40015578)
10-13 13:03:01.519: E/AndroidRuntime(6870): FATAL EXCEPTION: main
10-13 13:03:01.519: E/AndroidRuntime(6870): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
10-13 13:03:01.519: E/AndroidRuntime(6870):     at com.example.mytest.FirstScreen.onCreate(FirstScreen.java:25)
10-13 13:03:01.519: E/AndroidRuntime(6870):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-13 13:03:01.519: E/AndroidRuntime(6870):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-13 13:03:01.519: E/AndroidRuntime(6870):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-13 13:03:01.519: E/AndroidRuntime(6870):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-13 13:03:01.519: E/AndroidRuntime(6870):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-13 13:03:01.519: E/AndroidRuntime(6870):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 13:03:01.519: E/AndroidRuntime(6870):     at android.os.Looper.loop(Looper.java:130)
10-13 13:03:01.519: E/AndroidRuntime(6870):     at android.app.ActivityThread.main(ActivityThread.java:3687)
10-13 13:03:01.519: E/AndroidRuntime(6870):     at java.lang.reflect.Method.invokeNative(Native Method)
10-13 13:03:01.519: E/AndroidRuntime(6870):     at java.lang.reflect.Method.invoke(Method.java:507)
10-13 13:03:01.519: E/AndroidRuntime(6870):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-13 13:03:01.519: E/AndroidRuntime(6870):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-13 13:03:01.519: E/AndroidRuntime(6870):     at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

您是否尝试过将 jar 复制到 libs 文件夹中?如果该文件夹不存在,只需创建它。 我猜你足够健全,可以将它添加到 libs 文件夹中。 谢谢大家,这还不够,但这是正确的方法,请参阅我的要求下方的答案。 【参考方案1】:

(在问题编辑中回答。转换为社区 wiki 答案。参见What is the appropriate action when the answer to a question is added to the question itself?)

OP 写道:

感谢有关 libs 文件夹的提示,解决了这个问题。

感谢我收到的另一个提示,我能够找出问题所在。仅将库添加到 libs 文件夹是不够的 - 它还需要从该位置添加到构建路径(属性 => Java 构建路径 => 添加 Jars)。

这对于那些熟悉 Java/Android 开发的人来说可能很明显 - 但我对这些东西完全陌生。感谢您的帮助!

【讨论】:

以上是关于Android/Eclipse:如何嵌入 .jar 文件?的主要内容,如果未能解决你的问题,请参考以下文章

android------eclipse运行错误提示 Failed to load D:Androidsdkuild-tools26.0.0-previewlibdx.jar

如何将H2数据库嵌入到交付给客户端的jar文件中?

如何在嵌入 Eclipse 的 Maven 存储库中安装 jars?

如何使用嵌入式数据库 H2 创建 jar 文件?

如何将java数据库服务器嵌入分布式jar?

如何在 Servlet 中禁用不需要的 WebFilter(嵌入在 jar 中)? [复制]