How to change applicationID dynamically

不想你离开。 提交于 2019-12-12 01:44:16

问题


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.

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

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

回答1:


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



回答2:


I think you can achieve your goal using product flavour. In your app build.gradle, under android, you can add some flavour like this

productFlavors {

    full {
        applicationId "com.mycomp.myapp"
    }

    gitBranch {
        applicationId "com.mycomp.myapp.git_branch"
    }
}

Then remove applicationId from defaultConfig Now when you build, you can choice which build variant (builtype + flavour) to use by the panel on bottom left of android studio. In this case they should be debugFull, debugGitBranch, releaseFull and releaseGitBranch. You final apk will have applicationId accordingly with the one you have inserted in the used flavour.

Hope this helps



来源:https://stackoverflow.com/questions/39393477/how-to-change-applicationid-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!