【问题标题】:How to generate Android Apk by gradle in Android Studio?如何在 Android Studio 中通过 gradle 生成 Android Apk?
【发布时间】:2019-07-12 08:37:49
【问题描述】:

我正在尝试生成 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
    }
}

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

我已经通过 Build -> Generate Signed APK -> (Enter keystore details) -> Select Build variant.

生成了 Apk。

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

但它没有像 assembleReleaseassembleDev 在 gradle 中显示 gradle

我错过了什么吗?

提前致谢。

【问题讨论】:

  • 你想通过android studio实现这个吗?
  • 您想从 android studio 终端运行 gradle 命令吗?像...'gradlew assembleRelease'
  • 你找到解决方案了吗?
  • 不,我放弃了 gradle 构建 APK。最后我使用 Build --> Generate Signed Bundle or APK --> APK --> 输入密码 --> 选择 Build Variants 为每种构建类型构建 Apk。

标签: android android-studio android-gradle-plugin


【解决方案1】:

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

【讨论】:

  • 我尝试过,但它没有在我的 gradle 列表中显示 assembleDev 或 assembleRelease。查看我的编辑。
【解决方案2】:

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

对于项目级 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、productionRelease。

希望这会有所帮助。请在使用前查看并根据需要进行编辑。祝你好运

【讨论】:

  • 我的设置在 buildTypes 和 signingConfigs 中似乎与您的类似。我在 Build Variants 中有构建类型。但它没有显示在 gradle 中。
  • @Wun 从我的帖子中获取您需要的内容。
猜你喜欢
  • 2018-03-11
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多