我想有我的Gradle构建创建一个发布签名apk文件使用Gradle。

我不确定代码是否正确,或者我在做gradle构建时是否缺少一个参数?

这是build.gradle/build.gradle中的一些代码。节文件:

android {
    ...
    signingConfigs {
        release {
            storeFile(file("release.keystore"))
            storePassword("******")
            keyAlias("******")
            keyPassword("******")
        }
    }
}

Gradle构建成功完成,在我的build/apk文件夹中,我只看到…-release-unsigned.apk和…-debug-unaligned.apk文件。

对如何解决这个问题有什么建议吗?


当前回答

对于Groovy (build.gradle)

您不应该将签名凭据直接放入构建中。gradle文件。相反,凭证应该来自不受版本控制的文件。

放置一个文件签名。特定于模块构建的属性。Gradle被找到了。别忘了把它添加到你的。gitignore文件!

signing.properties

storeFilePath=/home/willi/example.keystore
storePassword=secret
keyPassword=secret
keyAlias=myReleaseSigningKey

build.gradle

android {
    // ...
    signingConfigs{
        release {
            def props = new Properties()

            def fileInputStream = new FileInputStream(file('../signing.properties'))
            props.load(fileInputStream)
            fileInputStream.close()

            storeFile = file(props['storeFilePath'])
            storePassword = props['storePassword']
            keyAlias = props['keyAlias']
            keyPassword = props['keyPassword']
        }
    }

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

其他回答

这是解决同一问题的另一种方法。由于不建议在源代码中存储任何类型的凭据,我们决定在一个单独的属性文件中设置密钥存储和密钥别名的密码,如下所示:

key.store.password=[STORE PASSWORD]
key.alias.password=[KEY PASSWORD]

如果使用git,可以创建一个名为secure.properties的文本文件。您应该确保从存储库中排除它(如果使用git,则将其添加到.gitignore文件中)。然后,您需要创建一个签名配置,就像其他一些答案所指出的那样。唯一的区别是如何加载凭据:

android {
    ...
    signingConfigs {
        ...
        release {
            storeFile file('[PATH TO]/your_keystore_file.jks')
            keyAlias "your_key_alias"

            File propsFile = file("[PATH TO]/secure.properties");
            if (propsFile.exists()) {
                Properties props = new Properties();
                props.load(new FileInputStream(propsFile))
                storePassword props.getProperty('key.store.password')
                keyPassword props.getProperty('key.alias.password')
            }
        }
        ...
    }

    buildTypes {
        ...
        release {
            signingConfig signingConfigs.release
            runProguard true
            proguardFile file('proguard-rules.txt')
        }
        ...
    }
}

永远不要忘记手动将signingConfig分配给发行版构建类型(出于某些原因,我有时假设它将被自动使用)。此外,启用proguard并不是强制性的,但建议这样做。

与使用环境变量或请求用户输入相比,我们更喜欢这种方法,因为它可以从IDE中完成,通过切换到release构建类型并运行应用程序,而不必使用命令行。

比之前的答案更简单的方法:

把它放到~/.gradle/gradle.properties中

RELEASE_STORE_FILE={path to your keystore}
RELEASE_STORE_PASSWORD=*****
RELEASE_KEY_ALIAS=*****
RELEASE_KEY_PASSWORD=*****

修改你的应用/构建。Gradle,并将其添加到android{代码块:

...    
signingConfigs {

   release {
       storeFile file(RELEASE_STORE_FILE)
       storePassword RELEASE_STORE_PASSWORD
       keyAlias RELEASE_KEY_ALIAS
       keyPassword RELEASE_KEY_PASSWORD

       // Optional, specify signing versions used
       v1SigningEnabled true
       v2SigningEnabled true
   }
}

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

然后你可以运行gradle assemblerrelease


另请参阅signingConfigs Gradle DSL的参考

@Destil的答案很好,如果你可以在所有项目中重用相同的配置。或者,Android Studio自带本地文件。可以使用的属性文件,但它应该是ide生成的,我找不到一种方法来扩展它从Android Studio。

这是@jonbo回答的一个变体。这个答案允许项目特定的设置,但它带来了一些开发人员的开销。具体来说,需要大量的样板文件来将signingConfigs定义移动到一个单独的文件中——特别是当您需要为多个项目这样做时,这是选择这个解决方案而不是Destil的主要原因。这可以通过包含线条在一定程度上得到缓解

apply plugin: 'com.android.application'

在凭证文件中,因为这将允许IDE完成。

最后,这里的大多数解决方案不允许在不提供语法上(如果不是语义上)有效的signingConfigs定义的情况下以调试模式构建项目(自动处理调试签名)。如果您不需要从给定的机器生成一个发布版本,那么这个额外的步骤可以被视为一个不必要的障碍。另一方面,它可以帮助在生产中运行调试构建的无知或懒惰的同事。

这个解决方案将允许调试构建而完全不用担心凭证,但是它将需要有效的凭证来生成发布构建,并且它只需要很少的样板文件。然而,作为一个缺点,它可能会鼓励其他人用真实的凭证替换虚拟值,并且没有办法防止这种情况。

// app/build.gradle
// Define this structure in signing.gradle to enable release builds.
ext.signing = [
        storeFilePath : 'path/to/keystore',
        storePassword : 'keystore password',
        keyAlias      : 'key alias',
        keyPassword   : 'key password',
]

if (file('signing.gradle').exists()) {
    apply from: 'signing.gradle'
}

android {
    ...
    signingConfigs {
        release {
            storeFile file(project.signing.storeFilePath)
            storePassword project.signing.storePassword
            keyAlias project.signing.keyAlias
            keyPassword project.signing.keyPassword
        }
    }
    buildTypes {
        debug { ... }
        release {
            signingConfig signingConfigs.release
            ...
        }
    }
}

这将创建一个虚拟属性,纯粹用于生成语法上有效的构建文件。在调试构建阶段,分配给ext.signing属性的值是不相关的。要启用发布版本,请将ext.signing复制到signing中。Gradle并将虚拟值替换为有效凭证。

// signing.gradle
ext.signing = [
        storeFilePath : 'real/keystore',
        storePassword : 'real keystore password',
        keyAlias : 'real key alias',
        keyPassword : 'real key password',
]

当然,签名。gradle应该被VCS忽略。

如果您想避免在构建中硬编码您的密钥库和密码。gradle,你可以使用属性文件解释这里:处理签名配置与gradle

基本上:

1)创建一个myproject。属性文件在/home/[username]/。签名内容如下:

keystore=[path to]\release.keystore
keystore.password=*********
keyAlias=***********
keyPassword=********

2)创建gradle。属性文件(可能在你的项目目录的根目录)的内容:

