相关疑难解决方法(0)

将API密钥保存在gradle.properties中

我是android的新手,正在开发一个项目,我看到我得到的API密钥保存gradle.properties为:

MyOpenWeatherMapApiKey="1c3ae96f93a0094e8a7chsjdgfid04aed3f10"

然后在build.gradle(module:app)我添加以下行:

buildTypes.each {
            it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', MyOpenWeatherMapApiKey
      }
Run Code Online (Sandbox Code Playgroud)

所以,在我的主程序中,我使用这个api访问数据,其URL由这段代码获得:

final String FORECAST_BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?";
            final String QUERY_PARAM = "q";
            final String FORMAT_PARAM = "mode";
            final String UNITS_PARAM = "units";
            final String DAYS_PARAM = "cnt";
            final String APPID_PARAM = "APPID";
            Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
                    .appendQueryParameter(QUERY_PARAM, params[0])
                    .appendQueryParameter(FORMAT_PARAM, format)
                    .appendQueryParameter(UNITS_PARAM, units)
                    .appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
                    .appendQueryParameter(APPID_PARAM, BuildConfig.OPEN_WEATHER_MAP_API_KEY)
                    .build();
            URL url = new URL(builtUri.toString());
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是为什么要采取所有变化的紧张来将appid存储在gradle部分.我们不能直接在主程序中访问吗?

我的问题的第二部分是在gradle部分实际发生了什么,尤其是buildTypes.each{}块?

api android

19
推荐指数
3
解决办法
1万
查看次数

Android BuildConfig字段生成字符串不正确

在开始一个新项目的最后一天,我在构建配置中创建了一些变量,作为在发布和调试版本之间单独处理它们的一个好习惯,如下所示:

...
buildTypes {
    release {
        minifyEnabled false
        buildConfigField("String", "PARSE_APP_ID", "xxxxxxxxxxxxxxxxxxxxxxxxxxx")
        buildConfigField("String", "PARSE_CLIENT_ID", "xxxxxxxxxxxxxxxxxxxxxxxxxx")
        buildConfigField("String", "FACEBOOK_APP_ID", "999999999999999")
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug{
        minifyEnabled false
        buildConfigField("String", "PARSE_APP_ID", "xxxxxxxxxxxxxxxxxxxxxxxxxxx")
        buildConfigField("String", "PARSE_CLIENT_ID", "xxxxxxxxxxxxxxxxxxxxxxxxxx")
        buildConfigField("String", "FACEBOOK_APP_ID", "999999999999999")
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
...
Run Code Online (Sandbox Code Playgroud)

问题是它没有正确创建字符串它是直接打印字符串没有这样的引号:

public final class BuildConfig {
  .....
  // Fields from build type: debug
  public static final String FACEBOOK_APP_ID = 99999999999999999;
  public static final String PARSE_APP_ID = xxxxxxxxxxxxxxxxxxxxxxxx;
  public static final String PARSE_CLIENT_ID = xxxxxxxxxxxxxxxxxxxxxxxxx;
}
Run Code Online (Sandbox Code Playgroud)

显然这会给你编译错误,我试图在stackoverflow中找到如何修复这个,因为这是在我的构建中导致错误,没有找到任何关于这个.如何使用引号正确地使用gradle打印字符串?

android gradle

7
推荐指数
2
解决办法
1086
查看次数

有没有办法像我们在 laravel 中那样在 Android (java) 中使用 .env 变量?

我不知道这是否离题,但在 Laravel 中,我们可以在.env文件中设置变量,然后我们可以使用getenv()函数访问该文件以防止对其进行硬编码,这可能不安全。

我们可以在 Android Studio(使用 Java)中做同样的事情吗?由于有一些信息,我宁愿不进行硬编码。

希望这是有道理的。

我尝试在类中设置变量并通过类访问它们,但我觉得有一种更好的方法,更类似于在 Laravel 中的做法。

这是我现在正在做的事情,但这不是我要找的:

public class EnvVariables {
    public final String API_KEY = "some API key";
    public final String API_ENDPOINT = "https://example.com/api/v1/endpoint";
}
Run Code Online (Sandbox Code Playgroud)

然后我在需要的地方使用这些变量。

这可以完成工作,但是该类仍然可以轻松访问,而 a.env则不是(因为它不应该)。

java android

4
推荐指数
1
解决办法
2796
查看次数

错误:Gradle 同步失败:无法获取 DefaultConfig_Decolated 的未知属性“API_KEY”

错误:无法获取 DefaultConfig_Decolated{name=main、dimension=null、minSdkVersion=null、targetSdkVersion=null、renderscriptTargetApi=null、renderscriptSupportModeEnabled=null、renderscriptSupportModeBlasEnabled=null、renderscriptNdkModeEnabled=null、versionCode=null、versionName 的未知属性“API_KEY” =null、applicationId=null、testApplicationId=null、testInstrumentationRunner=null、testInstrumentationRunnerArguments={}、testHandleProfiling=null、testFunctionalTest=null、signingConfig=null、resConfig=null、mBuildConfigFields={}、mResValues={}、mProguardFiles=[] 、mConsumerProguardFiles=[]、mManifestPlaceholders={}、mWearAppUnbundled=null},类型为 com.android.build.gradle.internal.dsl.DefaultConfig。应用插件:'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.2'
    defaultConfig {
        buildConfigField("String", "API_KEY", API_KEY)        //error here
        buildConfigField("String", "ER_API_KEY", ER_API_KEY)
        applicationId "com.gpads.gautham.imagetotextanalysis"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 2
        versionName "2.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    // ... other values
}
Run Code Online (Sandbox Code Playgroud)

java android gradle android-studio build.gradle

3
推荐指数
1
解决办法
9989
查看次数

如何在 AndroidManifest.xml 中注入主机和架构以进行 Android 深度链接

我正在尝试使用 build.gradle 将 url 架构和路径动态注入到我的 AndroidManifest.xml 文件中作为参考

但是,这不允许触发深度链接。

当我在 AndroidManifest.xml 中使用静态值时,我测试了深度链接的工作情况。

            // AndroidManifest.xml
            <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:scheme="${appScheme}"
                android:host="${hostName}"
                android:path="/path2" />

            <data
                android:scheme="${appScheme}"
                android:host="${hostName}"
                android:path="/path1" />
        </intent-filter>




// build.gradle (:app)
defaulConfig {
        manifestPlaceholders = [
        hostName: "www.host_name.com",
        appScheme: "https"
    ]
}


  demo {
        resValue "string", "hostName", "www.host_demo.com"
        resValue "string", "appScheme", "https"
    }

    staging {
        resValue "string", "hostName", "www.host_staging.com"
        resValue "string", "appScheme", "http"
    }
Run Code Online (Sandbox Code Playgroud)

android deep-linking android-manifest

2
推荐指数
1
解决办法
2765
查看次数