【问题标题】:Setting up development and production environment for android application in Android Studio在 Android Studio 中设置 android 应用程序的开发和生产环境
【发布时间】:2018-03-08 07:26:03
【问题描述】:

我有两个关于这个问题的问题。

  1. 如果在 laravel/web 我们有 .env 文件将环境设置为“开发”或生产并自动连接到不同的数据库。在 android/kotlin/android studio 中怎么样?

  2. 以及如何在“开发”环境中向 PC 上的本地主机 (127.0.2.1) 发出我的应用程序请求,如果在“生产”环境中,如何请求真正的 url API。仅供参考,我不使用模拟器。我用手机测试我的应用程序。

【问题讨论】:

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


    【解决方案1】:

    是的,这在您的 Android 应用程序中也是可能的。您只需修改您的build.gradle 文件,即可根据您的开发、测试或生产环境管理您的BuildConfig

    这是来自我的一个项目的示例 build.gradle 文件。

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    
    def keystorePropertiesFile = rootProject.file("../Path_To_KeyStore/keystore.properties")
    def keystoreProperties = new Properties()
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
    
    def appPropertiesFile = rootProject.file("app-settings.properties")
    def appProperties = new Properties()
    appProperties.load(new FileInputStream(appPropertiesFile))
    
    android {
        compileSdkVersion 27
        buildToolsVersion "27.0.3"
    
        signingConfigs {
            MyAppSigningConfig {
                keyAlias keystoreProperties['keyAlias']
                keyPassword keystoreProperties['keyPassword']
                storeFile file(keystoreProperties['storeFile'])
                storePassword keystoreProperties['storePassword']
            }
        }
    
        defaultConfig {
            applicationId "com.example.myapp"
            minSdkVersion 21
            targetSdkVersion 27
            versionCode appProperties['app.version.code'] as int
            versionName appProperties['app.version.name']
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    
            def buildVariant = getBuildVariant()
            def environmentPath
            if ((buildVariant == "Release")) {
                environmentPath = appProperties["env.path.live"]
            } else if ((buildVariant == "Debug")) {
                environmentPath = appProperties["env.path.test"]
            } else {
                environmentPath = appProperties["env.path.live"]
            }
    
            def envPropertiesFile = rootProject.file(environmentPath)
            def envProperties = new Properties()
            envProperties.load(new FileInputStream(envPropertiesFile))
            println("buildVariant = $buildVariant")
            for (String key : envProperties.keySet()) {
                buildConfigField "String", key.replaceAll("\\.", "_").toUpperCase(), envProperties[key]
            }
        }
    
        buildTypes {
            debug {
                applicationIdSuffix ".debug"
                manifestPlaceholders = [appName: "@string/app_name_debug_test"]
            }
    
            release {
                manifestPlaceholders = [appName: "@string/app_name"]
                signingConfig signingConfigs.MyAppSigningConfig
                minifyEnabled false
                multiDexEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    def getBuildVariant() {
        for (TaskExecutionRequest t : gradle.getStartParameter().getTaskRequests()) {
            for (String command : t.args) {
                if (command.matches(":app:generate(.*)Sources")) {
                    return command.replaceAll(":app:generate(.*)Sources", "\$1")
                } else if (command.matches(":app:assemble(.*)")) {
                    return command.replaceAll(":app:assemble(.*)", "\$1")
                }
            }
        }
    
        return "Release"
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
        implementation 'com.android.support:appcompat-v7:27.1.0'
        implementation 'com.android.support:recyclerview-v7:27.1.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    }
    

    我在这里有两种不同的构建变体。一个用于发布,另一个用于调试。我在应用程序目录中有三个属性文件。这些如下。

    app-settings.properties 文件如下所示。

    app.version.code=1
    app.version.name=0.0.1
    env.path.live=live-env.properties
    env.path.test=test-env.properties
    

    test-env.properties 看起来像

    base.url.auth="http://localhost:8888/auth/"
    base.url.communication="http://localhost:8000/communication/"
    base.url.site="http://localhost:8000/"
    api.key.auth="demo_key"
    

    live-env.properties 就像

    base.url.auth="http://auth.yourapp.com/auth/"
    base.url.communication="http://yourapp.com/communication/"
    base.url.site="http://yourapp.com/"
    api.key.auth="live_key1223ssHHddSSYYY"
    

    因此,一旦设置了build.gradle 和应用程序属性,您需要与 gradle 同步以生成BuildConfig.java 文件。您将看到该文件是使用从您的属性文件中找到的值自动生成的。

    您可以从代码中的任何位置访问环境变量,如下所示。

    const val BASE_URL = BuildConfig.BASE_URL_SITE
    const val BASE_URL_AUTH = BuildConfig.BASE_URL_AUTH
    

    从 Android Studio 中构建变体的左侧菜单中获取所需的应用构建。

    我的一位名叫 Sajid Shahriar 的同事帮助我了解了不同构建变体的设置。因此,我与您分享这个。希望对您有所帮助。

    【讨论】:

    • 仅供参考,使用新版 Android Studio,Android Studio 北极狐 | 2020.3.1补丁2,方法getBuildVariant中的代码不起作用。看来参数数组是空的。
    • 另一个有趣的细节。当您更改变体时,不会重新生成 BuildConfig。您必须重建项目才能正常工作。
    猜你喜欢
    • 2015-04-04
    • 2019-03-17
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多