为什么 me.pushy.sdk.Pushy 在 Eclipse Indigo 中导致 NoClassDefFoundError?

Why does me.pushy.sdk.Pushy result in a NoClassDefFoundError in Eclipse Indigo?

我正尝试在我的 android 应用程序中实现 Pushy Push notification gateway 推送通知,但我收到 NoClassdefFoundError。我的mainactivity代码如下:

import java.net.URL;

import me.pushy.sdk.Pushy;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;


public class Splash extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Pushy.listen(this);
    setContentView(R.layout.activity_splash);
    new registerForPushNotificationsAsync().execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_splash, menu);
    return true;
}



private class registerForPushNotificationsAsync extends AsyncTask<Void, Void, Exception>
{
    protected Exception doInBackground(Void... params)
    {
        try
        {
            // Acquire a unique registration ID for this device
            String registrationId = Pushy.register(getApplicationContext());

            // Send the registration ID to your backend server and store it for later
            sendRegistrationIdToBackendServer(registrationId);
        }
        catch( Exception exc )
        {
            // Return exc to onPostExecute
            return exc;
        }

        // We're good
        return null;
    }

    @Override
    protected void onPostExecute(Exception exc)
    {
        // Failed?
        if ( exc != null )
        {
            // Show error as toast message
            Toast.makeText(getApplicationContext(), exc.toString(), Toast.LENGTH_LONG).show();
            return;
        }

        // Succeeded, do something to alert the user
    }

    // Example implementation
    void sendRegistrationIdToBackendServer(String registrationId) throws Exception
    {
        // The URL to the function in your backend API that stores registration IDs
        URL sendRegIdRequest = new URL("https://[ip]/sahiyogihaat/register.php?registration_id=" + registrationId);

        // Send the registration ID by executing the GET request
        sendRegIdRequest.openConnection();
    }
}

}

我也添加了罐子...

我的 logcat 是:

无法理解如何解决这个问题...提前谢谢您

在您的第一个屏幕截图中,您似乎从磁盘上的绝对路径集成了这个库,因此它可能永远不会被 Android 工具“DEX”化,因此不会成为您的一部分最终的 APK 文件。在运行时,这会导致您在屏幕截图 2 中描述的行为。

解决方案

您需要将 pushy-1.0.7.jar 捆绑到您的 Android 应用程序中。因此,应该从项目根目录开始的 libs 文件夹中 相对地 引用它。提示:引用时不要使用“Add external JAR”。这可能是这里出现问题的原因。

另请参阅 dealing with dependencies in Android projects 上的这篇文章:

Projects have source folders, as well as Library Project and jar file dependencies. With no other setup needed than adding Library Projects as a dependency in project.properties, a project’s classpath is automatically populated with:

  • The content of the project’s libs/*.jar
  • The output of the Library Projects.
  • The Library Projects’ libs/*.jar

...

在 Android 开发人员 documentation/guides 中的文章 Managing Projects Overview 中给出了另一个(更通用的)概述。

来自Pushy的官方文档:

If you haven't migrated your project to Gradle yet, make sure to include the pushy-x.x.x.jar as a library in your project preferences (in Eclipse / IntelliJ / Android Studio). Make sure to export the library with the APK.

因此请检查 pushy-1.0.7.jar 文件是否放置在项目的 libs 文件夹中,并在您的类路径设置中从那里相对引用。有了这个它应该可以工作并且构建过程应该 DEX 编译这些 类 以供以后运行时加载。

希望对您有所帮助。