2013-12-20 555 views
39

我想我們的Android應用程序轉換爲一個構建的gradle。我有這個項目,它的圖書館建設成功。我現在正在嘗試爲我們的各種環境創建單獨的應用程序(dev/test/prod擁有不同的URL,以供他們使用的寧靜服務)。BuildConfig沒有得到正確創建(搖籃安卓)

在周圍尋找,我覺得這樣做是讓不同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 

我構建/源/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; 
} 

我在做什麼錯?

+0

可能是有意義的,以消除那些自由引用。並不是說這裏有任何關鍵代碼,但最好避免在StackOverflow上共享專有代碼;) – antonpug

+0

請參閱下面的鏈接: http://stackoverflow.com/questions/22604627/gradle-buildconfigfield-buildconfig-cannot-resolve-symbol – Michael

回答

50

請確保您正在建設「開發」或「刺」變種。默認的「debug」和「release」變體中沒有BuildConfig定義。在Android Studio中,您可以在左下角選擇當前變種:

Build Variants

爲了簡化您的build.gradle文件,您可以定義:

buildTypes { 
    debug { 
     buildConfigField "String", "URL_SEARCH", "\"https://dev-search.example.com\"" 
     // etc. 
    } 
    release { 
     buildConfigField "String", "URL_SEARCH", "\"https://search.example.com\"" 
     // etc.  
    } 
} 

,然後只用默認的「調試」和「發佈」變體。從buildConfigField參數的值;:

最後,刪除分號( '' 符號)。

+0

謝謝......正如我預料的那樣,這是我的愚蠢。 「gradle installDev」工作正常....「gradle build」給了我錯誤。 – Innova

+4

你也可以去像這樣'buildConfigField「串」,「URL_SEARCH」,「‘https://search.example.com’」' – Androiderson

0

我必須建立使用.initWith(someotherbuildtype)的類型是建立相關類似的問題,BuildConfig沒有被正確創建。我不得不切換到父構建變體並首先構建它,然後使用父構建好的構建類型。

6

以防萬一,幫助別人,在我的情況,這是一個缺少導入: import uk.co.yourpackage.yourapp.BuildConfig;

不知何故,無處在doc沒有提到你需要的是包括!讓我覺得它以某種方式自動導入,但它不是。至少不是對我來說......這麼多時間輸了......希望能幫助像我這樣的新手!

+0

最好的,它是進口的問題,我是從進口android.support.v4.BuildConfig 謝謝:) –

41

我有同樣的問題,並修復它象下面這樣:

buildConfigField 'String', 'BASE_URL', '"https://api.example.com"' 
+3

for'String',雙引號不能正確生成。只有單引號才能成功生成變量。 – Youngjae

+0

爲我工作。謝謝:) –

+0

對於像我這樣第一次錯過它的人來說,關鍵部分就是這個值在雙引號內用雙引號引起來。 – speckledcarp