5

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

现在构建系统将转向 Gradle,我希望有类似的高级教程供参考。那里的大多数教程(比如这个)只处理基本的东西,但我想知道如何执行一些“高级”的事情,比如在构建过程中自动替换代码中的值(这样我就可以拥有多个 APK 变体)。

4

1 回答 1

4

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

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

}

在调试模式下运行 Robolectric

localTest.doFirst {
  jvmArgs '-Xdebug',
        '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005'
}
于 2013-07-06T07:58:05.343 回答