在Android Studio中引用的apk从其他模块类 [英] In Android Studio refer to .apk classes from other module
问题描述
这个问题来源于这样的事实,我不是一个摇篮专家,我来自Eclipse中的Maven +背景。
This question stems from the fact that I am not a Gradle expert and I come from Eclipse + Maven background.
在Maven的,我可以参考我的apk的项目从另一个(一Robolectric分项目为实例),作为 Android的Maven的插件
在推无论 .apk文件
和(UN-dexed)的.jar
在我的本地库。
In Maven, I was able to refer to my apk project from another one (a Robolectric sub-project for instance), as the android-maven-plugin
was pushing both .apk
and (un-dexed) .jar
in my local repository.
我怎样才能达到同样的Android Studio中+摇篮?
How can I achieve the same in Android Studio + Gradle?
- 我尝试添加
应用插件:Java的
来我的应用程序的项目,但Java插件不与Android插件兼容 -
我试图建立我的Java插件应用程序的一个子模块只能与
的build.gradle
,但R.java不产生这样的:
- I tried to add
apply plugin: 'java'
to my app project but the java plugin is not compatible with the android plugin. I tried to build a sub-module of my app with the java plugin only with
build.gradle
, but R.java is not generated this way:
sourceSets {
main {
java.srcDirs = parent.android.sourceSets.main.java.srcDirs
}
}
是否有任何其他方式做到这一点?
Is there any other way to do this?
EDIT4:发布解决方案
posted solution.
推荐答案
在我的编辑解决方案实际效果很好。我在此报告的解决方案。 当心,它硬codeS的类文件的路径,我无法找出如何使用其中的$ buildType(有没有办法?)
The solution in my edits actually works well. I report it as solution here. Watch out, it hard-codes the path for the class files, I wasn't able to find out how to use the $buildType in it (is there a way?)
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
}
和转诊模块(S),将宣布一个新的依赖关系是这样的:
And the referring module(s), will declare a new dependency this way:
compile project(path: ':app', configuration: 'jarOfApk')
这篇关于在Android Studio中引用的apk从其他模块类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!