TL; DR



关于如何在我的图书馆中编译GoogleAnalytics的使用,我已经奋斗了一个多星期。

目标是:
1.在Android库中编译并运行GoogleAnalytics
2.维护应用程序上下文(Singletone?)


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'
}
}


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"



在我的库build.gradle中,我添加了以下类路径:
buildscript {
    ....
    dependencies {
        classpath 'com.google.gms:google-services:3.1.0'
    }
}

这是正确的吗?

大多数通用库都依靠其用户从应用程序类初始化该库。就像是
class MyApp : Application() {

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

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



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

非常感谢!

最佳答案

buildscript依赖关系是构建脚本本身的依赖关系,而不是库的依赖关系。这就是为什么它不包含在您的最终版本中的原因。

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

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

08-18 18:17