Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

How do you manage multiple environments while developing Android apps?

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 to the database when developing but find ourselves modifying a global API URL to the production URL every time we generate an APk for the Play Store.

Is there a better way to manage environments for Android? Can we also have two versions of the app (development version) and the Play Store version? I am not able to have two versions as both the apps have the same signature. How do we best manage this?

question from:https://stackoverflow.com/questions/22995057/how-do-you-manage-multiple-environments-while-developing-android-apps

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...