繁体   English   中英

如何在Android Studio中通过gradle生成Android Apk?

[英]How to generate Android Apk by gradle in Android Studio?

我正在尝试生成Android Apk,我已经设置了buildTypessigningConfigs ,如下所示:

android {
    signingConfigs {
        release {
            storeFile file('/Users/admin/Desktop/TestProject/Other/test')
            keyAlias = 'key0'
            storePassword '123456'
            keyPassword '123456'
        }
        dev {
            storeFile file('/Users/admin/Desktop/TestProject/Other/test')
            keyAlias = 'key0'
            storePassword '123456'
            keyPassword '123456'
        }
    }
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.test"
        minSdkVersion 27
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.TestAbstract.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            debuggable true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
        dev {
            debuggable = true
            signingConfig signingConfigs.dev
        }
    }
    dataBinding {
        enabled = true
    }
}

==========编辑==========

在此处输入图片说明

我已经通过构建->生成签名的APK->(输入密钥库详细信息)->选择构建变体来生成Apk

但是它没有在gradle中显示任何assembleDev或assembleRelease。

但是它没有在gradle显示像assembleReleaseassembleDev这样的gradle。

我错过了什么吗?

提前致谢。

在android studio中,转到Build-> Generate Signed APK->(输入密钥库详细信息)->选择Build变体(在这里您将看到在gradle文件中声明的变体)

这是一个有效的gradle设置。 只需应用您的signingConfigs。

对于项目级别gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

    repositories {
        google()
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
        maven { url 'https://maven.google.com/' }
        maven { url "https://jitpack.io" }
        maven { url 'https://tokbox.bintray.com/maven' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:4.3.0'

        // These docs use an open ended version so that our plugin
        // can be updated quickly in response to Android tooling updates

        // We recommend changing it to the latest version from our changelog:
        // https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin
        classpath 'io.fabric.tools:gradle:1.29.0'
    }
}

allprojects {
    allprojects {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
    repositories {
        google()
        jcenter()
        maven {
            url "https://jitpack.io"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

对于应用程序级别gradle:

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

android {
    bundle {
        density {
            // Different APKs are generated for devices with different screen densities; true by default.
            enableSplit true
        }
        abi {
            // Different APKs are generated for devices with different CPU architectures; true by default.
            enableSplit true
        }
        language {
            // This is disabled so that the App Bundle does NOT split the APK for each language.
            // We're gonna use the same APK for all languages.
            enableSplit false
        }
    }
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }
    // https://stackoverflow.com/questions/52521302/how-to-solve-program-type-already-present-com-google-common-util-concurrent-lis
    configurations {
        all*.exclude group: 'com.google.guava', module: 'listenablefuture'
    }
    signingConfigs {
        config {
            keyAlias 'xxx'
            keyPassword 'xxx'
            storeFile file('./YOUR.production.jks')
            storePassword 'xxx'
        }
    }
    flavorDimensions 'default'
    compileSdkVersion 28
    defaultConfig {
        multiDexEnabled true
        targetSdkVersion 28
        versionCode 21
        versionName "0.3.1"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    apply from: "./buildTypes.gradle", to: android

    productFlavors {
        production {
            applicationId "com.your.package"
            minSdkVersion 21
            targetSdkVersion 28
            signingConfig signingConfigs.config
            proguardFile './proguard-rules.pro'
        }
        dev {
            minSdkVersion 21
            applicationId "com.your.package.dev"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    //noinspection GradleCompatible
    implementation group: 'androidx.appcompat', name: 'appcompat', version: '1.0.2'
    implementation group: 'androidx.cardview', name: 'cardview', version: '1.0.0'
    implementation 'com.google.android.material:material:1.1.0-alpha07'
    implementation group: 'androidx.preference', name: 'preference', version: '1.0.0'
    implementation group: 'com.firebaseui', name: 'firebase-ui-firestore', version: '4.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation group: 'androidx.arch.core', name: 'core-runtime', version: '2.0.1'
    implementation group: 'com.google.firebase', name: 'firebase-auth', version: '16.1.0'
    implementation group: 'com.google.firebase', name: 'firebase-core', version: '16.0.9'
    implementation group: 'com.google.android.gms', name: 'play-services-auth', version: '17.0.0'
    implementation group: 'com.google.android.gms', name: 'play-services-analytics', version: '17.0.0'
    implementation('com.crashlytics.sdk.android:crashlytics:2.10.1@aar') {
        transitive = true;
    }
    implementation 'com.github.javiersantos:MaterialStyledDialogs:2.1'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

apply plugin: 'com.google.gms.google-services'

buildTypes.gradle:

signingConfigs {
    debug {
        storeFile file("./debug.keystore")
        storePassword "android"
        keyAlias "androiddebugkey"
    }
    release {
        storeFile file("./YOUR.production.jks")
        storePassword "xxx"
        keyAlias "xxx"
        keyPassword "xxx"
    }
}

buildTypes {
    debug {
    }
    release {
        shrinkResources true
        minifyEnabled true
        zipAlignEnabled true
//        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.config
    }
}

proguard-rules.pro:

# Uncomment this to preserve the line number information for
# debugging stack traces.
-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
-renamesourcefileattribute SourceFile

# https://www.guardsquare.com/en/products/proguard/manual/usage#repackageclasses
-repackageclasses 'zzz1'
-forceprocessing
-allowaccessmodification
#-ignorewarnings

gradle.properties:

android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1536m
android.injected.testOnly=false
#android.debug.obsoleteApi=true
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true

此设置将创建四个构建变体:

  • devDebug
  • devRelease
  • 生产调试
  • 生产发布

确保仅使用devDebug,productionRelease。

希望这可以帮助。 使用前请先检查并根据需要进行编辑。 祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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