【问题标题】:Building APK with Gradle outside IDE (migrating from Ant)在 IDE 外使用 Gradle 构建 APK(从 Ant 迁移)
【发布时间】:2013-07-04 04:33:44
【问题描述】:

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

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

【问题讨论】:

    标签: android eclipse ant gradle


    【解决方案1】:

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

    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'
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-08
      • 2013-06-23
      • 2020-01-19
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      • 1970-01-01
      • 2011-03-06
      • 1970-01-01
      相关资源
      最近更新 更多