用gradle生成debug和release包的问题。

ambitioner2012 2016-01-21 03:04:42
问题1:直接在android studio里面run app和debug app生成的包应该都是debug模式的吧?
问题2:我想run app和debug app的时候指定生成的apk包名,但是生成名字却是release模式指定的名字。我生成release包用的命令是gradlew assemblerelease.
下面是我的build.gradle文件。
apply plugin: 'com.android.application'

def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId 'com.xuetubao.android'
//为了调试暂时调高
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}

packagingOptions {
exclude 'project.properties'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'

}

lintOptions {
abortOnError false
}
signingConfigs {
debug {
// No debug config
}

release {
storeFile file *******
storePassword ********
keyAlias *******
keyPassword ********* }
}

buildTypes {
debug {
// 显示Log
buildConfigField "boolean", "LOG_DEBUG", "true"
versionNameSuffix "-debug"
minifyEnabled false
zipAlignEnabled false
shrinkResources false
}

release {
// 不显示Log
buildConfigField "boolean", "LOG_DEBUG", "false"
minifyEnabled false
zipAlignEnabled true
// 移除无用的resource文件
shrinkResources true
// proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release

applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 输出apk名称为boohee_v1.0_2015-01-15_wandoujia.apk
def fileName = "xtb_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
repositories {
flatDir {
dirs 'libs'
}
}



productFlavors {
baidu {}
qq {}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [CHANNEL_VALUE: name]
}

}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('src/main/libs/volly-1.0.11.jar')
compile files('libs/Volley.jar')
compile files('libs/gson-2.2.4.jar')
compile project(':barCode')
compile files('libs/bugly_1.2.3.8__release.jar')
compile project(':validate')
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.android.support:support-v4:23.0.1'
compile 'com.rengwuxian.materialedittext:library:2.1.4'
compile 'com.daimajia.numberprogressbar:library:1.2@aar'
compile 'de.greenrobot:eventbus:2.4.0'
}
...全文
14846 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ty110119120 2017-03-06
  • 打赏
  • 举报
回复
编译和安装的时候releaseTime的时间不一直,导致APK名字不同,push不到手机,有没有遇到这个问题呢
ambitioner2012 2016-03-23
  • 打赏
  • 举报
回复
谢谢楼上两位兄弟,说的都对,用你们的方法解决了。
画虎烂 2016-03-17
  • 打赏
  • 举报
回复
run的时候,你要看下AS左下角的bulid variants选的是哪个,debug还是release
StoneHui_ 2016-03-17
  • 打赏
  • 举报
回复
引用 1 楼 u014165119 的回复:
楼主解决了吗?我也遇到这样的问题了。
刚解决这个问题了,加个判断就行了,build.gradle代码片段如下:

android {

......

    //打包后应用名称
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            def fileName
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                if (variant.buildType.name.equals('release')) {
                    fileName = "XXX_v${defaultConfig.versionName}.apk"
                } else if (variant.buildType.name.equals('debug')) {
                    fileName = "XXX_v${defaultConfig.versionName}_debug.apk"
                }
                output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    }
}
StoneHui_ 2016-03-17
  • 打赏
  • 举报
回复
楼主解决了吗?我也遇到这样的问题了。

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