android - 无法使用 Gradle 构建 Android 项目(使用 Android 注释)

标签 android gradle android-annotations

我正在尝试使用 gradle 通过 Android 注释构建我的 Android 项目,但我仍然遇到错误。在我的 java 类中找不到 AA。

同样在 gradle 文件中我得到了一些提示:

versionName "1.0"<- 'versionName' 不能应用于 '(java.lang.String)'

“build”中的“main”不能应用于“(groovy.lang.Closure)”

        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java', 'GENERATED_FOLDER']
            resources.srcDirs = ['src/main/resources']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
        }

下面是我的完整 gradle 脚本:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        // replace with the current version of the Android plugin
        classpath 'com.android.tools.build:gradle:0.9.2'
        // the latest version of the android-apt plugin
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.1'
    }
}

repositories {
    mavenCentral()
    mavenLocal()
}

apply plugin: 'android'
apply plugin: 'android-apt'
def AAVersion = '3.0.1'

dependencies {
    // Android annotations
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"

    compile 'com.android.support:appcompat-v7:19.+'
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // ORMLite
    compile 'com.j256.ormlite:ormlite-android:4.46'

    // Google Guava
    compile 'com.google.guava:guava:16.0.1'
}

apt {
    arguments {
        androidManifestFile variant.processResources.manifestFile
        resourcePackageName 'pl.grzeslowski.weaselmoney'
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java', 'GENERATED_FOLDER']
            resources.srcDirs = ['src/main/resources']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
        }
    }
}

这是我在 Android Studio 中控制台的日志:

Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0
:weasel_moneyWeaselMoney:help

Welcome to Gradle 1.10.

To run a build, run gradle <task> ...

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle --help

BUILD SUCCESSFUL

Total time: 6.111 secs

Process finished with exit code 0

最佳答案

不要把你的 AA 配置放在你的 General build.gradle 上 把它放在 build.gradle 里面的你的 app 文件夹里,看@这个例子

apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '3.2'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName 'com.example.app'
        logLevel 'TRACE' //Use this to get AA log
        logAppenderConsole 'true' //Use this to get AA log
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt')
        }

        debug {
            debuggable true
        }
    }
    packagingOptions {
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
    }
}

dependencies {

    //Libs
    compile fileTree(dir: 'libs', include: ['*.jar'])

    //Dependency
    compile 'com.android.support:appcompat-v7:20.0.0'
    compile 'org.springframework.android:spring-android-rest-template:2.0.0.M1' //If you're using REST
    compile 'com.google.code.gson:gson:1.7.2'
    compile 'org.codepond:wizardroid:1.3.0'

    //Android Annotations
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}

关于android - 无法使用 Gradle 构建 Android 项目(使用 Android 注释),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22832711/

相关文章:

java - 为什么新的源集资源不在gradle的buildDir中

android - 如何在一段时间后更改 imageView 上的图像

java - Android Firebase 如何处理实时服务器到本地数据库的连接

android - QT 应用程序在 Android 10 设备上的 std::thread 析构函数处崩溃

android - 在 Scala 和 Gradle 中使用 AndroidAnnotations

java - Android 注释在库项目中不起作用

android - 没有使用maven将注释处理(生成的)源编译成apk

android - 如何避免 context.getSystemService(Context.CAMERA_SERVICE) 中的内存泄漏?

java - 有没有办法将 jar 库指向与 tomcat 8 中的 war 不同的位置以减少 war 规模?

android - 提取 `.aab` 文件时出错 : Invalid dex file indices, 期望文件 'classes٢.dex' 但找到 'classes2.dex'