其他用法:
Gradle的基本操作:配置同一应用不同的ApplicationID,不同资源的名字、图标
Gradle的基本操作:AndroidManifest.xml中的meta-data标签、gradle中的manifestPlaceholder
一篇文章理解groovy中闭包closure的使用,不仅仅限于groovy
一 sourceSets的使用
在build.gradle文件中使用sourceSets
给资源文件分包。
在 ‘app’模块下的build.gradle的android闭包中添加一下配置块,然后创建相应的文件夹(脚本和新建文件夹不分先后),新建的资源文件夹的目录结构和默认的保持一致即可。
sourceSets {
main {
jniLibs.srcDirs = ['libs']//修改 so 库的位置
java {
srcDir 'src/main/java'
}
//配置资源文件夹下
res.srcDirs = [
'src/main/res',
'src/main/res-home',
'src/main/res-news',
'src/main/res-mine'
]
}
}
经过上述配置,就可以单独为 home , news , mime 功能版块分别添加各自的资源文件,但是要保证资源的名字是唯一的。之后的使用就跟之前没啥区别了。

基本上按上述的配置就可以使用,究其原因,可以去看一下源码,也方便以后灵活配置。
如果想知道,为什么是 main,是不是还可以有其他的配置。可以在上述配置块后加下面的配置,打印出他可以进行那些配置。
sourceSets.each {
source ->
println " ---------- "+source.name
}

