我的Android App中存在一个类的NoClassDefFoundError。我正在使用maven构建,而无法找到的类是LruBitmapCache.java,这是我在android项目中编写的Volley ImageCache的实现:
package com.x.y.app;
public class LruBitmapCache extends LruCache<String, Bitmap> implements ImageCache
{
public static int getDefaultLruCacheSize()
{
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
return cacheSize;
}
public LruBitmapCache()
{
this(getDefaultLruCacheSize());
}
public LruBitmapCache(int sizeInKiloBytes)
{
super(sizeInKiloBytes);
}
@Override
protected int sizeOf(String key, Bitmap value)
{
return value.getRowBytes() * value.getHeight() / 1024;
}
@Override
public Bitmap getBitmap(String url)
{
return get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap)
{
put(url, bitmap);
}
}
我的pom.xml如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.x.y</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>android-project</artifactId>
<packaging>apk</packaging>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.8.2</version>
<configuration>
<sdk>
<platform>19</platform>
</sdk>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>false</undeployBeforeDeploy>
<device>usb</device>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>android</groupId>
<artifactId>android</artifactId>
<version>4.4.2_r3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v4</artifactId>
<version>r7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>
<dependency>
<groupId>com.googlecode.androidannotations</groupId>
<artifactId>androidannotations</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.mcxiaoke.volley</groupId>
<artifactId>library</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>fr.avianey</groupId>
<artifactId>facebook-android-api</artifactId>
<version>3.17.1</version>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>4.0.2</version>
</dependency>
</dependencies>
</project>
我正在使用“mvn install android:deploy”构建和部署应用程序,该应用程序已成功将应用程序部署到设备,但是当我尝试打开应用程序时,我得到NoClassDefFoundError(因为某些应用程序代码尝试初始化LruBitmapCache)。我原以为它可能是因为Volley依赖没有被包含在类中,但我并不是100%确定dex类是如何工作的。有没有人知道为什么会发生这种情况或者之前是否有人这样做过?错误的课程?依赖冲突?任何帮助表示赞赏。
修改: Logcat附件
08-31 04:24:36.385: E/dalvikvm(19634): Could not find class 'com.x.y.app.LruBitmapCache', referenced from method com.x.y.app.AppController.getImageLoader
08-31 04:24:36.485: E/AndroidRuntime(19634): Process: com.x.y, PID: 19634
08-31 04:24:36.485: E/AndroidRuntime(19634): java.lang.NoClassDefFoundError: com.x.y.app.LruBitmapCache
08-31 04:24:36.485: E/AndroidRuntime(19634): at com.x.y.app.AppController.getImageLoader(AppController.java:38)
08-31 04:24:36.485: E/AndroidRuntime(19634): at com.x.y.activitiy.MyActivity.init(MyActivity.java:41)
08-31 04:24:36.485: E/AndroidRuntime(19634): at com.x.y.activitiy.MyActivity_.afterSetContentView_(MyActivity_.java:89)
08-31 04:24:36.485: E/AndroidRuntime(19634): at com.x.y.activitiy.MyActivity_.setContentView(MyActivity_.java:95)
08-31 04:24:36.485: E/AndroidRuntime(19634): at com.x.y.activitiy.MyActivity.onCreate(MyActivity.java:27)
08-31 04:24:36.485: E/AndroidRuntime(19634): at com.x.y.activitiy.MyActivity_.onCreate(MyActivity_.java:32)
从堆栈跟踪中可以看出,我正在使用android注释。 LruBitmapCache的引用来自AppController(在AndroidManifest.xml中声明为android应用程序),它在MyActivity的@AfterViews init方法中调用。
AppController.java
package com.x.y.app;
public class AppController extends Application
{
private RequestQueue requestQueue;
private ImageLoader imageLoader;
public RequestQueue getRequestQueue()
{
if (requestQueue == null)
{
requestQueue = Volley.newRequestQueue(getApplicationContext());
}
return requestQueue;
}
public ImageLoader getImageLoader()
{
getRequestQueue();
if (imageLoader == null)
{
//NoClassDefFoundError thrown when instantiation of LruButmapCache attempted here
imageLoader = new ImageLoader(requestQueue, new LruBitmapCache());
}
return imageLoader;
}
}
MyActivity.java
package com.x.y.activity;
@EActivity(R.layout.my_activity)
public class MyActivity extends AbstractActivity
{
private MyAdapter adapter;
@ViewById
ListView listView;
@AfterViews
public void init()
{
AppController application = getApplicationContext();
adapter = new MyAdapter(application.getImageLoader());
listView.setAdapter(adapter);
}
}
AbstractActivity.java
public abstract class AbstractActivity extends Activity
{
@Override
public AppController getApplicationContext()
{
return (AppController) super.getApplicationContext();
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.x.y"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name="com.x.y.app.AppController"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/appTheme"
android:windowSoftInputMode="stateHidden">
<activity android:name="com.x.y.activitiy.MyActivity_">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
我以一种我认为完全相同的方式逐步重建我的项目,问题已经消失。我想我永远不会知道这是什么问题。