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

nik*_*3ro 5 eclipse ant android gradle

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

现在构建系统将转向Gradle我希望有类似的高级教程供参考.大多数教程(像这一个)只涉及基本的东西,但我想知道如何执行一些"高级"的东西,比如在构建期间自动替换代码中的值(这样我就可以拥有多种APK版本).

rob*_*ter 4

Google 提供的标准示例在这里

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 内部存储库获取的支持库

旧版本

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/'
  }
}
Run Code Online (Sandbox Code Playgroud)

使用 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"
Run Code Online (Sandbox Code Playgroud)

配置标准仪器测试

  defaultConfig {
    minSdkVersion 7
    targetSdkVersion 16
    testPackageName "com.mypackage.myapp.test"
    testInstrumentationRunner "com.maypackage.myapp.test.Runner"
  }
}
Run Code Online (Sandbox Code Playgroud)

在所有变体上调用 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
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

定义 Robolectric 本地测试的源集

sourceSets {
  testLocal {
    java.srcDir file('src/test/java')
    resources.srcDir file('src/test/resources')
  }
}
Run Code Online (Sandbox Code Playgroud)

本地 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
Run Code Online (Sandbox Code Playgroud)

}

在调试模式下运行 Robolectric

localTest.doFirst {
  jvmArgs '-Xdebug',
        '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005'
}
Run Code Online (Sandbox Code Playgroud)