1
votes

I am trying to generate Android Apk, I have setup the buildTypes and signingConfigs like the following:

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
    }
}

==========EDIT==========

enter image description here

I have generate Apk via Build -> Generate Signed APK -> (Enter keystore details) -> Select Build variant.

But it has not show any assembleDev or assembleRelease in gradle.

But it did not show the gradle like assembleRelease or assembleDev in gradle.

Did I missing something ?

Thanks in advance.

2
do you want to achieve this through android studio ?a_local_nobody
You want to run gradle commands from android studio terminal? like... 'gradlew assembleRelease'Ranjan Kumar
have you found a solution for this yet ?a_local_nobody
No, I gave up to build APK by gradle. finally I use Build --> Generate Signed Bundle or APK --> APK --> type the password --> choose the Build Variants to build the Apk for every build type.Wun

2 Answers

0
votes

In android studio, go to Build -> Generate Signed APK -> (Enter keystore details) -> Select Build variant (Here you will see your variants declared in gradle file)

0
votes

Here is a working gradle setup. Just apply your signingConfigs.

For project level 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
}

For app level 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://stackguides.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

This setup will create four build variant:

  • devDebug
  • devRelease
  • productionDebug
  • productionRelease

Make sure to use only devDebug, productionRelease.

Hope this helps. Please review before using and edit as needed. Goodluck