Inn*_*ova 44 android buildconfig gradle android-build android-gradle-plugin
我正在尝试将我们的Android应用程序转换为gradle构建.我有项目,它的库成功建立.我现在正在尝试为我们的各种环境创建单独的apks(dev/test/prod为他们使用的restful服务提供了不同的url).
在搜索时,我觉得这样做的最佳方式是为每个环境制作不同的BuildConfig.这是我试过的:
import java.util.regex.Pattern
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:+'
}
}
apply plugin: 'android'
task('increaseVersionCode') << {
def manifestFile = file("AndroidManifest.xml")
def pattern = Pattern.compile("versionCode=\"(\\d+)\"")
def manifestText = manifestFile.getText()
def matcher = pattern.matcher(manifestText)
matcher.find()
def versionCode = Integer.parseInt(matcher.group(1))
def manifestContent = matcher.replaceAll("versionCode=\"" + ++versionCode + "\"")
manifestFile.write(manifestContent)
}
tasks.whenTaskAdded { task ->
if (task.name == 'generateReleaseBuildConfig') {
task.dependsOn 'increaseVersionCode'
}
}
dependencies {
compile 'com.android.support:support-v4:19.0.0'
compile files('libs/commons-io-2.4.jar',
'libs/google-play-services.jar',
'libs/gson-2.2.4.jar',
'libs/universal-image-loader-1.8.6.jar',
'libs/wakeful-1.0.1.jar')
compile project(':pulltorefresh_lib')
compile project(':edgeeffect_lib')
compile project(':viewpagerindicator_lib')
}
android {
buildToolsVersion "18.1.1"
compileSdkVersion "Google Inc.:Google APIs:18"
defaultConfig {
minSdkVersion 14
targetSdkVersion 18
}
buildTypes {
debug {
packageNameSuffix ".debug"
}
dev.initWith(buildTypes.debug)
dev {
buildConfigField "String", "URL_SEARCH", "\"https://dev-search.example.com\";"
buildConfigField "String", "URL_CONNECT", "\"https://dev-connect.example.com\";"
buildConfigField "String", "URL_SVC_NEWSLIST", "\"https://dev-mobilenews.example.com/newslist\";"
buildConfigField "String", "URL_SVC_NEWSDETAIL", "\"https://dev-mobilenews.example.com/newsdetail\";"
buildConfigField "String", "URL_SVC_REGISTERENDPOINTS", "\"https://dev-mobilenews.example.com/registerendpoints\";"
}
prod.initWith(buildTypes.release)
prod {
buildConfigField "String", "URL_SEARCH", "\"https://search.example.com\";"
buildConfigField "String", "URL_CONNECT", "\"https://connect.example.com\";"
buildConfigField "String", "URL_SVC_NEWSLIST", "\"https://mobilenews.example.com/newslist\";"
buildConfigField "String", "URL_SVC_NEWSDETAIL", "\"https://mobilenews.example.com/newsdetail\";"
buildConfigField "String", "URL_SVC_REGISTERENDPOINTS", "\"https://mobilenews.pdc-np-cf.lmig.com/registerendpoints\";"
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我的BuildConfig.java似乎没有注入静态变量,因此我得到类似于的错误:
/Users/path/to/project/MainActivity.java:348: error: cannot find symbol
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(BuildConfig.URL_SEARCH)));
^
symbol: variable URL_SEARCH
location: class BuildConfig
/Users/path/to/project/MainActivity.java:359: error: cannot find symbol
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(BuildConfig.URL_CONNECT)));
^
symbol: variable URL_CONNECT
location: class BuildConfig
/Users/path/to/project/MainActivity.java:600: error: cannot find symbol
HttpPost httpPost = new HttpPost(BuildConfig.URL_SVC_REGISTERENDPOINTS);
^
symbol: variable URL_SVC_REGISTERENDPOINTS
location: class BuildConfig
/Users/path/to/project/service/AlarmNotificationService.java:145: error: cannot find symbol
String requestUrl = BuildConfig.URL_SVC_NEWSLIST + "?"
^
symbol: variable URL_SVC_NEWSLIST
location: class BuildConfig
/Users/path/to/project/service/NewsService.java:240: error: cannot find symbol
String requestUrl = BuildConfig.URL_SVC_NEWSLIST + "?"
^
symbol: variable URL_SVC_NEWSLIST
location: class BuildConfig
/Users/path/to/project/service/NewsService.java:530: error: cannot find symbol
HttpPost httpPost = new HttpPost(BuildConfig.URL_SVC_NEWSDETAIL);
^
symbol: variable URL_SVC_NEWSDETAIL
location: class BuildConfig
6 errors
Run Code Online (Sandbox Code Playgroud)
我的build/source/buildConfig/debug/com /.../ BuildConfig.java文件包含:
/**
* Automatically generated file. DO NOT MODIFY
*/
package com....;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String PACKAGE_NAME = "com.....debug";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 5;
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
erv*_*eeg 130
就我而言,我必须启用 buildConfig 作为构建功能。
android {
buildFeatures {
buildConfig true
}
}
Run Code Online (Sandbox Code Playgroud)
Pet*_*nut 55
请确保您正在构建"dev"或"prod"变体.默认的"debug"和"release"变体中没有BuildConfig定义.在Android Studio中,您可以在左下角选择当前变体:

