【问题标题】:In Android Studio refer to .apk classes from other module在 Android Studio 中引用其他模块中的 .apk 类
【发布时间】:2014-03-02 05:53:58
【问题描述】:

这个问题源于我不是 Gradle 专家,我来自 Eclipse + Maven 背景。

在 Maven 中,我能够从另一个项目(例如 Robolectric 子项目)引用我的 apk 项目,因为 android-maven-plugin 在我的本地存储库。

如何在 Android Studio + Gradle 中实现相同的功能?

  • 我尝试将apply plugin: 'java' 添加到我的应用项目中,但java 插件与android 插件不兼容。
  • 我尝试仅使用 build.gradle 使用 java 插件构建我的应用程序的子模块,但 R.java 不是以这种方式生成的:

    sourceSets {
        main {
            java.srcDirs = parent.android.sourceSets.main.java.srcDirs
        }
    } 
    

还有其他方法吗?

EDIT4:已发布解决方案。

【问题讨论】:

标签: android gradle android-studio android-gradle-plugin


【解决方案1】:

我编辑的解决方案实际上效果很好。我在这里将其报告为解决方案。 注意,它对类文件的路径进行了硬编码,我不知道如何在其中使用 $buildType(有什么办法吗?)

def variantName = "debug" // init to debug

apply plugin: 'android'

android {
    ...
    applicationVariants.all { variant ->
        variantName = variant.name
        buildDebugJar.dependsOn variant.packageApplication
    }
}

// Builds the jar containing compiled .java files only (no resources).
task buildJar(type: Jar) {
    description 'Generates the jar file along with the apk.'
    from new File("$project.buildDir/classes/$variantName")
}

configurations {
    jarOfApk
    transitive = false
}

artifacts {
    jarOfApk buildJar
}

而引用模块将以这种方式声明一个新的依赖项:

compile project(path: ':app', configuration: 'jarOfApk')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2020-06-13
    • 2022-06-22
    • 2012-05-05
    • 2021-06-09
    相关资源
    最近更新 更多