简体   繁体   English

摇篮。 如何在android app中编译前生成源代码

[英]Gradle. How to generate source code before compilation in android app

In my android application I need to generate source code and use it in the app.在我的 android 应用程序中,我需要生成源代码并在应用程序中使用它。
For that I created task genSources (using tutorials) for source generation.为此,我为源代码生成创建了任务genSources (使用教程)。 It works correctly if run it separately.如果单独运行它可以正常工作。

In my case I need to run source code generation automatically.就我而言,我需要自动运行源代码生成。

From tutorial I found out the following command:从教程中我发现了以下命令:

compileJava.dependsOn(genSources) compileJava.dependsOn(genSources)

but that is unknown command for the但这是未知的命令
apply plugin: 'com.android.library'应用插件:'com.android.library'
gradle throws following exception: gradle 抛出以下异常:
Error:(35, 0) Could not find property 'compileJava' on project ':data'.

Looks like it can be used with apply plugin: 'Java'看起来它可以与应用插件一起使用:'Java'
but I cannot use these 2 plugins together但我不能同时使用这两个插件

How can I solve this issue and generate needed source code before compilation?如何在编译前解决此问题并生成所需的源代码?

build.gradle构建.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 {
    ...
}

UPDATED更新

I was found some solution which at least not throw exception我找到了一些至少不会抛出异常的解决方案

gradle.projectsEvaluated {
    compileJava.dependsOn(genSources)
}

Then I execute gradle build but nothing happens然后我执行gradle build但什么也没发生

With gradle 2.2+ this should work:使用 gradle 2.2+ 这应该可以工作:

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn genSources
}

If you also want it to happen when you evaluate (eg when syncing your project with gradle in android studio) you can do it like this:如果您还希望在评估时发生这种情况(例如,在 android studio 中将项目与 gradle 同步时),您可以这样做:

gradle.projectsEvaluated {
    preBuild.dependsOn genSources
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM