2014-02-07 3 views
0

내가, 기본 Google 클라우드 메시징 응용 프로그램을 만들려고 그러나, 나는 오류 다음 얻을 : 여기java.lang.NoClassDefFoundError가 : com.google.android.gms.common.GooglePlayServicesUtil

02-07 12:53:51.510  642-642/com.youtubemp3downloader E/AndroidRuntime﹕ FATAL EXCEPTION: main 
java.lang.NoClassDefFoundError: com.google.android.gms.common.GooglePlayServicesUtil 
     at com.youtubemp3downloader.MainActivity.checkPlayServices(MainActivity.java:222) 
     at com.youtubemp3downloader.MainActivity.onCreate(MainActivity.java:53) 
     at android.app.Activity.performCreate(Activity.java:5008) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
     at android.app.ActivityThread.access$600(ActivityThread.java:130) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4745) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
     at dalvik.system.NativeStart.main(Native Method) 

가있다 코드 MainActivity.java : 나는 안드로이드 Studio를 사용하고

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:0.6.+' 
    } 
} 
apply plugin: 'android' 

repositories { 
    mavenCentral() 
} 

android { 
    compileSdkVersion 19 
    buildToolsVersion "19.0.0" 

    defaultConfig { 
     minSdkVersion 9 
     targetSdkVersion 16 
    } 
    buildTypes { 
     release { 
      runProguard true 
      proguardFile getDefaultProguardFile('proguard-android.txt') 
     } 
    } 
} 

dependencies { 
    compile 'com.android.support:appcompat-v7:+' 
    compile 'com.google.android.gms:play-services:4.0.30' 
} 

: 여기

package com.youtubemp3downloader; 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.content.pm.PackageInfo; 
import android.content.pm.PackageManager; 
import android.os.AsyncTask; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.os.Build; 
import android.widget.Toast; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.gcm.GoogleCloudMessaging; 

import org.w3c.dom.Text; 

import java.io.IOException; 

public class MainActivity extends Activity { 

    private static final String TAG = "MyActivity"; 

    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; 
    public static final String PROPERTY_REG_ID = "registration_id"; 
    private static final String PROPERTY_APP_VERSION = "appVersion"; 

    GoogleCloudMessaging gcm; 
    Context context; 

    String regid; 
    String SENDER_ID="201069056364"; 

    @TargetApi(Build.VERSION_CODES.GINGERBREAD) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     context = getApplicationContext(); 

