摇篮。如何在Android应用程序在编译之前生成源$ C $ C [英] Gradle. How to generate source code before compilation in android app
问题描述
在我的Android应用程序,我需要生成源$ C $ c和在应用程序中使用它。结果
对于我创建了源代码的生成任务的 genSources (使用教程)。它正常工作,如果单独运行它。
In my android application I need to generate source code and use it in the app.
For that I created task genSources (using tutorials) for source generation. It works correctly if run it separately.
在我来说,我需要自动运行的源$ C $ C产生。
In my case I need to run source code generation automatically.
从教程中,我发现了下面的命令:
From tutorial I found out the following command:
compileJava.dependsOn(genSources)
但未知命令的结果的应用插件:'com.android.library的结果
抛出的gradle以下异常:结果错误:(35,0)项目找不到财产compileJava'':数据。
but that is unknown command for the
apply plugin: 'com.android.library'
gradle throws following exception:
Error:(35, 0) Could not find property 'compileJava' on project ':data'.
看起来它可以搭配的应用插件:Java的的结果
但我不能使用这两个插件一起
Looks like it can be used with apply plugin: 'Java'
but I cannot use these 2 plugins together
我怎样才能解决这个问题,在编译之前生成所需的源$ C $ C?
How can I solve this issue and generate needed source code before compilation?
的build.gradle
apply plugin: 'com.android.library'
configurations {pmd}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
buildscript {
repositories {
maven {
url "http://repo1.maven.org/maven2/"
}
}
dependencies {
classpath group: 'net.sourceforge.fmpp', name: 'fmpp', version: '0.9.14'
}
ant.taskdef(name: 'fmpp', classname:'fmpp.tools.AntTask', classpath: buildscript.configurations.classpath.asPath)
}
task genSources << {
println "Generating sources...."
ant.fmpp configuration:"src/main/resources/codegen/config.fmpp",
sourceRoot:"src/main/resources/codegen/templates",
outputRoot:"target/generated-sources/main/java";
}
compileJava.dependsOn(genSources)
sourceSets {
main {
java {
srcDir 'target/generated-sources/main/java'
}
}
}
dependencies {
...
}
更新时间:
我找到了一些解决方案,它至少不会抛出异常。
I was found some solution which at least not throw exception
gradle.projectsEvaluated {
compileJava.dependsOn(genSources)
}
然后我执行构建的gradle
但没有任何反应。
推荐答案
通过gradle这个2.2+这应该工作:
With gradle 2.2+ this should work:
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn genSources
}
如果你也希望它当你评估发生(例如,同步与Android中的gradle工作室项目时),你可以做这样的:
If you also want it to happen when you evaluate (e.g. when syncing your project with gradle in android studio) you can do it like this:
gradle.projectsEvaluated {
preBuild.dependsOn genSources
}
这篇关于摇篮。如何在Android应用程序在编译之前生成源$ C $ C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!