android - 在 IDE 外部使用 Gradle 构建 APK(从 Ant 迁移)

标签 android eclipse ant gradle

我一直在使用本教程自学如何仅使用命令行(和 Ant)在 Eclipse 之外构建 APK - http://www.androidengineer.com/2010/06/using-ant-to-automate-building-android.html

现在构建系统将转向 Gradle,我希望有类似的高级教程供引用。大多数教程 ( like this one ) 只涉及基本内容,但我想知道如何执行一些“高级”操作,例如在构建期间自动替换代码中的值(这样我就可以拥有多个 APK 变体)。

最佳答案

谷歌提供的标准示例在这里

http://tools.android.com/tech-docs/new-build-system/gradle-samples-0.4.2.zip?attredirects=0&d=1

要在代码中自动更改值,请使用 BuildConfig 类。示例在上面的链接中。

这里解释了变体 http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants

更新

因为这个例子有点陈旧,这里是更新版本的 pasetbin http://pastebin.com/FmcCZwA5

主要区别在于插件提供的 Robolectric 支持,以及从 SDK 内部 repo 获取的支持库

旧版本

Robolectric 和 AndroidAnnotations 的基本示例

使用关系

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:0.4'
  }
}

apply plugin: 'android'

repositories {
  mavenCentral()
  maven {
    url 'https://oss.sonatype.org/content/repositories/snapshots/'
  }
}

使用 AndroidAnnotation 处理器、Robolectric 本地测试和 Jackson

configurations {
  compile
  testLocalCompile.extendsFrom(compile)
  androidannotations.extendsFrom(compile)
}

dependencies {
  compile files('libs/android-support-v4.jar')
  compile 'org.androidannotations:androidannotations-api:3.0-SNAPSHOT'
  compile 'com.github.japgolly.android:svg-android:2.0.3'
  compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.12'
  testLocalCompile 'junit:junit:4.8.2'
  testLocalCompile 'org.robolectric:robolectric:2.2-SNAPSHOT'
  testLocalCompile 'com.google.android:android:4.0.1.2'
  testLocalCompile 'com.google.android:support-v4:r6'
  testLocalCompile 'org.roboguice:roboguice:2.0'
  androidannotations 'org.androidannotations:androidannotations:3.0-SNAPSHOT'
}

android {
  compileSdkVersion 17
  buildToolsVersion "17.0.0"

配置标准仪器测试

  defaultConfig {
    minSdkVersion 7
    targetSdkVersion 16
    testPackageName "com.mypackage.myapp.test"
    testInstrumentationRunner "com.maypackage.myapp.test.Runner"
  }
}

在所有变体上调用 AndroidAnnotations 处理器

afterEvaluate { project ->
  android.applicationVariants.each { variant ->
    variant.javaCompile.options.compilerArgs += [
            '-classpath', configurations.compile.asPath,
            '-processorpath', configurations.androidannotations.asPath,
            '-processor', 'org.androidannotations.AndroidAnnotationProcessor',
            '-AandroidManifestFile=' + variant.processResources.manifestFile
    ]
  }
}

为 Robolectric 本地测试定义源集

sourceSets {
  testLocal {
    java.srcDir file('src/test/java')
    resources.srcDir file('src/test/resources')
  }
}

本地 Robolectric 测试任务

task localTest(type: Test, dependsOn: assemble) {
  testClassesDir = sourceSets.testLocal.output.classesDir

  android.sourceSets.main.java.srcDirs.each { dir ->
    def buildDir = dir.getAbsolutePath().split('/')
    buildDir =  (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')

    sourceSets.testLocal.compileClasspath += files(buildDir)
    sourceSets.testLocal.runtimeClasspath += files(buildDir)
}

classpath = sourceSets.testLocal.runtimeClasspath

在 Debug模式下运行 Robolectric

localTest.doFirst {
  jvmArgs '-Xdebug',
        '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005'
}

关于android - 在 IDE 外部使用 Gradle 构建 APK(从 Ant 迁移),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17499611/

相关文章:

android - 在Android应用程序开发中将OpenCV与NDK结合使用

android - 如何在ListView中为ImageView设置onClick事件

java - Eclipse 启动时出现 NumberFormatException

java - Apache Tomcat 8 : Getting Error while creating new Server

java - Eclipse 换行不换行

eclipse - 将复杂的 Ant 脚本转换为一组 Eclipse 项目的合理方法是什么?

java - 目标中有多个子子

Android 12 Kotlin - 强烈考虑使用 FLAG_IMMUTABLE,仅当某些功能依赖于 PendingIntent 可变时才使用 FLAG_MUTABLE

java - spring-android-auth maven Artifact 构建错误

java - Spring Security Apache Ant URL 对扩展名或后缀进行过滤