2016-09-17 109 views
1

可变string.xml我有在String.xml访问Java或从摇篮

<string name="id">4</string> 

一个字符串值,我有其中包含一个变量

public static int Id=1; 

现在什么一类我需要的是我想要在Gradle中获取这两个值中的任何一个,它将检查条件并根据条件重命名我的应用程序。下面给出的是gradle这个代码的一部分

def identifier //here i need to get value from the java or xml 
switch(identifier) 
{ 
    case 1: 
    temp="ApplicationNewName";break; 
    } 
newName=newName.replace("-release",temp); 
output.output.File=new File(output.outputFile.parent,newName); 

我的问题是,我可以访问在gradle这个文件Java文件或XML字符串初始化变量?

回答

0

您正在向后处理这个问题,Gradle让您能够在脚本本身中设置这些变量,然后您可以在整个Android代码中进一步访问这些变量。以下是您如何设置构建配置变量的相关答案:https://stackoverflow.com/a/17201265/2168085

这听起来像是您试图从单个代码库构建不同的应用程序,或构建这些应用程序的变体。如果是这种情况,那么你应该仔细研究一下build flavors来解决这个问题。基本上,构建风格允许您从单个主代码库构建不同的应用程序,并将变体或新功能应用于不同的风格。这可以像拥有免费和付费版本的应用程序或全白标签代码库一样基本,您可以在同一主代码库中构建非常不同的应用程序。在Android的这些通常被称为build variants和开发文档提供了很多很好的信息,如何开始:https://developer.android.com/studio/build/build-variants.html

-1
applicationVariants.all { variant -> 
     def appName 
     if (project.hasProperty("applicationName")) { 
      appName = "${applicationName}" 
     } else { 
      appName = parent.name 
     } 
     variant.outputs.all { output -> 
      def newApkName 
      newApkName = "${appName}_${variant.versionCode}_${variant.versionName}.apk" 
      outputFileName = new File(newApkName) 
     } 
    } 
+0

考虑,解释你的代码是如何解答的问题。 –