     if (checkPlayServices()) { 
      // If this check succeeds, proceed with normal processing. 
      // Otherwise, prompt user to get valid Play Services APK. 
      gcm = GoogleCloudMessaging.getInstance(this); 
      regid = getRegistrationId(context); 

      if (regid.isEmpty()) { 
       registerInBackground(); 
      } 
     } 
    } 


    /** 
    * Gets the current registration ID for application on GCM service. 
    * <p> 
    * If result is empty, the app needs to register. 
    * 
    * @return registration ID, or empty string if there is no existing 
    *   registration ID. 
    */ 
    @TargetApi(Build.VERSION_CODES.GINGERBREAD) 
    private String getRegistrationId(Context context) { 
     final SharedPreferences prefs = getGCMPreferences(context); 
     String registrationId = prefs.getString(PROPERTY_REG_ID, ""); 
     if (registrationId.isEmpty()) { 
      Log.i(TAG, "Registration not found."); 
      Toast.makeText(getApplicationContext(), "Registration not found.", 10).show(); 
      return ""; 
     } 
     // Check if app was updated; if so, it must clear the registration ID 
     // since the existing regID is not guaranteed to work with the new 
     // app version. 
     int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE); 
     int currentVersion = getAppVersion(context); 
     if (registeredVersion != currentVersion) { 
      Log.i(TAG, "App version changed."); 
      Toast.makeText(getApplicationContext(), "App version changed.", 10).show(); 
      return ""; 
     } 
     return registrationId; 
    } 

    /** 
    * @return Application's {@code SharedPreferences}. 
    */ 
    private SharedPreferences getGCMPreferences(Context context) { 
     // This sample app persists the registration ID in shared preferences, but 
     // how you store the regID in your app is up to you. 
     return getSharedPreferences(MainActivity.class.getSimpleName(), 
       Context.MODE_PRIVATE); 
    } 

    /** 
    * @return Application's version code from the {@code PackageManager}. 
    */ 
    private static int getAppVersion(Context context) { 
     try { 
      PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); 
      return packageInfo.versionCode; 
     } catch (PackageManager.NameNotFoundException e) { 
      // should never happen 
      throw new RuntimeException("Could not get package name: " + e); 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     switch (item.getItemId()) { 
      case R.id.action_settings: 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * Registers the application with GCM servers asynchronously. 
    * <p> 
    * Stores the registration ID and app versionCode in the application's 
    * shared preferences. 
    */ 
    private void registerInBackground() { 
     new AsyncTask() { 

      @Override 
      protected String doInBackground(Object... params) { 
       String msg = ""; 
       try { 
        if (gcm == null) { 
         gcm = GoogleCloudMessaging.getInstance(context); 
        } 
        regid = gcm.register(SENDER_ID); 
        msg = "Device registered, registration ID=" + regid; 
        Toast.makeText(getApplicationContext(), msg, 10).show(); 

        // You should send the registration ID to your server over HTTP, 
        // so it can use GCM/HTTP or CCS to send messages to your app. 
        // The request to your server should be authenticated if your app 
        // is using accounts. 
      //   sendRegistrationIdToBackend(); 

        // For this demo: we don't need to send it because the device 
        // will send upstream messages to a server that echo back the 
        // message using the 'from' address in the message. 

        // Persist the regID - no need to register again. 
        storeRegistrationId(context, regid); 
       } catch (IOException ex) { 
        msg = "Error :" + ex.getMessage(); 
        // If there is an error, don't just keep trying to register. 
        // Require the user to click a button again, or perform 
        // exponential back-off. 
       } 
       return msg; 
      } 

      @Override 
      protected void onPostExecute(Object msg) { 

      } 
     }.execute(null, null, null); 
    } 

    /** 
    * Stores the registration ID and app versionCode in the application's 
    * {@code SharedPreferences}. 
    * 
    * @param context application's context. 
    * @param regId registration ID 
    */ 
    private void storeRegistrationId(Context context, String regId) { 
     final SharedPreferences prefs = getGCMPreferences(context); 
     int appVersion = getAppVersion(context); 
     Log.i(TAG, "Saving regId on app version " + appVersion); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString(PROPERTY_REG_ID, regId); 
     editor.putInt(PROPERTY_APP_VERSION, appVersion); 
     editor.commit(); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
      return rootView; 
     } 
    } 

    private boolean checkPlayServices() { 
     int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     if (resultCode != ConnectionResult.SUCCESS) { 
      if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { 
       GooglePlayServicesUtil.getErrorDialog(resultCode, this, 
         PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
      } else { 
       Log.i("MyActivity", "This device is not supported."); 
       Toast.makeText(getApplicationContext(), "This devise is not supported.", 10).show(); 
       finish(); 
      } 
      return false; 
     } 
     return true; 
    } 

    } 

내 build.gradle입니다. 누군가 내가 문제를 해결하도록 도울 수 있습니까? Gradle을이 종속성을 추가

+1

@ user2078883하지만 그는 Eclipse를 사용하지 않고 Android Studio를 사용하여 질문에 분명하게 언급되어 있습니다. –

+0

http://stackoverflow.com/questions/34592849/java-lang-noclassdeffounderror-com-google-android-gms-common-internal-zzd –

답변

1

시도 :

compile 'com.android.support:support-v4:18.0.0' 

또는 다른 안드로이드 스튜디오 0.4.0에 이상 또는 완전 업그레이드하고 0.3.2에 체류하려는 경우 프로젝트를 다시 빌드합니다. Gradle 플러그인 (https://code.google.com/p/android/issues/detail?id=63366) 0.6 버전의 버그로 인해 새로 추가 된 종속성이 APK에 연결되지 않도록 할 수 있습니다. 그렇게하지 않으면 프로젝트를 정리하지 않는 한입니다.

0

$ANDROID_SDK_DIR\extras\google\m2repository\com\google\android\gms\play-services\4.0.30 

는 또한 0.4.3에 안드로이드 Studio를 업그레이드 당신이 당신의 SDK 디렉토리에이 있는지 확인하십시오. 0.4.2에서 코드는 Java 클래스 파일에서 심볼을 찾지 않지만 잘 컴파일됩니다.

관련 문제