如何动态更改applicationID [英] How to change applicationID dynamically

查看:776
本文介绍了如何动态更改applicationID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过将原始ID附加GIT分支的分支名称来更改我的applicationId.脚本 renameID.sh 完成了重命名applicationID的工作.

I am trying to change my applicationId by appending it the original id with the branch name of the GIT branch. The Script renameID.sh does the work of renaming applicationID.

尽管我可以在运行构建时成功重命名ApplicationID,但是我希望在构建后恢复该applicationID.

Although I can successfully rename the ApplicationID while running build, but I want that applicationID is restored after build.

脚本restoreID用于将applicationID恢复为原始名称,但似乎不起作用.

The script restoreID is to restore the applicationID to original name, but doesn't seems to work.

有人可以指出我做错了什么,还是建议一些更好的方法来执行我的客观任务?

Can someone point what am I doing wrong or suggest some better way to perform my objective task?

apply plugin: 'com.android.application'

class Config{
    String name
}

task renameAppId(type:org.gradle.api.tasks.Exec) {
    commandLine 'sh', 'renameID.sh'

}

preBuild.dependsOn renameAppId
task finaltask(type:org.gradle.api.tasks.Exec){
    commandLine 'sh', 'restoreID.sh'

}

build.finalizedBy(finaltask)


android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "com.abc.xyz"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:recyclerview-v7:+'
    compile 'com.android.support:cardview-v7:+'
    compile 'com.android.support:support-v4:24.0.0'
    compile 'de.hdodenhof:circleimageview:1.3.0'
    compile 'com.android.volley:volley:1.0.0'
    compile 'com.android.support:design:22.2.1'
}

推荐答案

有一个applicationIdSuffix函数,您可以将其应用于buildType.像这样

There is an applicationIdSuffix function which you can apply to your buildType. Something like this

buildTypes {
    debug {
        applicationIdSuffix branch_name
    }
}

此外,您可以通过这种方式获取当前分支名称

In addition, you can get current branch name by this way

    def branch_name = ""
    try {
        branch_name = "git rev-parse --abbrev-ref HEAD".execute().text.trim();
    } catch (ignored) {
        println "Git not found"
    }

这篇关于如何动态更改applicationID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