Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate signed and unsigned APK's using gradle?

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

like image 795
NullPointerException Avatar asked Aug 25 '15 08:08

NullPointerException


People also ask

How to build unsigned apk in Gradle?

Unsigned APK is actually signed by debugging key. It is Secure. Only the Developer has unsigned Apk. Step 1: Build your project then go to the Build > Build Bundle (s)/APK (s) > Build APK (s) as shown in the below image. Step 2: Then, You will see that Gradle is building. Wait for 3 to 4 minutes to complete the build.

How to generate unsigned apk?

That is why most of the time we prefer to generate unsigned apk as that is even easy to be generated. Unsigned APK is actually signed by debugging key. It is Secure. Only the Developer has unsigned Apk. Step 1: Build your project then go to the Build > Build Bundle (s)/APK (s) > Build APK (s) as shown in the below image.

How to generate signed bundle or APK for Android?

Step 1: Go to Build -> Generate Signed Bundle or APK, a pop up will arise. Choose APK in the pop-up and click on Next. Step 2: After completing step 1, if you already have a key, make sure you just refer to that and you can release the version of that app but in case if it is the first time it is recommended to create a new key.

What is a signed APK?

The signed apk is simply the unsigned apk that has been signed via the JDK jarsigner tool. If you want to generate a signed apk then refer to How to Generate Signed Apk in Android Studio? Why do We Need to Generate an Unsigned Apk?


1 Answers

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
like image 134
Lazy Beard Avatar answered Sep 18 '22 11:09

Lazy Beard