Android Studio: how to create a second debug build type

11,316

Solution 1

Also, you can make your Build type similar to debug using:

initWith(buildTypes.debug)

Here is an example:

...
buildTypes {

    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField 'String', 'URL_API_BASE_SERVICE', '"http://theidasworld.com"'
    }
    debug {
        versionName 'APP BETA'
        buildConfigField "Integer", "PIN", "0000"  
        buildConfigField 'String', 'URL_API_BASE_SERVICE', '"http://debug.theidasworld.com"'
    }
    inspection {
        initWith(buildTypes.debug) // keep versionName and PIN from 'debug'
        buildConfigField 'String', 'URL_API_BASE_SERVICE', '"http://inspection.theidasworld.com"'
    }
}
...

Solution 2

You can specify in the build.gradle file which signingConfig should be used with the buildType.

To sign using the same signingConfig as the default debug buildType, use the following:

buildTypes {
    local {
        signingConfig signingConfigs.debug
    }

    /* NOTE: the debug block is not required because it is a default
     * buildType configuration; all of its settings are defined implicitly
     * by Android Studio behind the scenes.
     */
}

If you would prefer to use a custom keystore located on your local system, use the following instead:

signingConfigs {
    local {
        storeFile file("/path/to/custom.keystore")
        storePassword "password"
        keyAlias "MyLocalKey"
        keyPassword "password"
    }
}

buildTypes {
    local {
        signingConfig signingConfigs.local
    }
}

Solution 3

For Kotlin build scripts, the equivalent of the above answer would be something like this:

android {
  buildTypes {
    create("local") {
      initWith(buildTypes["debug"])
      buildConfigField("String", "URL_API_BASE_SERVICE", """"http://..."""")
      buildConfigField("Boolean", "IS_CI", "${System.getenv("CI") == "true"}") // boolean example
      isDebuggable = true
    }
    testBuildType = "local"
  }
}

Solution 4

Other way could be using flavors, like so:

productFlavors {

  app1 {}

  app2 {}

}
Share:
11,316

Related videos on Youtube

Mister Smith
Author by

Mister Smith

Android freak, but still a noob.

Updated on July 22, 2022

Comments

  • Mister Smith
    Mister Smith almost 2 years

    I want to create a second build type that should work exactly as the already existing debug type. Currently I have two build types: debug and release. The debug one can be run with a single click, and is automatically signed with the debug keystore. I manually compile the release build through the Build -> Generate signed APK wizard.

    So to clone the debug build type, I first added a second build type named "local" to the app build.graddle file:

    buildTypes {
        ...
        debug {
            debuggable true
            minifyEnabled false
        }
        local {
            debuggable true
            minifyEnabled false
        }
    }
    

    Then I created app/src/local/res and added some files.

    Then I do a gradle resync and select the new build type in the left tab: build type tab

    Finally I click the run button and I expected it to just work. This IntelliJ help article says the debug signing config is the default:

    This means that if you do not configure an artifact manually and select the Deploy default APK option in the Run/Debug Configuration: Android Application dialog box, IntelliJ IDEA will use the predefined values in the certificate for the generated

    Instead, this dialog is shown:

    run dialog

    When I click the fix button, it opens the signing config dialog for the whole app module. However, I don't want to sign this apk for release, I need it signed with the debug cert. Also I noticed that a new assembleLocal gradle task has been created, but it generates an unaligned apk. In this folder I can see the regular debug apks are generated correctly in their unaligned and final versions.

    How on Earth can I just clone the debug build type?

    • TacB0sS
      TacB0sS over 6 years
      You should accept the correct answer, for me it was ivan.panasiuk answer
  • Mister Smith
    Mister Smith over 8 years
    Sorry, we don't include the signing config in the build.gradle as each team member has a different OS and keystore path, and a VCS sync would break other devs workspaces. But most importantly, I don't want to sign for release. It should sign with the same debug cert the debug config is using.
  • Gabriele Mariotti
    Gabriele Mariotti over 8 years
    @MisterSmith Each build type is signed with a keystore. And of course you can use the debug keystore, check the first part of the answer. The important part of the answer is that you have to define the signing config in the build.gradle because the local build type is a custom build type and there isn't a default.
  • Mister Smith
    Mister Smith over 8 years
    I understand that. But what should I write in the "local" signingConfig so that it uses the default debug keystore?
  • Gabriele Mariotti
    Gabriele Mariotti over 8 years
    Should be enough signingConfig signingConfigs.debug. If it doesn't work add signingConfigs { debug { storeFile file("debug.keystore") }}
  • Mister Smith
    Mister Smith over 8 years
    Yes, that s probably the easiest way.
  • narancs
    narancs almost 7 years
    that's a good solution, but my problem with falviours that I will have buildType&flavors combination. What if i just want to switch between different debug environments?
  • aricherca
    aricherca almost 7 years
    You have the build variants on a tab. There you can choose the build variant.
  • TacB0sS
    TacB0sS over 6 years
    Thanks a ton, I tried setting up signingConfig signingConfigs.debug, I've expected it to work, but it didn't, your solution did it!! thanks
  • Samuel
    Samuel over 6 years
    It took me ours to find that signingConfig option which is implicitly set for the debug build. Geez.
  • V.S.
    V.S. almost 5 years
    If your app has sub-modules, the compilation will fail with this configuration unless you declare the local build type for your sub-modules as well. This can be more easily overcome by adding matchingFallbacks = ['debug'] to the local in build type in main :app's build.gradle. Gradle will then use the debug configuration for your sub-modules, which is probably what you want.
  • Aman Alam
    Aman Alam about 4 years
    Looks like signingConfig.debug doesn't work anymore, and it's changed to debug.signingConfig now