可见 main就在其中,你品,你细品!
常用源集属性
属性名 | 类型 | 描述 |
---|---|---|
name | String | 只读属性,比如main |
java | SourceDirectorySet | Java 源文件的位置 |
java.srcDirs | Set | Java源文件所在的目录 |
resources | SourceDirectorySet | 资源文件 |
resources.srcDirs | Set | 资源文件所在目录 |
二 操作扩展-----分析一下Gradle源码
android 对应着源码中的 AppExtension ,sourceSets 其实就是位于 BaseExtension 类中的一个方法,AppExtension 又是 BaseExtension的子类,这也是为什么要在android代码块中配置了。
/**
* Encapsulates source set configurations for all variants.
*
* <p>Note that the Android plugin uses its own implementation of source sets. For more
* information about the properties you can configure in this block, see {@link
* AndroidSourceSet}.
*/
public void sourceSets(Action<NamedDomainObjectContainer<AndroidSourceSet>> action) {
checkWritability();
sourceSetManager.executeAction(action);
}
由注释可知,该方法配置了所有变提的源集信息,并且Android插件与它自己的实现。具体还要看 AndroidSourceSet。
AndroidSourceSet 源码
它是一个接口,
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.build.gradle.api;
import com.android.annotations.NonNull;
import groovy.lang.Closure;
/**
* An AndroidSourceSet represents a logical group of Java, aidl and RenderScript sources
* as well as Android and non-Android (Java-style) resources.
*/
public interface AndroidSourceSet {
/**
* Returns the name of this source set.
*
* @return The name. Never returns null.
*/
@NonNull
String getName();
/**
* Returns the Java resources which are to be copied into the javaResources output directory.
*
* @return the java resources. Never returns null.
*/
@NonNull
AndroidSourceDirectorySet getResources();
/**
* Configures the Java resources for this set.
*
* <p>The given closure is used to configure the {@link AndroidSourceDirectorySet} which
* contains the java resources.
*
* @param configureClosure The closure to use to configure the javaResources.
* @return this
*/
@NonNull
AndroidSourceSet resources(Closure configureClosure);
/**
* Returns the Java source which is to be compiled by the Java compiler into the class output
* directory.
*
* @return the Java source. Never returns null.
*/
@NonNull
AndroidSourceDirectorySet getJava();
/**
* Configures the Java source for this set.
*
* <p>The given closure is used to configure the {@link AndroidSourceDirectorySet} which
* contains the Java source.
*
* @param configureClosure The closure to use to configure the Java source.
* @return this
*/
@NonNull
AndroidSourceSet java(Closure configureClosure);
/**
* Returns the name of the compile configuration for this source set.
*
* @deprecated use {@link #getImplementationConfigurationName()}
*/
@NonNull
@Deprecated
String getCompileConfigurationName();
/**
* Returns the name of the runtime configuration for this source set.
*
* @deprecated use {@link #getRuntimeOnlyConfigurationName()}
*/
@NonNull
@Deprecated
String getPackageConfigurationName();
/**
* Returns the name of the compiled-only configuration for this source set.
*
* @deprecated use {@link #getCompileOnlyConfigurationName()}
*/
@NonNull
@Deprecated
String getProvidedConfigurationName();
/** Returns the name of the api configuration for this source set. */
@NonNull
String getApiConfigurationName();
/**
* Returns the name of the compileOnly configuration for this source set.
*/
@NonNull
String getCompileOnlyConfigurationName();
/**
* Returns the name of the implemenation configuration for this source set.
*/
@NonNull
String getImplementationConfigurationName();
/**
* Returns the name of the implemenation configuration for this source set.
*/
@NonNull
String getRuntimeOnlyConfigurationName();
/**
* Returns the name of the wearApp configuration for this source set.
*/
@NonNull
String getWearAppConfigurationName();
/**
* Returns the name of the annotation processing tool classpath for this source set.
*/
@NonNull
String getAnnotationProcessorConfigurationName();
/**
* The Android Manifest file for this source set.
*
* @return the manifest. Never returns null.
*/
@NonNull
AndroidSourceFile getManifest();
/**
* Configures the location of the Android Manifest for this set.
*
* <p>The given closure is used to configure the {@link AndroidSourceFile} which contains the
* manifest.
*
* @param configureClosure The closure to use to configure the Android Manifest.
* @return this
*/
@NonNull
AndroidSourceSet manifest(Closure configureClosure);
/**
* The Android Resources directory for this source set.
*
* @return the resources. Never returns null.
*/
@NonNull
AndroidSourceDirectorySet getRes();
/**
* Configures the location of the Android Resources for this set.
*
* <p>The given closure is used to configure the {@link AndroidSourceDirectorySet}
* which contains the resources.
*
* @param configureClosure The closure to use to configure the Resources.
* @return this
*/
@NonNull
AndroidSourceSet res(Closure configureClosure);
/**
* The Android Assets directory for this source set.
*
* @return the assets. Never returns null.
*/
@NonNull
AndroidSourceDirectorySet getAssets();
/**
* Configures the location of the Android Assets for this set.
*
* <p>The given closure is used to configure the {@link AndroidSourceDirectorySet}
* which contains the assets.
*
* @param configureClosure The closure to use to configure the Assets.
* @return this
*/
@NonNull
AndroidSourceSet assets(Closure configureClosure);
/**
* The Android AIDL source directory for this source set.
*
* @return the source. Never returns null.
*/
@NonNull
AndroidSourceDirectorySet getAidl();
/**
* Configures the location of the Android AIDL source for this set.
*
* <p>The given closure is used to configure the {@link AndroidSourceDirectorySet}
* which contains the AIDL source.
*
* @param configureClosure The closure to use to configure the AIDL source.
* @return this
*/
@NonNull
AndroidSourceSet aidl(Closure configureClosure);
/**
* The Android RenderScript source directory for this source set.
*
* @return the source. Never returns null.
*/
@NonNull
AndroidSourceDirectorySet getRenderscript();
/**
* Configures the location of the Android RenderScript source for this set.
*
* <p>The given closure is used to configure the {@link AndroidSourceDirectorySet}
* which contains the Renderscript source.
*
* @param configureClosure The closure to use to configure the Renderscript source.
* @return this
*/
@NonNull
AndroidSourceSet renderscript(Closure configureClosure);
/**
* The Android JNI source directory for this source set.
*
* @return the source. Never returns null.
*/
@NonNull
AndroidSourceDirectorySet getJni();
/**
* Configures the location of the Android JNI source for this set.
*
* <p>The given closure is used to configure the {@link AndroidSourceDirectorySet}
* which contains the JNI source.
*
* @param configureClosure The closure to use to configure the JNI source.
* @return this
*/
@NonNull
AndroidSourceSet jni(Closure configureClosure);
/**
* The Android JNI libs directory for this source set.
*
* @return the libs. Never returns null.
*/
@NonNull
AndroidSourceDirectorySet getJniLibs();
/**
* Configures the location of the Android JNI libs for this set.
*
* <p>The given closure is used to configure the {@link AndroidSourceDirectorySet}
* which contains the JNI libs.
*
* @param configureClosure The closure to use to configure the JNI libs.
* @return this
*/
@NonNull
AndroidSourceSet jniLibs(Closure configureClosure);
/**
* The Android shaders directory for this source set.
*
* @return the shaders. Never returns null.
*/
@NonNull
AndroidSourceDirectorySet getShaders();
/**
* Configures the location of the Android shaders for this set.
*
* <p>The given closure is used to configure the {@link AndroidSourceDirectorySet}
* which contains the shaders.
*
* @param configureClosure The closure to use to configure the shaders.
* @return this
*/
@NonNull
AndroidSourceSet shaders(Closure configureClosure);
/**
* Sets the root of the source sets to a given path.
*
* All entries of the source set are located under this root directory.
*
* @param path the root directory.
* @return this
*/
@NonNull
AndroidSourceSet setRoot(String path);
}
里面的方法名就是可以配置的选项。拿下面方法举例,比如为某一模块配置 AndroidManifest.xml :
/**
* Configures the location of the Android Manifest for this set.
*
* <p>The given closure is used to configure the {@link AndroidSourceFile} which contains the
* manifest.
*
* @param configureClosure The closure to use to configure the Android Manifest.
* @return this
*/
@NonNull
AndroidSourceSet manifest(Closure configureClosure);
该方法返回值是一个AndroidSourceFile,而且这个方法的注释有一句是这样的 The given closure is used to configure the {@link AndroidSourceFile} which contains the manifest. 这就告诉了我们 manifest 的配置参数,所以先去 AndroidSourceFile中看一下:
AndroidSourceFile.java
package com.android.build.gradle.api;
import java.io.File;
/**
* An AndroidSourceFile represents a single file input for an Android project.
*/
public interface AndroidSourceFile {
/**
* A concise name for the source directory (typically used to identify it in a collection).
*/
String getName();
/**
* Returns the file.
* @return the file input.
*/
File getSrcFile();
/**
* Sets the location of the file. Returns this object.
*
* @param srcPath The source directory. This is evaluated as for
* {@link org.gradle.api.Project#file(Object)}
* @return the AndroidSourceFile object
*/
AndroidSourceFile srcFile(Object srcPath);
}
这个类很简单,看最后一个方法,也就是路径。也就是说,manifest 可以调用 srcFile来设置AndroidManifest.xml的位置。
sourceSets {
main {
if (!isRelease) {
manifest.srcFile 'src/main/debug/AndroidManifest.xml'
} else {
manifest.srcFile 'src/main/AndroidManifest.xml'
}
}
}
其他属性的配置方法,亦如上述。
网友评论