java.lang.NoClassDefFoundError 在 Android 台设备上安装了 KitKat
java.lang.NoClassDefFoundError on Android devices with KitKat
我正在开发一个应用程序,如果我使用 Android KitKat,该应用程序会崩溃,但如果使用 Lollipop 设备,该应用程序会运行而不会崩溃。来自 LogCat 的错误:
java.lang.NoClassDefFoundError: mypackage.utils.PlacesAutoCompleteAdapter
at mypackage.utils.PlacesAutoCompleteAdapter.getFilter(PlacesAutoCompleteAdapter.java:60)
at android.widget.AutoCompleteTextView.setAdapter(AutoCompleteTextView.java:635)
at mypackage.PlanRouteActivity.initializeLayout(PlanRouteActivity.java:148)
at mypackage.PlanRouteActivity.onCreate(PlanRouteActivity.java:83)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access0(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
class在错误包内:
package mypackage.utils;
public class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private static final String TAG = "AutoCompleteAdapter";
// Autocomplete Google Places
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
private static final String API_BROWSER_KEY = "AIzaSyD5JwM_VRieW2WmzPU_D4sl6YST0wuH6Io";
private ArrayList<String> resultList;
// Constructor
public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
/**
* Autocomplete text input with Google places
*
* @param input: Text input from AutoCompleteTextView
* @return ArrayList<String>
*/
private ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?key=" + API_BROWSER_KEY);
sb.append("&components;=country:de");
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(TAG, "Error processing Places API URL", e);
return null;
} catch (IOException e) {
Log.e(TAG, "Error connecting to Places API", e);
return null;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
// Extract the Place descriptions from the results
resultList = new ArrayList<String>(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
}
} catch (JSONException e) {
Log.e(TAG, "Cannot process JSON results", e);
}
return resultList;
}
}
因此,应用程序再次使用 Android Lollipop 运行而不会崩溃,有人知道它为什么在 KitKat 上崩溃吗?我的minSDKversion是16,targetSDKversion是21
我之前在两种情况下观察到这个错误:
1. 我使用的是启用了 ART 的 Kitkat 的物理设备(即不是模拟器)。当我 运行 另一个带有 Dalvik 运行时的 Kitkat 设备上的应用程序时,错误似乎已经消失了。
2. 由于 .dex
将 SVN 转移到另一个位置时发生的构建错误,出现了同样的问题。我完全删除了旧项目并在我的计算机上从原始项目创建了一个新的存储库,并且在完全重建项目后错误没有再次出现。
请签到 gradle 这段代码可能对您有帮助
dex选项{
javaMaxHeapSize "4g"
preDexLibraries = 假
}
如果您的应用程序 class 没有实例化,请仔细检查您的 Menifest.xml 应用程序 class 条目,这可能会发生。您的应用程序 class 应如下所示:
public class AppController extends MultiDexApplication {
public void onCreate() {
super.onCreate();
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
添加依赖compile 'com.android.support:multidex:1.0.1'
在默认配置下将 multiDexEnabled = true
选项添加到 build.gradle:
defaultConfig {
applicationId "com.app"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1"
multiDexEnabled = true
}
就是这样。
我正在开发一个应用程序,如果我使用 Android KitKat,该应用程序会崩溃,但如果使用 Lollipop 设备,该应用程序会运行而不会崩溃。来自 LogCat 的错误:
java.lang.NoClassDefFoundError: mypackage.utils.PlacesAutoCompleteAdapter
at mypackage.utils.PlacesAutoCompleteAdapter.getFilter(PlacesAutoCompleteAdapter.java:60)
at android.widget.AutoCompleteTextView.setAdapter(AutoCompleteTextView.java:635)
at mypackage.PlanRouteActivity.initializeLayout(PlanRouteActivity.java:148)
at mypackage.PlanRouteActivity.onCreate(PlanRouteActivity.java:83)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access0(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
class在错误包内:
package mypackage.utils;
public class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private static final String TAG = "AutoCompleteAdapter";
// Autocomplete Google Places
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
private static final String API_BROWSER_KEY = "AIzaSyD5JwM_VRieW2WmzPU_D4sl6YST0wuH6Io";
private ArrayList<String> resultList;
// Constructor
public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
/**
* Autocomplete text input with Google places
*
* @param input: Text input from AutoCompleteTextView
* @return ArrayList<String>
*/
private ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?key=" + API_BROWSER_KEY);
sb.append("&components;=country:de");
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(TAG, "Error processing Places API URL", e);
return null;
} catch (IOException e) {
Log.e(TAG, "Error connecting to Places API", e);
return null;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
// Extract the Place descriptions from the results
resultList = new ArrayList<String>(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
}
} catch (JSONException e) {
Log.e(TAG, "Cannot process JSON results", e);
}
return resultList;
}
}
因此,应用程序再次使用 Android Lollipop 运行而不会崩溃,有人知道它为什么在 KitKat 上崩溃吗?我的minSDKversion是16,targetSDKversion是21
我之前在两种情况下观察到这个错误:
1. 我使用的是启用了 ART 的 Kitkat 的物理设备(即不是模拟器)。当我 运行 另一个带有 Dalvik 运行时的 Kitkat 设备上的应用程序时,错误似乎已经消失了。
2. 由于 .dex
将 SVN 转移到另一个位置时发生的构建错误,出现了同样的问题。我完全删除了旧项目并在我的计算机上从原始项目创建了一个新的存储库,并且在完全重建项目后错误没有再次出现。
请签到 gradle 这段代码可能对您有帮助
dex选项{ javaMaxHeapSize "4g" preDexLibraries = 假 }
如果您的应用程序 class 没有实例化,请仔细检查您的 Menifest.xml 应用程序 class 条目,这可能会发生。您的应用程序 class 应如下所示:
public class AppController extends MultiDexApplication {
public void onCreate() {
super.onCreate();
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
添加依赖compile 'com.android.support:multidex:1.0.1'
在默认配置下将 multiDexEnabled = true
选项添加到 build.gradle:
defaultConfig {
applicationId "com.app"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1"
multiDexEnabled = true
}
就是这样。