要简化build.gradle文件,您可以定义:
buildTypes {
debug {
buildConfigField "String", "URL_SEARCH", "\"https://dev-search.example.com\""
// etc.
}
release {
buildConfigField "String", "URL_SEARCH", "\"https://search.example.com\""
// etc.
}
}
Run Code Online (Sandbox Code Playgroud)
然后只使用默认的"debug"和"release"变体.
最后,从buildConfigField参数的值中删除分号(sign:';').
sav*_*ion 47
我有同样的问题并修复如下:
buildConfigField 'String', 'BASE_URL', '"https://api.example.com"'
Run Code Online (Sandbox Code Playgroud)
Mir*_*aig 30
先做
File -> Invalidate Caches/Restart... -> Invalidate and Restart
Run Code Online (Sandbox Code Playgroud)
然后做
Build -> Clean Project
Run Code Online (Sandbox Code Playgroud)
然后做
Build - > Rebuild Project
Run Code Online (Sandbox Code Playgroud)
Erf*_*tfi 14
将其添加到build.gradle
buildFeatures {
buildConfig = true
}
Run Code Online (Sandbox Code Playgroud)
如果使用喷气背包组成
buildFeatures {
compose = true
buildConfig = true
}
Run Code Online (Sandbox Code Playgroud)
Nob*_*dy8 13
您的文件中需要包含这一行gradle.properties:
android.defaults.buildfeatures.buildconfig=true
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
Lit*_*ome 11
为了防止其他人,在我的情况下,它是一个缺少导入:
import uk.co.yourpackage.yourapp.BuildConfig;
不知何故,在文档中没有提到你需要包括!让我觉得它是以某种方式自动导入但它不是.至少不适合我......失去了太多时间......希望能帮助像我这样的新手!
小智 10
试着做
构建 -> 清理项目
然后做
构建 -> 重建项目
如果它不起作用,请尝试
File -> Invalidate Caches/Restart... -> Invalidate and Restart
然后做
构建 -> 清理项目
然后做
构建 -> 重建项目
小智 7
我解决了这个问题,如下所示,在这种情况下,Android studio 将自行生成构建配置文件
File -> Sync Project with Gradle Files
Run Code Online (Sandbox Code Playgroud)
非常合乎逻辑的答案,如果这不是在撰写中发生的
buildFeatures {
// The first line should already be in your project!
compose = true
buildConfig = true
}
Run Code Online (Sandbox Code Playgroud)
现在将生成文件,您可以在应用程序和任何地方使用 BuildConfig.Debug。
适用于 Android Studio 版本
Android Studio 海豚 | 2021.3.1 补丁1
内部版本 #AI-213.7172.25.2113.9123335,建于 2022 年 9 月 30 日
运行时版本:11.0.13+0-b1751.21-8125866 aarch64 VM:JetBrains sro 的 OpenJDK 64 位服务器 VM
我想做什么?
我为应用程序模块设置了三个构建变体,我希望每次更改构建变体时都更改 BuildConfig,以针对 build.gradle(应用程序)配置中定义的特定变量。
什么不起作用
我已经尝试了所有三种方法,但根本不起作用,为我正在更改的构建变体重新生成 BuildConfig:
File > invalidate cache and restart随着Build > Clean Project&Build > Rebuild ProjectBuild > Make Project独自的File > Sync Project with Gradle Files独自的什么有效
然后我尝试了一种组合,效果出奇的好:
Build > Make ProjectFile > Sync Project with Gradle Files我希望它适用于该特定版本,我不确定是否适用于 Android Studio 的其他版本。
感谢@ravi 和@ibrokhim 对此案例的部分回答。
| 归档时间: |
|
| 查看次数: |
53068 次 |
| 最近记录: |