5 题: BuildConfig未正确创建(Gradle Android)

在...创建的问题 Mon, Apr 27, 2015 12:00 AM

我正在尝试将我们的Android应用程序转换为gradle版本。我有项目,它的库成功建立。我现在正在尝试为我们的各种环境创建单独的apks(dev /test /prod为他们使用的其余服务设置了不同的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']
        }
    }
}

问题是我的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

我的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;
}

我做错了什么?

    
42
  1. 删除那些Liberty引用可能有意义。并不是说这里有任何关键代码,但最好避免在StackOverflow上共享专有代码;)
    2014-05-30 18:27:30Z
  2. 2016-06-27 09:29:08Z
  3. 2018-10-04 12:49:55Z
  4. 醇>
    5答案                              5 跨度>                         

    请确保您正在构建“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.      
        }
    }
    

    然后只使用默认的“debug”和“release”变体。

    最后,从buildConfigField参数的值中删除分号(符号:';')。

        
    53
    2015-11-19 03:40:34Z
    1. 谢谢......正如我所料,这是我的愚蠢行为。 “gradle installDev”工作正常....“gradle build”给了我错误。
      2013-12-20 16:19:22Z
    2. 你也可以像这样buildConfigField 'String', 'URL_SEARCH', '"https://search.example.com"'
      2015-07-03 23:03:09Z
    3. 醇>

    我有同样的问题并修改如下:

     
    buildConfigField 'String', 'BASE_URL', '"https://api.example.com"'
    
        
    45
    2015-11-18 11:38:47Z
    1. 对于String
      ,双引号不能正确生成。只有单引号才能成功生成变量。
      2015-11-19 05:02:31Z
    2. 为我工作。谢谢:)
      2016-06-10 12:02:07Z
    3. 对于像我这样第一次错过它的人来说,关键部分是单引号内的双引号值。
      2016-12-30 20:34:43Z
    4. 醇>

    为了防止其他人,在我的情况下,它是一个缺少导入:  import uk.co.yourpackage.yourapp.BuildConfig;

    不知何故,在文档中没有提到你需要包括!让我觉得它是以某种方式自动导入但它不是。至少不适合我......失去了很多时间......希望能帮到像我这样的新手!

        
    9
    2015-07-27 20:20:21Z
    1. 太棒了,这是导入问题,我是从android.support.v4.BuildConfig导入的。谢谢:)
      2017-04-10 18:55:30Z
    2. 醇>

    我遇到了与使用.initWith(someotherbuildtype)设置的构建类型相关的类似问题,BuildConfig未正确创建。我不得不切换到父构建变体并首先构建它,然后构建类型initWith父进程构建正常。

        
    0
    2015-04-27 00:56:44Z

    以下是为我解决的问题:

     
    File -> Invalidate Caches/Restart... -> Invalidate and Restart 
    
        
    0
    2019-02-20 21:19:40Z
来源放置 这里