2013-12-20 61 views
39

私はAndroidアプリケーションをgradleビルドに変換しようとしています。私はプロジェクトを持っており、図書館は成功しています。私は今、さまざまな環境(dev/test/prodが消費する安らかなサービスのために異なるURLを持つ)のために別々のapkを作成しようとしています。BuildConfigが正しく作成されない(Android Gradle)

私がこれを行うには、環境ごとに異なる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

"dev"または "prod"の亜種を作成してください。デフォルトの "debug"と "release"にはBuildConfig定義がありません。 Androidのメーカーでは、あなたが左下隅に現在のバリアントを選択することができます。

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

をしてからちょうどデフォルトの「デバッグ」を使用します。

Build Variants

は、あなたのbuild.gradleファイルを簡素化するために、あなたが定義することができますと "リリース"バリアント。

最後に、buildConfigFieldパラメータの値からセミコロン(記号: ';')を削除します。

+0

ありがとう...私が期待していたように、それは私が大嫌いでした。 "gradle installDev"はうまくいきます。 "gradle build"でエラーが出ました。 – Innova

+4

あなたはこの 'buildConfigField 'String'、 'URL_SEARCH'、 '" https://search.example.com "'' – Androiderson

0

.initWith(someotherbuildtype)を使用してセットアップするビルドタイプに関連する同様の問題がありましたが、BuildConfigが正しく作成されていませんでした。私は、親のビルドバリアントに切り替えて、それを最初にビルドしなければなりませんでした。 どういうわけかimport uk.co.yourpackage.yourapp.BuildConfig;

、どこにもdocには、それはあなたがそれには、必要と言及していないん:他の誰かを助け念のため

6

は、私の場合には欠けて輸入しました!私はそれが何とか自動的にインポートされたと思ったが、それはそうではない。私のためではない少なくとも...時間が失われた...希望は私のような別の初心者に役立ちます!

+0

のように行くことができます驚くべきことに、インポートの問題で、私はandroid.support.v4.BuildConfig ありがとうございました:) –

41

私は同じ問題を持っていたし、下記のようにそれを修正:

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

の 'String'では、二重引用符は正しく生成されません。一重引用符でのみ変数が正常に生成されます。 – Youngjae

+0

私のために働いた。ありがとう:) –

+0

私が最初にそれを見逃した人のために、重要な部分は、値がシングルクォートの中にダブルクォートであることです。 – speckledcarp

関連する問題