将Google Analytics(分析)添加到Flutter应用中的问题

时间:2019-01-31 08:31:57

标签: android-studio google-analytics flutter

我想将Google Analytic(分析)添加到我的flutter应用程序中。 这是我的休闲之地(Add Analytics to Your Android App

当我执行链接帮助的步骤时,在构建过程中出现错误, 而且我的IDE无法解决这些导入:

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;

我将我的项目文件放在这里:

MainActivity.java:

package com.example.flutterapp;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;

public class MainActivity extends FlutterActivity  {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
  }


  // Obtain the shared Tracker instance.
  AnalyticsApplication application = (AnalyticsApplication) 
  getApplication();
  private static Tracker mTracker = application.getDefaultTracker();

  mTracker.setScreenName("home");
  mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}

AnalyticsApplication.java:

package com.example.flutterapp;

import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;

public class AnalyticsApplication extends Application {

private static GoogleAnalytics sAnalytics;
private static Tracker sTracker;

@Override
public void onCreate() {
    super.onCreate();

    sAnalytics = GoogleAnalytics.getInstance(this);
}

/**
 * Gets the default {@link Tracker} for this {@link Application}.
 * @return tracker
 */
 synchronized public Tracker getDefaultTracker() {
    // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
    if (sTracker == null) {
        sTracker = sAnalytics.newTracker(R.xml.global_tracker);
    }

    return sTracker;
 }
}

我在构建时遇到的错误:

...............................

Launching lib\main.dart on SM J320F in debug mode...
Initializing gradle...
[{"event":"app.progress","params":{"appId":"11b94d0e-10f8-4ea3-aa07-9c1df09f1d27","id":"1","progressId":null,"message":"Resolving dependencies..."}}]Resolving dependencies...

Gradle task 'assembleDebug'...
D:\projects\flutter_test\flutter_app\android\app\src\main\java\com\example\flutterapp\MainActivity.java:22: error: <identifier> expected
    mTracker.setScreenName("home");
                          ^
D:\projects\flutter_test\flutter_app\android\app\src\main\java\com\example\flutterapp\MainActivity.java:22: error: illegal start of type
    mTracker.setScreenName("home");
                           ^
D:\projects\flutter_test\flutter_app\android\app\src\main\java\com\example\flutterapp\MainActivity.java:23: error: <identifier> expected
    mTracker.send(new HitBuilders.ScreenViewBuilder().build());
                 ^
D:\projects\flutter_test\flutter_app\android\app\src\main\java\com\example\flutterapp\MainActivity.java:23: error: illegal start of type
    mTracker.send(new HitBuilders.ScreenViewBuilder().build());
                  ^
D:\projects\flutter_test\flutter_app\android\app\src\main\java\com\example\flutterapp\MainActivity.java:23: error: ')' expected
    mTracker.send(new HitBuilders.ScreenViewBuilder().build());
                     ^
D:\projects\flutter_test\flutter_app\android\app\src\main\java\com\example\flutterapp\MainActivity.java:23: error: ';' expected
    mTracker.send(new HitBuilders.ScreenViewBuilder().build());
                                 ^
D:\projects\flutter_test\flutter_app\android\app\src\main\java\com\example\flutterapp\MainActivity.java:23: error: invalid method declaration; return type required
    mTracker.send(new HitBuilders.ScreenViewBuilder().build());
                                  ^
D:\projects\flutter_test\flutter_app\android\app\src\main\java\com\example\flutterapp\MainActivity.java:23: error: ';' expected
    mTracker.send(new HitBuilders.ScreenViewBuilder().build());
                                                     ^
8 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 22s
Finished with error: Gradle task assembleDebug failed with exit code 1

...............................

how Can I solve this problem?
please help me, thank you.

1 个答案:

答案 0 :(得分:0)

我认为是因为AndroidX compatibility。您可以尝试更改为旧版本的依赖关系。那帮助了我一些依赖。