android - 带有谷歌分析的独立安卓库

标签 android google-analytics kotlin android-library

长话短说

Any chance to compile Google Analytics inside library module without any help from application module?

我为如何在我的库中编译使用 GoogleAnalytics 而苦苦挣扎了一个多星期。

目标是: 1.在android库中编译运行GoogleAnalytics 2.维护一个应用上下文(Singletone?)

Here is how i'm trying to compile GoogleAnalytics in my library build.gradle file

buildscript {

ext {
    support = '27.0.2' // https://developer.android.com/topic/libraries/support-library/revisions.html
    kotlin = '1.2.20' // https://kotlinlang.org/docs/reference/using-gradle.html
    playServices = '11.8.0'
}

repositories {
    google()
    jcenter()
    mavenCentral()
}

dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin"
    classpath 'com.google.gms:google-services:3.1.0'
}
}

And the code that handles all analytics:

class GenericSettingsAnalytics private constructor(context : Context) {

private val tracker = GoogleAnalytics.getInstance(context).newTracker(R.xml.global_tracker)

companion object {

    private var instance : GenericSettingsAnalytics? = null

    fun getInstance(context: Context) : GenericSettingsAnalytics {
        if (instance == null) {
            instance = GenericSettingsAnalytics(context)
            instance!!.sendInitAnalytics(context)
        }
        return instance!!
    }
}

fun sendEvent(category: String, action: String) {
    tracker.send(HitBuilders.EventBuilder().setCategory(category).setAction(action).build())
}

fun sendInitAnalytics(context: Context) {
    tracker.send(HitBuilders.EventBuilder()
            .setCategory(ANALYTICS_CATEGORIES.CATEGORY_APP_INFO.categoryName)
            .setAction(ANALYTICS_ACTIONS.ACTION_APP_INIT.actionName)
            .setLabel(context.packageName)
            .build())
}

}

很简单。 在调试中一切都很好,但是当将库上传到 bintray 时(使用它的官方编译)我得到以下异常:

FATAL EXCEPTION: main
Process: com.udioshi85.genericsettings, PID: 12398
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/analytics/GoogleAnalytics;
    at com.oshi.libgenericsettings.GenericSettingsAnalytics.<init>(GenericSettingsAnalytics.kt:14)
    at com.oshi.libgenericsettings.GenericSettingsAnalytics.<init>(GenericSettingsAnalytics.kt:12)
    at com.oshi.libgenericsettings.GenericSettingsAnalytics$Companion.getInstance(GenericSettingsAnalytics.kt:22)
    at com.oshi.libgenericsettings.GenericSettingsLib$Companion.initAnalytics(GenericSettingsLib.kt:20)
    at com.oshi.libgenericsettings.GenericSettingsLib$Companion.init(GenericSettingsLib.kt:16)
    at com.oshi.genericsettings.GenericSettingsApp.onCreate(GenericSettingsApp.kt:10)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5827)
    at android.app.ActivityThread.-wrap1(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1673)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:172)
    at android.app.ActivityThread.main(ActivityThread.java:6637)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.analytics.GoogleAnalytics"

Some thougts:

在我的库 build.gradle 中,我添加了以下类路径:

buildscript {
    ....
    dependencies {
        classpath 'com.google.gms:google-services:3.1.0'
    }
}

是否正确?

大多数常见的库都依赖于它们的用户从应用程序类中初始化库。有点像

class MyApp : Application() {

override fun onCreate() {
    super.onCreate()
    MyLibrary.init(this)
}
}

我已经试过了。但仍然遇到相同的异常。也许我做错了什么?

Last thing, I've seen & read lots of examples on how to integrate GoogleAnalytics. The important thing is that my users should not add anything in their build.gradle except my compile/implementation library.

如果有人能帮助我,我将不胜感激。

非常感谢!

最佳答案

buildscript 依赖项是构建脚本本身的依赖项,而不是库的依赖项。这就是为什么它没有包含在您的最终构建中。

您也可以通过将 classpath 依赖项添加到应用程序来解决此问题。

buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:3.1.0'
    }
}

关于android - 带有谷歌分析的独立安卓库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48516133/

相关文章:

android - 获取 Android 应用程序启动的端口

google-analytics - 如何在 Google Analytics 中跟踪 "Open New Tab"流量

Android 将宽度设置为 ConstraintLayout.LayoutParams.MATCH_PARENT 给出 -1

android - MapBox SDK如何获取marker的点击事件?

android - 工具栏字幕被剪切(不适合高度)

java - 如何使 SVG 在 Android 中可点击?

javascript - Google Analytics - 将 'ga' cookie 域设置为 '.www.example.com'

javascript - 检查是否加载了 analytics.js

android - LocationSettings 请求对话框再次出现

android - 重复构建失败 "To use Coroutine features, you must add ` ktx`......"