How to solve java.lang.NoClassDefFoundError for com.google.android.gms.R$styleable error?

泪湿孤枕 提交于 2020-01-05 09:21:44

问题


I use Google Maps in a project and build it with Android.mk. But still (after days of research) cann't figure out how to solve NoClassDefFoundError which crashes the apk in time Google Maps fragment inflation is happen. Can any inside me about a way it have to be solved? Detalization is below.

Google Play Services library is added in the following way:

#Adding classpath to Google Play Services classes
LOCAL_CLASSPATH += $(LOCAL_PATH)/google-play-services_lib/bin/
...
#Google Play Services
LOCAL_STATIC_JAVA_LIBRARIES += google-play-services \
                                google-play-services_lib
...
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES += google-play-services:libs/google-play-services.jar \
                                        google-play-services_lib:libs/google-play-services_lib.jar

Build goes fine but application throws runtime error when tries to inflate layout with Google Maps fragment. LogCat:

**java.lang.NoClassDefFoundError**:
java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
    at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
    at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
    at android.app.Activity.onCreateView(Activity.java:4996)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
    at com.mapsvisitor.ui.MapsFragment.onCreateView(MapsFragment.java:81)
    ...

Code:

final View layout = inflater.inflate(R.layout.maps_layout, null);

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map_main"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:orientation="vertical">
        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>

回答1:


The problem is in that when you use dependency project as a library the generated JAR (google-play-services_lib.jar) is not suitable for independent use (as java static libraries). As solution simple use JAR generated by Eclipse export instead in the following way Export > Java > JAR file with selected src and gen folders to export:

The following snippet helps to define that com.google.android.gms.R and nested classes are unavailable (com.google.android.gms.R$attr, com.google.android.gms.R$color, etc):

void isClassesAvailable(List<String> classes) {
    for(String cl: classes) {
        try {
            Class.forName(cl);
        }
            catch (ClassNotFoundException e) {
            Log.w(TAG, e.getMessage());
        }
    }
}

How JAR is packed for dependency project (with src folder classes. See folder "android" below):

How JAR has to be packed for robust static library (with src and gen folder classes. See folders "android" and "com" below):

However before idiomatically right solution which uses Eclipse export light hack becomes the GTD. Simple repacking google-play-services_lib.jar with google-play-services_lib/bin/classes content and previously presented there ones makes all resource's classes available.



来源:https://stackoverflow.com/questions/24649217/how-to-solve-java-lang-noclassdeffounderror-for-com-google-android-gms-rstyleab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!