參考 https://developers.google.com/analytics/devguides/collection/android/v4/ 和 https://developers.google.com/admob/android/app 資訊,更新 AndroidManifest.xml 位置,添加:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
接著在 build.gradle (Project) 添加:
classpath 'com.google.gms:google-services:2.0.0-alpha6'
在 build.gradle (app) 添加:
dependencies {
//...
compile 'com.google.android.gms:play-services-analytics:8.4.0'
}
apply plugin: 'com.google.gms.google-services'
結果一直出錯:Gradle sync failed: Unable to load class 'com.android.sdklib.repository.FullRevision'. 這種很 general 的訊息。
甚至跑去看看 googlesample 檔案,如:
分別查看:build.gradle、app/build.gradle 和 gradle/wrapper/gradle-wrapper.properties 三個檔案。
改採用:
dependencies {
//classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle-experimental:0.4.0'
classpath 'com.android.tools.build:gradle:2.0.0-beta5'
classpath 'com.google.gms:google-services:2.0.0-beta5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
仍是一樣的現象,最後靠直覺,嘗試亂改 classpath 'com.google.gms:google-services:1.0.0+' 後,會看到錯誤訊息引導說,可以查看 metadata 得知有哪些版本可用:
https://jcenter.bintray.com/com/google/gms/google-services/maven-metadata.xml
就這樣,直接挑沒有 beta/alpha 字眼:
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle-experimental:0.4.0'
classpath 'com.google.gms:google-services:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
以及 gradle/wrapper/gradle-wrapper.properties 使用:
#distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-all.zip
如此一來,終於解掉編譯問題。接續就可以參考 Google Analytics SDK/Admob SDK 的文件,例如產生 google-services.json 以及 AnalyticsApplication extends Application 等用法等。
最後,我自己的用法:
AndroidManifest.xml:
...
<application
android:name=".MyApplication"
...
>
...
</application>
MyApplication.java:
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
public class MyApplication extends Application {
private Tracker mTracker;
synchronized public Tracker getDefaultTracker() {
if (mTracker == null) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
// To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
//mTracker = analytics.newTracker(R.xml.global_tracker);
mTracker = analytics.newTracker("UA-XXXXXXX-XX");
}
return mTracker;
}
}
build.gradle (app):
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
// Google Analytics & Admob
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.google.android.gms:play-services-ads:8.4.0'
compile 'com.google.android.gms:play-services-analytics:8.4.0'
}
build.gradle (Project):
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle-experimental:0.4.0'
classpath 'com.google.gms:google-services:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
MainActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication application = (MyApplication) getApplication();
Tracker mTracker = application.getDefaultTracker();
mTracker.setScreenName("main");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());
//...
}
沒有留言:
張貼留言