【发布时间】:2014-07-31 18:16:38
【问题描述】:
我在使用 Google Play 服务库时遇到了 IllegalStateException,但我不知道为什么。
我已将 Android SDK 中的 google-play-service-lib 项目复制到我的工作 Git 存储库中,并与我的项目一起导入到工作区中。
我在 Project Proprieties->Android 中签入,google-play-service-lib 已添加到库部分,我看到它附近有一个绿色复选标志。
AndroidManifest.xml 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"
android:resource="@xml/global_tracker"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</activity>
</application>
</manifest>
在我的MainActivity.java 文件中的onCreate() 方法中,执行int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 时会引发异常。
try {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
// Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
LoggerUtils.d(LOG,"Google Play services is available, create Ad");
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
FrameLayout layout = (FrameLayout) findViewById(R.id.FragmentContainer);
layout.addView(adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
} else {
// Google Play services was not available, print reason
LoggerUtils.d(LOG, "Google Play services is not available. " + String.valueOf(resultCode));
}
} catch (Exception e) {
LoggerUtils.e(LOG, e.getMessage());
}
我在 LogCat 中收到此消息,但我无法弄清楚问题所在
07-31 20:54:31.140: E/com.example.test.MainActivity.LOG(3269): The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 5077000 but found 2131034114. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
我从google-play-service-lib 项目的res->values 文件夹中检查了version.xml 中的google_play_services_version,它包含预期的编号5077000。我只能在dump.txt 文件的proguard 文件夹中找到2131034114 的引用。这个文件是从我之前构建的用于发布的 apk 中创建的,其中 google_play_services_version 是项目的一部分,但只引入了 Google Analytics 而不是 AdMob。对于我所做的测试,我总是构建一个调试 apk,据我所知,这不是用 proguard 完成的。
我多次尝试从工作区清理项目,然后关闭并再次打开 Eclipse。
我什至卸载了 Google SDK、ADT 并再次安装它们并从工作区中删除了 google-play-service-lib 项目,删除它并在重新安装后从 Google SDK 中复制它。
我在运行 Android 4.3(Galaxy S3 上的三星股票 ROM)和 Android 4.4.4(带有 CyanogenMod 的三星 Galaxy S2)的手机上进行了测试,在两部手机上我都遇到了同样的错误。
【问题讨论】:
-
会不会是为
google-play-service-lib项目设置的构建版本导致了这种行为?
标签: java android eclipse git google-play-services