Task
想要知道一个项目中有哪些任务可以被使用,可以运行./gradlew tasks查看,该命令可以打印出所有可用的任务。
hhh:MyApplication huozhenpeng$ ./gradlew tasks
Android tasks
-------------
androidDependencies - Displays the Android dependencies of the project.
signingReport - Displays the signing info for each variant.
sourceSets - Prints out all the source sets defined in this project.
Build tasks
-----------
assemble - Assembles all variants of all applications and secondary packages.
assembleAndroidTest - Assembles all the Test applications.
assembleDebug - Assembles all Debug builds.
assembleRelease - Assembles all Release builds.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
clean - Deletes the build directory.
cleanBuildCache - Deletes the build cache directory.
compileDebugAndroidTestSources
compileDebugSources
compileDebugUnitTestSources
compileReleaseSources
compileReleaseUnitTestSources
mockableAndroidJar - Creates a version of android.jar that's suitable for unit tests.
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'MyApplication'.
components - Displays the components produced by root project 'MyApplication'. [incubating]
dependencies - Displays all dependencies declared in root project 'MyApplication'.
dependencyInsight - Displays the insight into a specific dependency in root project 'MyApplication'.
dependentComponents - Displays the dependent components of components in root project 'MyApplication'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'MyApplication'. [incubating]
projects - Displays the sub-projects of root project 'MyApplication'.
properties - Displays the properties of root project 'MyApplication'.
tasks - Displays the tasks runnable from root project 'MyApplication' (some of the displayed tasks may belong to subprojects).
Install tasks
-------------
installDebug - Installs the Debug build.
installDebugAndroidTest - Installs the android (on device) tests for the Debug build.
uninstallAll - Uninstall all applications.
uninstallDebug - Uninstalls the Debug build.
uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the Debug build.
uninstallRelease - Uninstalls the Release build.
Verification tasks
------------------
check - Runs all checks.
connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
connectedCheck - Runs all device checks on currently connected devices.
connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices.
deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers.
deviceCheck - Runs all device checks using Device Providers and Test Servers.
lint - Runs lint on all variants.
lintDebug - Runs lint on the Debug build.
lintRelease - Runs lint on the Release build.
lintVitalRelease - Runs lint on just the fatal issues in the release build.
test - Run unit tests for all variants.
testDebugUnitTest - Run unit tests for the debug build.
testReleaseUnitTest - Run unit tests for the release build.
在androidstudio开发环境的侧边栏有一个gradle视图
列出了所有的task,双击可以运行某个task
BuildConfig
从SDK工具版本升级到17之后,构建工具都会生成一个叫做BuildConfig的类,该类包含一个按照构建类型设置值的DEBUG常量,如果有一部分代码只想在debug版本上运行,比如日志,那么这个常量就会很有用。
我们可以通过Gradle来扩展该文件,这样在debug和release时期就可以拥有不同值的常量。
例如,我们可以定义一个url(通常debug版和release版的url是不同的)
现在我们自己定义一个变量API_URL
buildTypes {
release {
buildConfigField("String","API_URL","\"http://www.miduo.com\"")
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug{
buildConfigField("String","API_URL","\"http://192.168.3.169\"")
}
}
看下buildConfigField这个函数的声明
public void buildConfigField(String type, String name, String value) {
ClassField alreadyPresent = (ClassField)this.getBuildConfigFields().get(name);
if(alreadyPresent != null) {
String message = String.format("BuildType(%s): buildConfigField '%s' value is being replaced: %s -> %s", new Object[]{this.getName(), name, alreadyPresent.getValue(), value});
this.errorReporter.handleSyncWarning((String)null, 0, message);
}
this.addBuildConfigField(new ClassFieldImpl(type, name, value));
}
查看BuildConfig.java
/**
* Automatically generated file. DO NOT MODIFY
*/
package com.example.huozhenpeng.myapplication;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.example.huozhenpeng.myapplication";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Fields from build type: debug
public static final String API_URL = "http://192.168.3.169";
}
注意第三个参数的写法,如果写成这样就是错误的
buildConfigField("String","API_URL","http://192.168.3.169")
看下在BuildConfig中生成的变量
在BuildConfig中的变量,在java代码中都可以直接使用
package com.example.huozhenpeng.myapplication;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
Log.e("tag",BuildConfig.API_URL);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState, persistentState);
}
}
项目范围的设置
如果在你的项目中有多个Andorid module,那么你可以将每个module中通用的构建文件属性提取到顶层构建文件中
例如,在顶层的build.gradle中配置compileSdkVersion属性
allprojects {
repositories {
google()
jcenter()
}
android{
compileSdkVersion 26
}
}
这样在所有的module中都不再需要配置compileSdkVersion属性了,更好的办法是在顶层构建文件中定义值,然后将它们应用到模块中,我们可以通过ext代码块给build.gradle文件添加额外的属性
在顶层的build.gradle中
ext{
compileSdkVersion=26
}
在app这个module的build.gradle中
android {
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
applicationId "com.example.huozhenpeng.myapplication"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
buildConfigField("String","API_URL","\"http://www.miduo.com\"")
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug{
buildConfigField("String","API_URL","\"http://192.168.3.169\"")
}
}
}