How to create a release signed apk file using Gradle?

前端 未结 30 1830
既然无缘
既然无缘 2020-11-22 13:48

I would like to have my Gradle build to create a release signed apk file using Gradle.

I\'m not sure if the code is correct or if I\'m missing a parameter when doing

30条回答
  •  孤独总比滥情好
    2020-11-22 14:17

    An alternative is to define a task that runs only on release builds.

    android {
      ...
      signingConfigs {
         release {
            // We can leave these in environment variables
            storeFile file('nameOfKeystore.keystore')
            keyAlias 'nameOfKeyAlias'
    
            // These two lines make gradle believe that the signingConfigs
            // section is complete. Without them, tasks like installRelease
            // will not be available!
            storePassword "notYourRealPassword"
            keyPassword "notYourRealPassword"
    
         }
      }
      buildTypes {
         ...
         release {
            signingConfig signingConfigs.release
            ...
         }
      }
      ...
    }
    
    task setupKeystore << {
    final Console console = System.console();
    if (console != null) {
        //def keyFile = console.readLine(“\nProject: “ + project.name + “Enter keystore path: "))
        //def keyAlias = console.readLine(“Project: “ + project.name + “Enter key alias: ")
            def storePw = new String(console.readPassword(“Project: “ + project.name + “. Enter keystore password: "))
            def keyPw  = new String(console.readPassword(“Project: “ + project.name + “.Enter keystore password: "))
    
        //android.signingConfigs.release.storeFile = file(keyFile);
        //android.signingConfigs.release.keyAlias = keyAlias
            android.signingConfigs.release.storePassword = storePw
            android.signingConfigs.release.keyPassword = keyPw
    }
    }
    
    //Validate t
    def isReleaseConfig = gradle.startParameter.taskNames.any {it.contains('Release') }
    if (isReleaseConfig) {
        setupKeystore.execute();
    }
    

提交回复
热议问题