首页 文章

如何设置gradle和android studio进行发布构建?

提问于
浏览
60

我想构建Android应用程序并开始签名 . 为此,我需要有apk的发布版本 . Google文档仅建议使用Eclipse和ant方法来发布版本:http://developer.android.com/tools/publishing/app-signing.html#releasecompile

但是我找不到如何强制gradle build release版本的apk . build.gradle 也没有提供任何提示 . gradlew tasks 建议,没有安装Release配置,但存在卸载版本:

Install tasks
-------------
installDebug - Installs the Debug build
installTest - Installs the Test build for the Debug build
uninstallAll - Uninstall all applications.
uninstallDebug - Uninstalls the Debug build
uninstallRelease - Uninstalls the Release build
uninstallTest - Uninstalls the Test build for the Debug build

我的 build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.android.support:support-v4:13.0.+'
    compile files('libs/android-support-v4.jar')
    compile project(":libraries:ActionBarSherlock")
    compile project(":libraries:CollabsibleSearchMenu")
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 16
    }
}

我错过了什么?

4 回答

  • 23

    在最新版本的android studio中,您可以这样做:

    ./gradlew assembleRelease
    

    aR 简称 . 这将产生一个未签名的发布apk . 构建已签名的apk可以类似地完成,也可以在Android Studio中使用Build - > Generate Signed Apk .

    See the docs here

    这是我的build.gradle供参考:

    buildscript {
      repositories {
        mavenCentral()
      }
      dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
      }
    }
    apply plugin: 'android'
    
    dependencies {
      compile fileTree(dir: 'libs', include: '*.jar')
    }
    
    android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"
    
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    
        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')
    
        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    
    }
    
    buildTypes {
        release {
    
        }
    }
    
  • 80
    • 打开 Build Variants

    • 设置 debugrelease

    • shift+f10 跑!!

    然后,Android Studio将执行 assembleRelease 任务并将xx-release.apk安装到您的设备 .

  • 37

    无需更新gradle在Android studio中制作发布应用程序 . 如果您是eclipse用户,那么它将非常容易 . 如果您是新手,请按照步骤操作

    1:转到工具栏部分的"Build" . 2:选择"Generate Signed APK..."选项 .
    enter image description here

    3:填写打开的表格并转到下一步4:如果您已经有.keystore或.jks,那么选择该文件输入您的密码和别名以及相应的密码 . 5:或者没有.keystore或.jks文件,然后单击创建新...按钮,如图1所示,然后填写表格 .
    enter image description here

    上面的过程是手动构建 . 如果您希望android studio自动签署您的应用程序

    在Android Studio中,您可以将项目配置为在构建过程中自动签署发布APK:

    在项目浏览器上,右键单击您的应用程序,然后选择“打开模块设置” . 在“项目结构”窗口中,选择“模块”下的应用程序模块 . 单击“签名”选项卡 . 选择密钥库文件,输入此签名配置的名称(因为您可以创建多个),然后输入所需的信息 .
    enter image description here
    图4.在Android Studio中创建签名配置 .

    单击“构建类型”选项卡 . 选择发布版本 . 在“签名配置”下,选择刚刚创建的签名配置 .
    enter image description here
    图5.在Android Studio中选择签名配置 .

    4:最重要的是在gradle中使debuggable = false .

    buildTypes {
            release {
               minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-  android.txt'), 'proguard-rules.txt'
            debuggable false
            jniDebuggable false
            renderscriptDebuggable false
            zipAlignEnabled true
           }
         }
    

    访问更多信息developer.android.com

  • 18

    要激活 installRelease 任务,您只需要一个 signingConfig . 就这些 .

    http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Android-tasks

    最后,只要可以安装(需要签名),插件就会为所有构建类型(调试,发布,测试)创建安装/卸载任务 .

    这是你想要的:

    Install tasks
    -------------
    installDebug - Installs the Debug build
    installDebugTest - Installs the Test build for the Debug build
    installRelease - Installs the Release build
    uninstallAll - Uninstall all applications.
    uninstallDebug - Uninstalls the Debug build
    uninstallDebugTest - Uninstalls the Test build for the Debug build
    uninstallRelease - Uninstalls the Release build   <--- release
    

    以下是获取installRelease任务的方法:

    示例 build.gradle

    buildscript {
        repositories {
            jcenter()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:1.2.3'
        }
    }
    
    apply plugin: 'com.android.application'
    
    android {
      compileSdkVersion 22
      buildToolsVersion '22.0.1'
    
      defaultConfig {
        applicationId 'demo'
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName '1.0'
      }
    
      signingConfigs {
          release {
              storeFile <file>
              storePassword <password>
              keyAlias <alias>
              keyPassword <password>
          }
      }
    
      buildTypes {
        release {
            signingConfig signingConfigs.release
        }
      }
    }
    

相关问题