27

Android Studio refuses to sign my code for debug build.

I have an older project which did not have any signing instructions in build.gradle, so I added these according to this Android gradle signingConfig error and other posts.

My build.gradle file on module level (the only module) looks like this (excerpt):

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'
    defaultConfig {
        applicationId "cc.appname.android"
        minSdkVersion 11
        targetSdkVersion 21
        versionCode 1
        versionName '1.0'
    }
    signingConfigs {
        debug {
            storeFile file('../../../.android/debug.keystore')
            keyAlias 'androiddebugkey'
            keyPassword 'android'
            storePassword 'android'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    productFlavors {
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

The storeFile can be found, because when I change the path I get a compile error. When the path is correct, it compiles, but when I try to use the Facebook SDK within my app, it reports a wrong keyhash.

I noticed that signingConfigs

signingConfig signingConfigs.debug

is underlined with the error message "Cannot infer argument types..."

So I went to Project Settings in the UI, removed signing and the relationship between the build and signing, saved this, and added it back. Same problem.

I am sure this is something very small that I just overlooked, or Google renamed the command between versions, whatever.

Can anybody help?

Community
  • 1
  • 1
Oliver Hausler
  • 4,900
  • 4
  • 35
  • 70

3 Answers3

43

Several things here, assuming your debug.keystore is the one from the ~/.android folder.

Change this:

    debug {
        storeFile file('../../../.android/debug.keystore')
        keyAlias 'androiddebugkey'
        keyPassword 'android'
        storePassword 'android'
    }

to this(store the debug.keystore in the root project):

    debug {
        storeFile rootProject.file('debug.keystore')
        keyAlias 'androiddebugkey'
        keyPassword 'android'
        storePassword 'android'
    }

You do not need to override the debug BuildType, it naturally signs with the debug key anyways, so you can remove:

    debug {
        signingConfig signingConfigs.debug
    }

The final build.gradle:

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'
    defaultConfig {
        applicationId "cc.appname.android"
        minSdkVersion 11
        targetSdkVersion 21
        versionCode 1
        versionName '1.0'
    }
    signingConfigs {
        debug {
            storeFile rootProject.file('debug.keystore')
            keyAlias 'androiddebugkey'
            keyPassword 'android'
            storePassword 'android'
        }
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    productFlavors {
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • 1
    Thanks Jared, it signs now. The funny thing is when I go to the UI it adds the wrong `signingConfig signingConfigs.debug` itself. I believe there is either a bug or a missing dependency. At least it works now. – Oliver Hausler Feb 14 '15 at 16:19
  • About `rootProject.file('debug.keystore')` - thanks for the tip, but the relative path still starts at the project root. As the debug keystore is higher up the path, I still need to cd back `rootProject.file('../../.android/debug.keystore')`. So either the debug keystore is in the wrong position (not within the project root), or this is on purpose and convention of the build system, but then it shouldn't need to be referenced from within build.gradle at all. – Oliver Hausler Feb 14 '15 at 16:23
  • I had to use storeFile file('../../../.android/debug.keystore') as well. – Clive Jefferies Dec 10 '15 at 13:35
  • @CliveJefferies That is because you put it several directories up. – Jared Burrows Dec 10 '15 at 18:31
  • @JaredBurrows I didn't put the key anywhere ;) – Clive Jefferies Dec 11 '15 at 09:57
  • This answer works for me, except that I also have to replace line: storeFile rootProject.file('debug.keystore') with line: storeFile rootProject.file('../../.android/debug.keystore') or even better with the following line as shown in @donturner's answer: storeFile = new File("${System.properties['user.home']}/.android/debug.keystore") – jonathanzh May 12 '20 at 18:29
2

Both buildTypes.debug and signingConfigs.debug exist by default.
You do not need to define buildTypes.debug unless you want to change it.
You do not need to define signingConfigs.debug unless you want to change it.
If you want to use the default debug signing for a different build type, you can do that. For example, the following works also without defining anything else:

buildTypes {
 unsignedRelease {
  initWith release
  signingConfig signingConfigs.debug
 }
}
Doron Ben-Ari
  • 443
  • 3
  • 12
-1

Adding an answer for the new gradle experimental plugin since the syntax is different:

model {
    android {
        buildTypes {
            release {
                signingConfig = $("android.signingConfigs.myConfig")
                ...
            }
        }
    }
    android.signingConfigs {
        create("myConfig") {
            keyAlias = 'androiddebugkey'
            keyPassword = 'android'
            storeFile = new File("${System.properties['user.home']}/.android/debug.keystore")
            storePassword = 'android'
            storeType = "jks"
        }
    }
 }

NOTE: the android.signingConfigs block must be placed outside of the android block.

donturner
  • 17,867
  • 8
  • 59
  • 81