我正在爲Android應用程序中存在的類獲取NoClassDefFoundError。我使用Maven構建和無法發現類是LruBitmapCache.java,凌空ImageCache的實現,我已經寫了我的Android項目中:運行使用Maven構建的Android應用程序時出現NoClassDefFoundError
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安裝Android:部署」,成功地部署應用到設備上,但是當我嘗試打開該應用程序我得到的NoClassDefFoundError的(因爲有些應用程序代碼的企圖初始化LruBitmapCache)。我認爲這可能是因爲Volley依賴沒有被包含在類中,但我不能100%確定類的工作方式。有沒有人知道爲什麼會發生這種情況,或者有人有過這種情況?錯誤的類?依賴衝突?任何幫助表示讚賞。
編輯: 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>
是否可以提供logcat? – hoomi 2014-08-31 08:53:05
我們能否請自己有錯誤? – Joehot200 2014-08-31 16:07:25
對不起,我不確定你的意思是哪個錯誤 – 2014-08-31 16:33:08