Google Play Services V2库中的NoClassDefFoundError

时间:2012-12-03 20:22:22

标签: android google-maps google-play-services

在按照official tutorial后尝试使用Google Play Services V2库时出现以下异常。

java.lang.NoClassDefFoundError: com.google.android.gms.R$string
at com.google.android.gms.common.GooglePlayServicesUtil.b(Unknown Source)
at com.google.android.gms.common.GooglePlayServicesUtil.a(Unknown Source)
at com.google.android.gms.common.GooglePlayServicesUtil.getErrorDialog(Unknown Source)

我哪里出错了?

8 个答案:

答案 0 :(得分:34)

我也有同样的问题,我正确地按照步骤解决了这个问题

首先(从Google Developer Console设置您的项目) 转到API Console - Google Code

创建项目如图所示 enter image description here

单击“创建”,然后您将要求添加项目名称,如图所示

enter image description here

一旦您创建项目时间选择我们需要使用的服务,在这种情况下我们需要android v2地图,因此从服务中选择 Google Maps Android API v2 ,如图所示,{{0 }}

现在转到Api Access并创建您的OAuth 2.0。在相应的字段中提供您的包名称和SHA1指纹。 enter image description here

完成OAuth 2.0后,我们就可以使用您的API密钥了 enter image description here

现在创建一个Android项目,其中包含在创建OAuth 2.0时使用的相同包名称。并检查您是否在Android SDK Manager中拥有 Google Play服务,否则请安装Google Play服务。 enter image description here

安装Google Playservice后,您会在Android YourSdkpath \ extras \ google \ google_play_services 中找到一个Google Play库。导入该项目到您的工作区并将其作为参考库到您的项目 enter image description here

enter image description here

enter image description here

之后将相应的java和xml文件放入项目中。

<强> MainActivity.java

package yourpackage;//Package name used while creating the Api key


import com.google.android.gms.common.ConnectionResult;
 import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity {
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Getting status
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    // Showing status
    if(status==ConnectionResult.SUCCESS)
    {
        SupportMapFragment supportMapFragment = (SupportMapFragment) 
                getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting a reference to the map
        googleMap = supportMapFragment.getMap();
    }
    else{

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

<强> activity_main.xml中

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
   android:name="com.google.android.gms.maps.SupportMapFragment"
   android:layout_width="wrap_content"
   android:layout_height="match_parent" />

<强>的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="yourpackage"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
   <permission
    android:name="yourpackage.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>

<uses-permission android:name="yourpackage.permission.MAPS_RECEIVE"/>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="yourpackage.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
      <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="YourAPIkey"/>

希望它能帮到你

答案 1 :(得分:32)

版本2包含资源,必须作为库项目导入。有关如何在各种IDE中执行此操作,请参阅http://developer.android.com/google/play-services/setup.html上的说明。

资源适用于GooglePlayServicesUtil.getErrorDialog(),用于指导用户安装,更新,启用Google Play服务(如果设备上没有该服务)。

答案 2 :(得分:11)

对于Android Studio:

1)打开模块设置:

2)从SDK

添加(导入)模块google_play_service_lib

Picture

3)模块google_play_service_lib检查为库

4)从SDK

添加库google_play_service.jar

5)完成

Picture

答案 3 :(得分:2)

如果您已升级SDK并出现此类错误,请记住:

  • 如果您在eclipse项目中复制此文件,请使用新版本的google-play-services.jar
  • project.properties
  • 中设置target = android-19
  • 对于较新的API(我使用API​​ 19),您可能需要添加以下代码

AndroidManifest.xml

<application
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>

我还使用 ourSdkpath \ extras \ google \ google_play_services 作为上面导入的Eclipse库。

答案 4 :(得分:1)

我在导入google play服务库时选中“将项目复制到工作区”,解决了这个错误 更多信息:http://developer.android.com/google/play-services/setup.html

答案 5 :(得分:1)

如果您正在使用

compile 'com.google.android.gms:play-services:9.0.2'

将其更改为个别内容,例如

compile 'com.google.android.gms:play-services-location:9.0.2'

还添加 如果您有compile 'com.android.support:multidex:1.0.1'

,请multiDexEnabled=true

答案 6 :(得分:0)

在没有Gradle的构建项目时,我遇到与Android Studio相同的问题。 GooglePlayService项目中AndroidManifest.xml中的包名称可能存在问题。 它应该是 package =“com.google.android.gms”

当我将库项目添加为“新模块”而不是“导入模块”时,会出现

问题。

enter image description here

当您选择新模块时,Android Studio会显示对话框以指定“模块名称”和“包名称”,默认情况下“包名称”看起来像“com.example.MODULE_NAME_YOU_SPECIFED”,这是错误 。很容易错过它,因为项目建立成功。

enter image description here

当您通过“导入模块”添加库项目时,一切都应该没问题,因为Android Studio只是添加项目而不对库项目的源代码进行任何更改。

答案 7 :(得分:0)

我也面临同样的问题。如果您在模拟器中运行应用程序,则可能会遇到此错误,因为在大多数模拟器中没有安装google playService(Play商店应用)。

因此,请尝试在安装了playService app的android设备中运行您的应用程序。

解决方案: - 在try...catch中添加错误部分代码然后尝试它也可以在模拟器中工作。

注意: - Google Messaging服务需要palyservice。否则抛出错误SERVICE_NOT_AVAILABLE