有没有办法在我的代码中使用build.gradle中的变量,这取决于flavor AND buildType?
在这个例子中:是否可以在Gradle中声明一个可用于Java的变量?新资源值仅取决于其是否为调试版本或发布版本类型.
我想拥有的是每个可能的buildVariant的一个变量.
像这样的东西:
flavor1Debug => resValue "string", "app_name", "App 1 Debug"
flavor1Release => resValue "string", "app_name", "App 1"
flavor2Debug => resValue "string", "app_name", "App 2 Debug"
flavor2Release => resValue "string", "app_name", "App 2"
Run Code Online (Sandbox Code Playgroud)
有没有一种很好的方法可以通过build.gradle或其他不包含switch或if语句的方式来实现这一点?
我在src / main / java / com / company / project / Config.java中有以下课程:
public class Config {
public static final boolean DEBUG = true;
...
}
Run Code Online (Sandbox Code Playgroud)
因此,在其他某个类中,我可以知道Java编译器在评估结果为false时会剥离if()语句,从而可以执行以下操作:
import static com.company.project.Config.DEBUG
if (DEBUG) {
client.sendMessage("something");
log.debug("something");
}
Run Code Online (Sandbox Code Playgroud)
在Gradle中,什么是在编译时过滤和更改Config.java中的DEBUG值而不修改原始文件的最佳方法?
到目前为止,我在想:
以上可能吗?有没有更好的办法?
我想访问 build.gradle 属性,以便我可以在我的应用程序中自动化一些流程。
这是我目前的结构:
rootProject/build.gradle
buildscript {
ext {
buildTools = "25.0.2"
minSdk = 16
compileSdk = 23
targetSdk = 23
versions = [supportLib : '25.0.0',
multidex : '1.0.1',
playServices : '10.0.1']
libs = [appcompat : "com.android.support:appcompat-v7:$versions.supportLib",
design : "com.android.support:design:$versions.supportLib"]
.... Other libraries and setup....
}
}
Run Code Online (Sandbox Code Playgroud)
rootProject/app/build.gradle
...
dependencies {
// Support libraries
compile libs.appcompat
compile libs.design
.....
}
Run Code Online (Sandbox Code Playgroud)
我想在 Java 和 build.gradle 端访问属性版本和libs变量。我怎样才能做到这一点?
嗨,也许这是一个愚蠢的问题,但我对 groovy 和 gradle 不太熟悉,无法得到确切的答案。
public class Config{
public static final String APPLICATION_ID="com.app.dracula";
public static final int VERSION_CODE=1;
public static final String VERSION_NAME="1.0.0";
}
Run Code Online (Sandbox Code Playgroud)
我有上面的课。我可以使用这个类build.gradle来分配所有字段吗(注意字段名称)。
我知道我可以BuildConfigField从properties我现在正在做的文件中创建。但出于好奇,有没有办法在 .java 类或接口中使用build.gradle?我可以实现类似的东西吗?
defaultConfig {
applicationId Config.APPLICATION_ID
minSdkVersion 16
targetSdkVersion 26
versionCode Config.VERSION_CODE
versionName Config.VERSION_NAME
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Run Code Online (Sandbox Code Playgroud)
不要被上面的代码所迷惑,这只是我想要实现的隐喻。
我在我的gradle属性上有我的facebook应用程序ID:
FACEBOOK_APPLICATION_ID="XXXXXXXXXXXXXXX"
Run Code Online (Sandbox Code Playgroud)
我在defaultConfig中设置:
manifestPlaceholders = [facebookAppId: FACEBOOK_APPLICATION_ID]
Run Code Online (Sandbox Code Playgroud)
然后,我想在我的清单中使用它:
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="${facebookAppId}" />
Run Code Online (Sandbox Code Playgroud)
但它不起作用.唯一可行的方法是在字符串中设置我的faceboook id:
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
Run Code Online (Sandbox Code Playgroud)
或者直接用这个:
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="\ XXXXXXXXXXXXXX" />
Run Code Online (Sandbox Code Playgroud)
但我必须在我的gradle属性中设置我的facebook id.这样做的正确方法是什么?
谢谢.
我正在尝试使用 build.gradle 将 url 架构和路径动态注入到我的 AndroidManifest.xml 文件中作为参考
但是,这不允许触发深度链接。
当我在 AndroidManifest.xml 中使用静态值时,我测试了深度链接的工作情况。
// AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="${appScheme}"
android:host="${hostName}"
android:path="/path2" />
<data
android:scheme="${appScheme}"
android:host="${hostName}"
android:path="/path1" />
</intent-filter>
// build.gradle (:app)
defaulConfig {
manifestPlaceholders = [
hostName: "www.host_name.com",
appScheme: "https"
]
}
demo {
resValue "string", "hostName", "www.host_demo.com"
resValue "string", "appScheme", "https"
}
staging {
resValue "string", "hostName", "www.host_staging.com"
resValue "string", "appScheme", "http"
}
Run Code Online (Sandbox Code Playgroud) 在 strings.xml 中
<resources>
<string>name="app_name">App_Name</string>
<string>name="app_port">https://example.com</string>
<string>name="app_key">App_Key</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
在 build.gradle 中
productFlavors {
production {
applicationId = "com.example.production"
}
staging {
applicationId = "com.example.staging"
//code to manipulate strings.xml value because for production and staging app_name, app_port and key are different
}
}
Run Code Online (Sandbox Code Playgroud)
我需要一个代码来操作 build.gradle 文件的 ProductFlavours 中的 strings.xml,而不是为暂存和生产创建单独的文件夹。
android ×6
java ×3
build.gradle ×2
gradle ×2
deep-linking ×1
facebook ×1
filtering ×1
regex ×1
xml ×1