14

This may sound extremely trivial but as an android newbie I just found myself buried under a ton of hashes tokens keystores and keytools which can be a bit overwhelming.

I am trying to make android studio run my application on the emulator signed with my custom keystore and not the debug.keystore

Is this an option, or do I just have to generate a signed .apk every time I make changes, then install it via adb and then run it from the emulated device's menu?

Also is that a good practice when testing applications or should I avoid it?

ppp
  • 2,035
  • 9
  • 32
  • 52
  • Why do you want to build with a custom keystore? – Scott Barta Jul 22 '14 at 18:44
  • @ScottBarta my app makes use of facebook login so I need to register a valid key hash with them, so I want to use the one I intend to release when it's ready – ppp Jul 22 '14 at 18:47
  • I'm not that familiar with writing Facebook API apps, so I can't comment on whether or not this is the right thing to do. But I otherwise answered your question below. – Scott Barta Jul 22 '14 at 19:39
  • 1
    @ScottBarta I am actually having the same question but because I want to run a MapFragment (google maps) since it requires a registered Key (obtained by SHA1 from keystore), hence i need to sign the apk file i deploy to my device (for testing and development). Afaik there is no other way, so I dare say it is a valid question. – AgentKnopf Aug 24 '14 at 17:17

3 Answers3

26

After running into problems when using the Android Studio UI to create a signing config I successfully managed setting it up via the gradle build file.

Open your projects build.gradle file. It should contain something like this:

android{
    //signingConfigs goes here
    defaultConfig{
        //SDK version, version code etc
    } 

     //Some more stuff
}

If it isn't already in there, add the following snippet below android {

signingConfigs {
    debug {
        storeFile file(project.property("MyApp.signing"))
        storePassword project.property("MyApp.signing.password")
        keyAlias project.property("MyApp.signing.alias")
        keyPassword project.property("MyApp.signing.password")
    }
}

Now in the same directory where your build.gradle file lies you should have a gradle.properties file (if not, create it). We'll now add the properties we used above to the properties file in order to map the values:

MyApp.signing=RelativeOrAbsolutePathToKeystore
MyApp.signing.password=yourPassword
MyApp.signing.alias=aliasNameOfYourKeystore

An example where the keystore.jsk file (generated via Android Studio) lies one directory above the app directory (in which the properties file is):

MyApp.signing=../myapp.keystore.jsk
MyApp.signing.password=helloworkd
MyApp.signing.alias=myapp_alias

The above configuration would then use the key to sign a debug build (because our signingConfigs was made for the debug build).

So make sure that in Android Studio, to set your build variant to "debug". If you want to do all this for the release build switch your build variants to release and your signingConfigs to release {...} instead of debug{...} or simply add both if you want to switch between them.

AgentKnopf
  • 4,295
  • 7
  • 45
  • 81
  • Grear answer! Thank you! – researcher Dec 23 '15 at 09:55
  • 1
    I've +1ed this for completeness but wholey measure of goat why is Android Studio so sucky.? With eclipse this was a one click solution. – s1ni5t3r Apr 11 '16 at 20:16
  • This works, but I don't like having the password displayed in clear. Is there a way to avoid it? (for example, using an encrypted password) – Thomas Jan 07 '21 at 10:45
18

You can just add a signing config to the debug build type and it will use it. You can do it through the Project Structure dialog -- in your module, select the "Signing" tab and configure your signing info, then in the "Build Types" tab, for the "Signing config" popup, choose it.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Changed those settings (Android Studio 0.8.2) and once I saved them my gradle build failed miserably (debug - sign was suddenly empty in the gradle config), unable to actually to the signing. Will try to fix it but it seems a better idea to directly edit the gradle file. – AgentKnopf Aug 24 '14 at 17:24
  • 1
    Works for me Perfectly – Ashok Varma Feb 13 '15 at 04:52
  • 1
    this should be the desired answer for OP, click 'run' now generates a signed application for both emulators and adb devices – Ge Rong Nov 09 '15 at 08:30
0

Now it's very simple in Android Studio 3

Configure the signingConfigs and release buildTypes in your build.gradle file and go to the build menu.

Build -> Select Build Variants

There has two options module and build variant.

Module will be app and Build Variant will be release