How do you manage multiple environments while developing Android apps?

前端 未结 6 622
温柔的废话
温柔的废话 2020-12-22 23:04

We\'re building an Android app that connects to the cloud. We have a test URL for our APIs and a production URL. We connect the app to our local development machines to talk

6条回答
  •  悲&欢浪女
    2020-12-22 23:42

    With android studio and gradle its simple now.

    inside your app build.gradle edit signing configs

    signingConfigs {
        debug {
            storeFile file("debug.keystore")
            storePassword "..."
            keyAlias "..."
            keyPassword "..."
        }
    
        prod {
            storeFile file("prod.keystore")
            storePassword "..."
            keyAlias "..."
            keyPassword "..."
        }
    
        dev {
            storeFile file("dev.keystore")
            storePassword "..."
            keyAlias "..."
            keyPassword "..."
        }
    }
    

    add buildTypes

    buildTypes {
    
        debug {
            buildConfigField 'String', 'BASE_URL', '"http://127.0.0.1:8080/"'
            ......
            signingConfig signingConfigs.debug
        }
    
        prod {
    
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    
            buildConfigField 'String', 'BASE_URL', '"http://prod.example.com"'
            ......
            signingConfig signingConfigs.prod
        }
    
    
        dev {
            buildConfigField 'String', 'BASE_URL', '"http://dev.example.com"'
            ......
            signingConfig signingConfigs.dev
        }
    }
    

    In your code take base url configured in gradle file by this code.

    public final static String BASE_URL = BuildConfig.BASE_URL;
    

    You can also put different KEY or whatever which is build type specific in gradle file and in code it will take according to the build type you are running.

    Its even possible to have different package name.

    productFlavors {
        my_prod {
            applicationId "com.example.packtwo"
        }
        my_dev {
            applicationId "com.example.packone"
        }
    }
    

    In recent gradle config, there are some updates in specifying package name. You have to add flavourDimensions if using productFlavours. See below code with added flavourDimensions

    flavorDimensions "pack"
    
    productFlavors {
        flavor_dev {
            applicationId 'com.example.packtwo'
            dimension "pack"
        }
    
        flavor_prod {
            applicationId 'com.example.packone'
            dimension "pack"
        }
    }
    

    This will give you more details about product flavours and dimensions

    https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html

    Check for more possibilities...

    But if you are using different flavors you might have to deal with Manifest merging and all.

提交回复
热议问题