简体   繁体   中英

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

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

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

The answer you linked to is correct - if your chosen variant (ie 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM