C
C
chrispsow2019-07-17 17:54:58
Java
chrispsow, 2019-07-17 17:54:58

How to properly integrate AppMetrica into an Andorid application?

I have a simple webview application:
MainActivity.java

package com.asd.asdf;

import android.annotation.TargetApi;
import android.app.Application;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.yandex.metrica.YandexMetrica;
import com.yandex.metrica.YandexMetricaConfig;


public class MainActivity extends AppCompatActivity {
    private WebView mainWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainWebView = findViewById(R.id.webView);
        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mainWebView.loadUrl("https://site.com");
        mainWebView.setWebViewClient(new WebViewClient());
    }

    private class MyWebViewClient extends WebViewClient {
        @TargetApi(Build.VERSION_CODES.N)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.getUrl().toString());
            return true;

        }
    }

    @Override
    public void onBackPressed() {
        if (mainWebView.canGoBack()) {
            mainWebView.goBack();
        } else {
            super.onBackPressed();
        }

    }
}

public class MyApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // Creating an extended library configuration.
        YandexMetricaConfig config = YandexMetricaConfig.newConfigBuilder("API").build();
        // Initializing the AppMetrica SDK.
        YandexMetrica.activate(getApplicationContext(), config);
        // Automatic tracking of user activity.
        YandexMetrica.enableActivityAutoTracking(this);
    }
}

At the very bottom, a class is added to initialize the AppMetrica library, just like they say : An
5d2f3499915bd971489737.jpeg
error occurs
5d2f34cf34658040668568.jpeg
I created a file MyApp.java, which lies next to the main MainActivity.java and moved the content from the main
package com.asdf.asdf;

import android.app.Application;
import com.yandex.metrica.YandexMetrica;
import com.yandex.metrica.YandexMetricaConfig;

public class MyApp extends Application {
 
    @Override
    public void onCreate() {
        super.onCreate();
        // Creating an extended library configuration.
        YandexMetricaConfig config = YandexMetricaConfig.newConfigBuilder("api").build();
        // Initializing the AppMetrica SDK.
        YandexMetrica.activate(getApplicationContext(), config);
        // Automatic tracking of user activity.
        YandexMetrica.enableActivityAutoTracking(this);
    }
}

There are no more errors left, the application starts up, I checked it from the built-in emulator, it only writes that Class MyApp is never usedit is a warning.
But in the AppMetrica dashboard, I do not see any information that someone used the application.
And it's not clear if I connected everything correctly

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-07-18
@chrispsow

The problem is that you didn't understand what MyApp is.
You need to specify it in the manifest.
https://developer.android.com/guide/topics/manifes...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question