13

I need to generate both unsigned and signed release apks using gradle command line (gradlew assembleRelease is the command im using now)

The apks must be aligned. I checked this: Build Unsigned APK with Gradle but it seems to be an old way to achieve this, now it does not work and something has changed in lastest versions of android and gradle compilation. And also i need to generate both apks at same time, not only release mode or unsigned mode

3
  • Apologies for my initial answer, I misread the question Commented Aug 25, 2015 at 9:00
  • run gradle clean assemble (this will execute assembleDebug and assembleRelease. Make sure the debug buildType has no singingConfig, but the release buildType has got one. Commented Aug 25, 2015 at 9:29
  • So, did you solve your problem? Commented Aug 27, 2015 at 11:06

4 Answers 4

9

I know it's pretty old answer but it still might help someone gain your goal without adding extra flavour (even as in my case it might be challenging because many dependencies in the project).

android {
  signingConfigs {
    release { ... }
  }

  productFlavors {
    signed { 
      signingConfig (checkUnsigned() ? null : signingConfigs.release)

    }
}

def checkUnsigned ()  {
    return project.hasProperty("unsigned")
}

In order to use it just use

gradle assembleRelease

or

gradle assembleRelease '-Punsigned'

for creating unsigned (quotes for CI, otherwise it might not be needed)

Disadvantage of the solution is just when you want to assemble several flavours in one line ie

gradle assembleRelease assembleDebug assembleRelease '-Punsigned'

assembleRelease checks all properties in command line, so first assembleRelease will be callse also with param '-Punsigned' I resolved this CI issue by using 2 commands - one for signed, other for unsigned versions

gradle assembleRelease assembleOtherFlavour '-Punsigned'
gradle assembleDebug assembleRelease assembleOtherFlavour
2
  • Never is late to help other like me :D Thanks!
    – equiman
    Commented Apr 10, 2017 at 21:20
  • This is lighter weight than adding a new build variant. Thanks!
    – Erik B
    Commented Oct 19, 2017 at 0:18
7

I think buildTypes is a more suitable place than productFlavors.
You can extend your release build with replacing signingConfig.

buildTypes {
    release {
        ....
        signingConfig signingConfigs.release
    }

    releaseUnsigned.initWith(buildTypes.release)
    releaseUnsigned {
        signingConfig null
    }
}

Then for building both APK-files:

./gradlew assemble

Or if you want only release builds

./gradlew assembleRelease assembleReleaseUnsigned
or ./gradlew assR assRU

If you really want to use only assembleRelease task, you can make this dependency

assembleRelease.dependsOn assembleReleaseUnsigned

And build with just

./gradlew assembleRelease
2
  • where do you add the line assembleRelease.dependsOn assembleReleaseUnsigned ? Commented Jun 27, 2019 at 10:23
  • @KhaledOsman anywhere in app/build.gradle, after dependencies {} or other custom tasks. Commented Jun 27, 2019 at 14:51
4

The answer you linked to is correct - if your chosen variant (i.e. build type + flavor combination) is not using a "signing ready" signing config, gradle will create an unsigned APK.

So you can define a setup like this:

android {
  signingConfigs {
    release { ... }
  }

  productFlavors {
    signed { 
      signingConfig signingConfigs.release // defined above
    }

    unsigned {} // use the default options
  }
}

Then running ./gradlew :app:assembleRelease will create your APKs:

app/build/outputs/apk
├── app-signed-release.apk
├── app-signed-release-unaligned.apk
└── app-unsigned-release-unsigned.apk
1
  • 3
    I think signingConfig should take place in buildTypes, and flavors are for app logic Commented Aug 25, 2015 at 11:05
3

It didn't work for me with defining new buildType. But it works with debug/release:

 buildTypes {
         release {
                signingConfig null
            }
         debug {
                signingConfig null
            }
    }

this will create unsigned apks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.