MyProject.properties=/home/[username]/.signing/myproject.properties

3)在你的构建中参考它。这样Gradle:

    if(project.hasProperty("MyProject.properties")
        && new File(project.property("MyProject.properties")).exists()) {

    Properties props = new Properties()
    props.load(new FileInputStream(file(project.property("MyProject.properties"))))

    signingConfigs {
        release {
            storeFile file(props['keystore'])
            storePassword props['keystore.password']
            keyAlias props['keyAlias']
            keyPassword props['keyPassword']
        }
    }
}

我在计算这个过程中获得了很多乐趣。下面是我的演练。

如何在IntelliJ中创建gradle构建文件(v.13.1.4) 本演练假设您知道如何生成密钥存储库文件。 为了本教程的工作,你需要你的keystore文件位于你的应用文件夹,你需要有你的zipalign.exe文件位于'SDK-ROOT\tools'。这个文件通常在“SDK-ROOT\build-tools”文件夹中,在这个文件夹下,它将在最高的api文件夹中(alpha或beta我推荐alpha版本)。

对于那些希望直接跳到这里的人来说,这里是gradle构建文件。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}
android {
    compileSdkVersion 19
    buildToolsVersion '20.0.0'
    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    signingConfigs {
        playstore {
            keyAlias 'developers4u'
            keyPassword 'thisIsNotMyRealPassword'
            storeFile file('developers4u.keystore')
            storePassword 'realyItIsNot'
        }
    }
    buildTypes {
        assembleRelease {
            debuggable false
            jniDebugBuild false
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            zipAlign true
            signingConfig signingConfigs.playstore
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:support-v4:20.0.0'
    implementation 'com.android.support:appcompat-v7:20.0.0'
}

You can build part of this build file (above) from menu option: File/Project Structure From here select Facets and click 'Android-Gradle(App). From here you will see tabs: 'Properties', 'Signing', 'Flavors', 'Build Types' and 'Dependencies' for this walk-through we will just be using 'Signing' and 'Build Types'. Under 'Build Types' (in the name section) enter any name that you wish to identify your build type configuration and in the other 4 fields enter your keystore information (setting the keystore path the the one under your app folder).

在“构建类型”下,在名称字段中输入值“assemblerrelease”,“Debuggable”应该设置为false,“Jni Debug Build”应该设置为false,“Run Proguard”设置为true,“Zip Align”设置为true。这将生成构建文件,但不是像上面描述的那样,您必须随后向构建文件添加一些东西。这里的ProGuard文件位置将在gradle构建文件中手动设置。(如上所述)

你需要添加的DSL容器如下所示:

android {
    ....
    compileSdkVersion 19
    buildToolsVersion '20.0.0'
    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    ....
}

您还必须添加:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:support-v4:20.0.0'
    implementation 'com.android.support:appcompat-v7:20.0.0'
}

注意上面的DSL容器('dependencies')应该在配置文件的底部,而不是在android DSL容器内。 为了从IntelliJ菜单中构建依赖项容器,选择:File/Project Structure。再次选择Facets,然后选择Android-Gradle(app)。您将看到与上述相同的5个选项卡。选择“Dependencies”选项卡并添加所需的依赖项。

所有这些都完成后,您应该会看到一个Gradle构建文件,类似于本演练开头的文件。 要构建你签名的zip对齐版本,你需要打开Gradle任务。你可以通过选择视图/工具窗口/Gradle来进入这个窗口。 在这里你可以双击' assembleassemblerrelease '。这将生成可部署的APK。

编译你的版本时可能出现的潜在问题有(但不限于):你的Gradle构建文件在错误的位置。有两个Gradle构建文件;一个在应用程序根文件夹中,另一个在应用程序根下的应用程序文件夹中。你必须使用后者。

你也可能有棉绒问题。(注意:Android Developer Studio在发现Lint问题方面比IntelliJ要好得多,当你试图从菜单选项中生成签名APK时,你会注意到这一点)

为了解决lint问题,你需要把下面的DSL容器放入android容器中(在顶部):

android {
        ....
    lintOptions {
        abortOnError false
    }
    ....
}

putting this inside your android DSL container will cause an error file to be generated in the build folder (directly under your app folder) the file name should be something like 'lint-results-release-fatal.html' this file will tell you the the class where the error occurred. Another file that will be generated is an XML file that contains the 'issue ID' associated with the lint error. The file name should be something like 'lint-results-release-fatal.xml'. Somewhere near the top of the file you will see a node 'issue' inside which you will see something similar to 'id="IDOfYourLintProblem"'

要纠正此问题,打开项目中'lint-results- assemblerrelease -fatal.html'文件中列出的文件,并在Java类文件中类名上方输入以下代码行:@SuppressLint("IDOfYourLintProblem")。你可能需要导入'android.annotation.SuppressLint;'

所以你的java类文件应该是这样的:

package com.WarwickWestonWright.developers4u.app.CandidateArea;

import android.annotation.SuppressLint;
... other imports

@SuppressLint("IDOfYourLintProblem")
public class SearchForJobsFragment extends Fragment {... rest of your class definition}

请注意,抑制lint错误并不总是最好的想法,你可能会更好地改变导致lint错误的代码。

另一个可能发生的问题是,如果您没有为Gradle HOME环境变量设置环境变量。这个变量名为'GRADLE_HOME',应该设置为gradle主目录的路径,类似于'C:\gradle-1.12' 有时候你可能还想设置ANDROID_HOME的环境变量将其设置为YOUR-SDK-Root\sdk

完成后,返回Gradle任务窗口,双击assembleassemblerrelease。

如果一切都成功了,你应该能够进入app\build\apk文件夹并找到你的可部署apk文件。