ÕªÒª£ºGradleºÍMavenµÄ×÷ÓÃÏàËÆ£¬ÊǰïÖúÎÒÃǹ¹½¨Android¹¤³ÌµÄ¹¤¾ß£¬GradleÒ²ÊÇGoogleÍÆ¼öµÄ¹¤¾ß£¬±¾ÎÄÏêϸ·ÖÎöÁË Maven¡¢GradleµÄ¹¤×÷ÔÀí¡¢Ê¹Ó÷½·¨ÒÔ¼°AndroidÖÐÏîÄ¿¹¹½¨µÄ»ù´¡ÖªÊ¶¡£

1¡¢ÓÃGradle¹¹½¨
1.1 ¹¤³Ì½á¹¹

ÈçͼËùʾ£¬ÕâÊÇÒ»¸ö²»ÄܸüÆÕͨµÄAndroidµÄGradle¹¤³ÌÁË¡£
- ¸ùĿ¼ÏÂÃæµÄsettings.gradleµ±ÖÐÖ÷ÒªÊÇÓÃÀ´include×ÓÄ£¿éµÄ£¬±ÈÈçÎÒÃÇÕâ¸ö¹¤³ÌÓÐÒ»¸ö½Ð×÷appµÄ×ÓÄ£¿é£¬ÄÇôsettings.gradleµÄÄÚÈÝÈçÏ£º
- ¸ùĿ¼ÏÂÃæµÄbuild.gradle°üº¬Ò»Ð©Í¨ÓõÄÅäÖã¬ÕâЩÅäÖÿÉÒÔÔÚ¸÷¸ö×ÓÄ£¿éµ±ÖÐʹÓá£
- gradle.propertiesÎļþ°üº¬µÄÊôÐÔ£¬»á³ÉΪprojectµÄpropertiesµÄ³ÉÔ±,ÀýÈçÎÒÃÇÌí¼ÓÁËÊôÐÔhello£¬
- È»ºóÎÒÃÇÔÚbuild.gradleµ±Öд´½¨task£º
task hello << { println hello println project.getProperties().get("hello") } |
Êä³öµÄ½á¹ûÊÇÒ»ÑùµØ£º
14:28:11: Executing external task 'hello'... Configuration on demand is an incubating feature. :app:hello Hello Tas! Hello Tas! BUILD SUCCESSFUL Total time: 0.54 secs 14:28:12: External task execution finished 'hello'. |
- local.propertiesÕâ¸öÎļþÔÚAndroid¹¤³Ìµ±ÖлáÓöµ½£¬ÎÒÃÇͨ³£ÔÚÆäÖÐÉèÖÃAndroidµÄSDKºÍNDK·¾¶¡£µ±È»£¬Õâ¸öAndroid Studio»á°ïÎÒÃÇÉèÖúõġ£ÎªÁ˸üÇå³þµØÁ˽âÕâÒ»µã£¬ÎÒ°ÑAndroidµÄGradle²å¼þµÄ²¿·ÖÔ´Âëժ¼³öÀ´£º
SDK.groovy£¬ÏÂÃæµÄ´úÂëÖ÷Òª°üº¬Á˼ÓÔØSDK¡¢NDK·¾¶µÄ²Ù×÷¡£
private void findLocation() { if (TEST_SDK_DIR != null) { androidSdkDir = TEST_SDK_DIR return }
def rootDir = project.rootDir def localProperties = new File(rootDir, FN_LOCAL_PROPERTIES) if (localProperties.exists()) { Properties properties = new Properties() localProperties.withInputStream { instr -> properties.load(instr) } def sdkDirProp = properties.getProperty('sdk.dir')
if (sdkDirProp != null) { androidSdkDir = new File(sdkDirProp) } else { sdkDirProp = properties.getProperty('android.dir') if (sdkDirProp != null) { androidSdkDir = new File(rootDir, sdkDirProp) isPlatformSdk = true } else { throw new RuntimeException( "No sdk.dir property defined in local.properties file.") } }
def ndkDirProp = properties.getProperty('ndk.dir') if (ndkDirProp != null) { androidNdkDir = new File(ndkDirProp) }
} else { String envVar = System.getenv("ANDROID_HOME") if (envVar != null) { androidSdkDir = new File(envVar) } else { String property = System.getProperty("android.home") if (property != null) { androidSdkDir = new File(property) } }
envVar = System.getenv("ANDROID_NDK_HOME") if (envVar != null) { androidNdkDir = new File(envVar) } } } |
BasePlugin.groovy£¬Í¨¹ýÕâÁ½¸ö·½·¨£¬ÎÒÃÇ¿ÉÒÔÔÚGradle½Å±¾µ±ÖлñÈ¡SDKºÍNDKµÄ·¾¶£º
File getSdkDirectory() { return sdk.sdkDirectory }
File getNdkDirectory() { return sdk.ndkDirectory } |
ÀýÈ磺
task hello << { println android.getSdkDirectory() } |
14:37:33: Executing external task 'hello'... Configuration on demand is an incubating feature. :app:hello /Users/benny/Library/Android/sdk BUILD SUCCESSFUL Total time: 0.782 secs 14:37:35: External task execution finished 'hello'. |
ÉÏÃæ¸ø³öµÄÖ»ÊÇ×î³£¼ûµÄhierarchy½á¹¹£¬»¹ÓÐ flat ½á¹¹£¬ÈçÏÂͼ1Ϊflat½á¹¹£¬2Ϊhierarchy½á¹¹¡£ÓÐÐËȤµÄ»°¿ÉÒÔGoogleһϡ£

1.2 ¼¸¸öÖØÒªµÄ¸ÅÄî
ÕâһС½ÚµÄ³ö³¡Ë³Ðò»ù±¾Éϸúbuild.gradleµÄ˳ÐòÒ»Ö¡£
1.2.1 RepositoryºÍDependency
Èç¹ûÄãÖ»ÊÇдAndroid³ÌÐò£¬ÄÇôÒÀÀµÎÊÌâ¿ÉÄÜ»¹²»ÊÇÄÇôµÄ·³ÈË¡ª¡ªÈç¹ûÄãÓÃJavaд·þÎñ¶Ë³ÌÐò£¬ÄǿɾÍÊÇÒ»°ÑÐÁËáÒ»°ÑÀáÁË¡£
²Ö¿âµÄ³öÏÖ£¬ÍêÃÀµÄ½â¾öÁËÕâ¸öÎÊÌ⣬ÎÒÃÇÔÚ¿ª·¢Ê±Ö»ÐèÒªÖªµÀÒÀÀµµÄidºÍ°æ±¾£¬ÖÁÓÚËü´æ·ÅÔÚÄÄÀÎÒ²»¹ØÐÄ£»ËüÓÖÒÀÀµÁËÄÄЩ£¬¹¹½¨¹¤¾ß¶¼¿ÉÒÔÔÚ²Ö¿âÖаïÎÒÃÇÕÒµ½²¢¸ã¶¨¡£ÕâÒ»Çж¼ÊÇÄÇô×ÔÈ»£¬Òª²»ÒªÀ´Ò»±ÄÃÌú£¬ÈôúÂë¹¹½¨Ò»»á¶ù£¿
¾Ý˵ÔÚJava·¢Õ¹Ê·ÉÏ£¬Ó¿ÏÖ³ö·Ç³£¶àµÄ²Ö¿â£¬²»¹ý×îÖøÃûµÄµ±È»ÊÇMavenÁË¡£Mavenͨ¹ýgroupIdºÍartifactIdÀ´Ëø¶¨¹¹¼þ£¬ÔÙÅäÖúð汾£¬ÄÇôMaven²Ö¿â¾Í¿ÉÒÔ×îÖÕËø¶¨Ò»¸öÈ·¶¨°æ±¾µÄ¹¹¼þ¹©ÄãʹÓÃÁË¡£±ÈÈçÎÒÃÇ¿ªÍ·ÄǸöÀý×Ó£º
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.4</version> </dependency> |
Maven¾ÍƾÕâô¼¸¾äÅäÖþͿÉÒÔ°ïÄã¸ã¶¨gson-2.4.jar£¬²»½öÈç´Ë£¬Ëü»¹»á°´ÕÕÄãµÄÉèÖðïÄã°ÑjavadocºÍsource¸ã¶¨¡£ÂèÂèÔÙÒ²²»Óõ£ÐÄÎÒ¿´²»µ½¹¹¼þµÄÔ´ÂëÁË¡£
ÄÇôÕâ¸öÉñÆæµÄMaven²Ö¿âÔÚÄĶùÄØ£¿Maven Central£¬ÖÐÑë²Ö¿â£¬ÊÇMaven²Ö¿âµÄ±Ç׿£¬ÆäËûµÄ´ó¶àÊý²Ö¿â¶¼»á¶ÔËü½øÐдúÀí£¬Í¬Ê±¸ù¾ÝÐèÇóÌí¼Ó×Ô¼ºµÄÌØÉ«¿â·¿¡£¼òµ¥Ëµ¼¸¸ö¸ÅÄ
- ´úÀí²Ö¿â£ºÒª×â·¿£¬È¥ËÑ·¿Íø°¡¡£ÄãҪȥ¼ÝУ±¨Ãû£¬ÎÒÊǼÝУ´úÀí£¬ÄãÕÒÎÒ£¬ÎÒÈ¥ÕÒ¼ÝУ¡£¾ßÌåµ½ÕâÀ»¹Óеã¶ù²»Ò»Ñù£¬Ò»µ©ÓÐÈË´Ó´úÀí²Ö¿âÏÂÔØ¹ýÒ»´ÎÌØ¶¨µÃ¹¹¼þ£¬ÄÇôÕâ¸ö¹¹¼þ»á±»´úÀí²Ö¿â»º´æÆðÀ´£¬ÒÔºó¾Í²»ÐèÒªÕÒ±»´úÀíµÄ²Ö¿âÏÂÔØÁË¡£
- ˽Óвֿ⣺ÖйúÌØÉ«Éç»áÖ÷Òå¡£×ß×Ô¼ºµÄ·£¬Äã¹ÜÎÒ°¡£¿¹«Ë¾ÄÚ²¿µÄ²Ö¿âÀïÃæÓм¸¸öhostedµÄ²Ö¿â£¬ÕâЩ²Ö¿â¾ÍÊÇÎÒÃǹ«Ë¾ÄÚ²¿ÌØÓеģ¬ÀïÃæµÄ¹¹¼þÒ²ÊÇÎÒÃÇ×Ô¼ºÄÚ²¿µÄͬÊÂÉÏ´«ÒÔºó¹©ÍŶӿª·¢Ê¹Óõġ£
- ±¾µØ²Ö¿â£º´óÒþÒþÓÚÊС£¸ú´úÀí²Ö¿âµÄµÀÀíºÜÏñ£¬Ö»²»¹ý£¬Õâ¸ö²Ö¿âÊÇ´æ·ÅÔÚÄã×Ô¼ºµÄÓ²ÅÌÉϵġ£
˵ÆðÀ´£¬Android SDKÏÂÃæÓиöextraĿ¼£¬ÀïÃæµÄºÜ¶àÒÀÀµÒ²ÊÇÒÔMaven²Ö¿âµÄÐÎʽ×éÖ¯µÄ¡£²»¹ýÕâÊÇGoogleÌØÉ«ÂÈ˼ÒÅ£µ½²»ÍùMavenµÄÖÐÑë²Ö¿âÉÏ´«£¬ÕæÊÇûÕÞ¡£
1.2.2 SourceSets
Ô´Â뼯£¬ÕâÀïÃæÖ÷Òª°üº¬ÄãµÄ¸÷ÖÖÀàÐ͵ĴúÂëµÄ·¾¶£¬±ÈÈç'src/main/java'µÈµÈ¡£
1.2.3 Properties
Ç°ÃæÎÒÃÇÆäʵҲÉÔÉÔÓÐÌáµ½£¬Õâ¸öpropertiesÆäʵÊÇgradleµÄÊôÐÔ£¬ÔÚgradleÔ´Âëµ±ÖУ¬ÎÒÃÇÕÒµ½Project.javaÕâ¸ö½Ó¿Ú£¬¿ÉÒÔ¿´µ½£º
/** * <p>Determines if this project has the given property. See <a href="#properties">here</a> for details of the * properties which are available for a project.</p> * * @param propertyName The name of the property to locate. * @return True if this project has the given property, false otherwise. */ boolean hasProperty(String propertyName); /** * <p>Returns the properties of this project. See <a href="#properties">here</a> for details of the properties which * are available for a project.</p> * * @return A map from property name to value. */ Map<String, ?> getProperties(); /** * <p>Returns the value of the given property. This method locates a property as follows:</p> * * <ol> * * <li>If this project object has a property with the given name, return the value of the property.</li> * * <li>If this project has an extension with the given name, return the extension.</li> * * <li>If this project's convention object has a property with the given name, return the value of the * property.</li> * * <li>If this project has an extra property with the given name, return the value of the property.</li> * * <li>If this project has a task with the given name, return the task.</li> * * <li>Search up through this project's ancestor projects for a convention property or extra property with the * given name.</li> * * <li>If not found, a {@link MissingPropertyException} is thrown.</li> * * </ol> * * @param propertyName The name of the property. * @return The value of the property, possibly null. * @throws MissingPropertyException When the given property is unknown. */ Object property(String propertyName) throws MissingPropertyException; /** * <p>Sets a property of this project. This method searches for a property with the given name in the following * locations, and sets the property on the first location where it finds the property.</p> * * <ol> * * <li>The project object itself. For example, the <code>rootDir</code> project property.</li> * * <li>The project's {@link Convention} object. For example, the <code>srcRootName</code> java plugin * property.</li> * * <li>The project's extra properties.</li> * * </ol> * * If the property is not found, a {@link groovy.lang.MissingPropertyException} is thrown. * * @param name The name of the property * @param value The value of the property */ void setProperty(String name, Object value) throws MissingPropertyException; |
²»ÄÑÖªµÀ£¬propertiesÆäʵ¾ÍÊÇÒ»¸ömap£¬ÎÒÃÇ¿ÉÒÔÔÚgradle.propertiesµ±Öж¨ÒåÊôÐÔ£¬Ò²¿ÉÒÔͨ¹ý gradle ½Å±¾À´¶¨Ò壺
setProperty('hello', 'Hello Tas again!') |
ʹÓ÷½·¨ÎÒÃÇÇ°ÃæÒѾÌáµ½£¬ÕâÀï¾Í²»¶à˵ÁË¡£
1.2.4 ProjectºÍTask
Èç¹ûÄãÓùýant£¬ÄÇôproject»ù±¾ÉÏÀàËÆÓÚantµÄproject±êÇ©£¬taskÔòÀàËÆÓÚantµÄtarget±êÇ©¡£ÎÒÃÇÔÚ build.gradleµ±ÖбàдµÄ£º
ʵ¼ÊÉÏ£¬Êǵ÷Óãº
Task Project.task(String name) throws InvalidUserDataException; |
´´½¨ÁËÒ»¸ötask£¬²¢Í¨¹ý << À´¶¨ÒåÕâ¸ötaskµÄÐÐΪ¡£ÎÒÃÇ¿´µ½task»¹ÓÐÈçϵÄÖØÔØ£º
Task task(String name, Closure configureClosure); |
ËùÒÔÏÂÃæµÄ¶¨ÒåÒ²ÊǺϷ¨µÄ£º
task('hello2',{ println hello }) |
¼òµ¥Ëµ£¬project¾ÍÊÇÕû¸ö¹¹½¨ÏîÄ¿µÄÒ»¸öÂ߼ʵÌ壬¶øtask¾ÍÊÇÕâ¸öÏîÄ¿µÄ¾ßÌåÈÎÎñµã¡£¸ü¶àµÄ½éÉÜ¿ÉÒԲμû¹ÙÍøµÄÎĵµ£¬ºÍgradleµÄÔ´Âë¡£
2¡¢·¢²¼¹¹¼þ
·¢²¼¹¹¼þ£¬»¹ÊÇÒÀÀµ²Ö¿â£¬ÎÒÃÇÈÔÈ»ÒÔMaven²Ö¿âΪÀý£¬Ë½Óвֿâ¶àÊý²ÉÓÃsonatype¡£
2.1 UI ·¢²¼
Èç¹û¹ÜÀíÔ±¸øÄ㿪ÁËÕâ¸öȨÏÞ£¬Äã»áÔÚUIÉÏÃæ¿´µ½upload artifactµÄtab£¬Ñ¡ÔñÄãÒªÉÏ´«µÄ¹¹¼þ£¬ÅäÖúöÔÓ¦µÄ²ÎÊý£¬µã»÷ÉÏ´«¼´¿É¡£

2.2 ʹÓà Maven ²å¼þ
ÕâÀïµÄÒâ˼ÊÇʹÓÃMavenµÄgradle²å¼þ£¬ÔÚ¹¹½¨µÄ¹ý³ÌÖÐÖ±½ÓÉÏ´«¡£¹¹½¨ºÃµÄ¹¹¼þÐèҪǩÃû£¬ÇëÏÂÔØGPG4WIN(windows)£¬»òÕßGPGTOOLS(mac)£¬Éú³É×Ô¼ºµÄkey¡£
Ö±½ÓÉÏ´úÂ룺
gradle.properties
sonatypeUsername=ÄãµÄÓû§Ãû sonatypePassword=ÄãµÄÃÜÂë signing.keyId=ÄãµÄkeyid signing.password=ÄãµÄkeypass #×¢Ò⣬ͨ³£À´½²ÊÇÕâ¸ö·¾¶¡£ # mac/linux signing.secretKeyRingFile=/Users/ÄãµÄÓû§Ãû/.gnupg/secring.gpg # Window XP and earlier (XP/2000/NT) # signing.secretKeyRingFile=C:\\Documents and Settings\\<username>\\Application Data\\GnuPG\\secring.gpg # Windows Vista and Windows 7 # signing.secretKeyRingFile=C:\\Users\\<username>\\AppData\\Roaming\\gnupg\\secring.gpg projectName=ÄãµÄ¹¹¼þÃû³Æ group=ÄãµÄ¹¹¼þgroupid artifactId=ÄãµÄ¹¹¼þartifactid # °æ±¾ºÅ£¬²ÉÓÃÈýλÊý×ÖµÄÐÎʽ£¬Èç¹ûÊÇ·ÇÎȶ¨°æ±¾£¬ÇëÎñ±ØÌí¼ÓSNAPSHOT version=0.0.1-SNAPSHOT |
build.gradle
apply plugin: 'com.android.library' apply plugin: 'maven' apply plugin: 'signing' android { compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig { minSdkVersion 17 targetSdkVersion 21 versionCode 1 versionName "0.2" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') ...... } def isSnapshot = version.endsWith('-SNAPSHOT') def sonatypeRepositoryUrl if(isSnapshot) { sonatypeRepositoryUrl = "http://maven.oa.com/nexus/content/repositories/thirdparty-snapshots/" } else { sonatypeRepositoryUrl = "http://maven.oa.com/nexus/content/repositories/thirdparty/" } sourceSets { main { java { srcDir 'src/main/java' } } } task sourcesJar(type: Jar) { from sourceSets.main.allSource classifier = 'sources' } artifacts { //archives javadocJar archives sourcesJar } signing { if(project.hasProperty('signing.keyId') && project.hasProperty('signing.password') && project.hasProperty('signing.secretKeyRingFile')) { sign configurations.archives } else { println "Signing information missing/incomplete for ${project.name}" } } uploadArchives { repositories { mavenDeployer { if(project.hasProperty('preferedRepo') && project.hasProperty('preferedUsername') && project.hasProperty('preferedPassword')) { configuration = configurations.archives repository(url: preferedRepo) { authentication(userName: preferedUsername, password: preferedPassword) } } else if(project.hasProperty('sonatypeUsername') && project.hasProperty('sonatypePassword')) { beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } repository(url: sonatypeRepositoryUrl) { authentication(userName: sonatypeUsername, password: sonatypePassword) } } else { println "Settings sonatypeUsername/sonatypePassword missing/incomplete for ${project.name}" } pom.artifactId = artifactId pom.project { name projectName packaging 'aar' developers { developer { id 'wecar' name 'wecar' } } } } } } |
È»ºóÔËÐÐgradle uploadArchives¾Í¿ÉÒÔ½«´ò°üµÄaar·¢²¼µ½¹«Ë¾µÄMaven²Ö¿âµ±ÖÐÁË¡£jar°üµÄ·½Ê½ÀàËÆ£¬ÕâÀï¾Í²»ÔÚÁгöÁË¡£
2.3 ʹÓÃMavenÃüÁî
Õâ¸ö¿ÉÒÔͨ¹ýmvnÔÚcmdlineÖ±½Ó·¢²¼¹¹¼þ£¬ÃüÁîʹÓÃ˵Ã÷£º
mvn deploy:deploy-file -Durl=file://C:\m2-repo \ -DrepositoryId=some.id \ -Dfile=your-artifact-1.0.jar \ [-DpomFile=your-pom.xml] \ [-DgroupId=org.some.group] \ [-DartifactId=your-artifact] \ [-Dversion=1.0] \ [-Dpackaging=jar] \ [-Dclassifier=test] \ [-DgeneratePom=true] \ [-DgeneratePom.description="My Project Description"] \ [-DrepositoryLayout=legacy] \ [-DuniqueVersion=false] |
µ±È»ÕâÀïÈÔÈ»ÓиöÈÏÖ¤µÄÎÊÌ⣬ÎÒÃÇÐèÒªÊ×ÏÈÔÚmavenµÄsettingsÅäÖõ±ÖмÓÈ룺
<servers> <server> <id>Maven.oa.com</id> <username>rdm</username> <password>rdm</password> </server> </servers> |
È»ºóÎÒÃǾͿÉÒÔʹÓÃÃüÁîÉÏ´«ÁË£º
mvn deploy:deploy-file -DgroupId=com.tencent.test -DartifactId =test -Dversion=1.0.0 -Dpackaging=aar -Dfile=test.aar -Durl =http://maven.oa.com/nexus/content/repositories/thirdparty -DrepositoryId=Maven.oa.com |
3¡¢²å¼þ
3.1 ʲôÊDzå¼þ
²å¼þÆäʵ¾ÍÊÇÓÃÀ´ÈÃÎÒÃÇ͵ÀÁµÄ¡£Èç¹ûûÓвå¼þ£¬ÎÒÃÇÏëÒª¹¹½¨Ò»¸ö Java ¹¤³Ì£¬¾ÍÒª×Ô¼º¶¨Òå sourceSets£¬×Ô¼º¶¨Òå classpath£¬×Ô¼º¶¨Òå¹¹½¨²½ÖèµÈµÈ¡£
¼òµ¥µØËµ£¬²å¼þÆäʵ¾ÍÊÇÒ»×éÅäÖúÍÈÎÎñµÄºÏ¼¯¡£
gradle ²å¼þµÄ´æÔÚÐÎʽÖ÷ÒªÓÉÈýÖÖ£¬
- gradleÎļþÖÐÖ±½Ó±àд£¬Äã¿ÉÒÔÔÚÄãµÄbuild.gradleµ±ÖÐдһ¸ö²å¼þÀ´Ö±½ÓÒýÈ룺
apply plugin: GreetingPlugin class GreetingPlugin implements Plugin<Project{ void apply(Project project) { project.task('hello') << { println "Hello from the GreetingPlugin" } } } |
- buildSrc¹¤³Ì£¬Õâ¸ö¾ÍÊÇÔÚÄãµÄ¹¤³Ì¸ùĿ¼ÏÂÃæÓÐÒ»¸ö±ê×¼µÄGroovy²å¼þ¹¤³Ì£¬Ä¿Â¼ÊÇbuildSrc£¬Äã¿ÉÒÔÖ±½ÓÒýÓÃÆäÖбàдµÄ²å¼þ¡£
- ¶ÀÁ¢µÄ¹¤³Ì£¬´Ó½á¹¹ÉϸúbuildSrc¹¤³ÌÊÇÒ»ÑùµÄ£¬Ö»²»¹ýÕâÖÖÐèҪͨ¹ý·¢²¼µ½²Ö¿âµÄÐÎʽÒýÓá£Í¨³£ÎÒÃǽӴ¥µÄ²å¼þ¶¼ÊÇÕâÖÖÐÎʽ¡£
3.2 ³£¼ûµÄ²å¼þ
Ŀǰ½Ó´¥µ½µÄ²å¼þ£¬ÓÐÏÂÃæÕâô¼¸ÖÖ£º
- java£¬¹¹½¨ java ¹¤³Ì
- war£¬·¢²¼ war °üÓ㬹¹½¨ web ¹¤³Ì»áÓõ½
- groovy£¬¹¹½¨ groovy ¹¤³Ì
- com.android.application£¬¹¹½¨ Android app ¹¤³Ì
- com.android.library£¬¹¹½¨ Android library£¬Í¨³£Êä³ö aar
- sign£¬Ç©Ãû
- maven£¬·¢²¼µ½ maven ²Ö¿â
- org.jetbrains.intellij£¬¹¹½¨ intellij ²å¼þ¹¤³Ì
3.3 ×Ô¼º¶¯ÊÖдһ¸ö²å¼þ
´´½¨Ò»¸öÆÕͨµÄ groovy ¹¤³Ì£¨java ¹¤³ÌҲûÓйØÏµ£©£¬´´½¨ src/main/groovy Ŀ¼£¬±àдÏÂÃæµÄ´úÂ룺
package com.tencent.wecar.plugin
import org.gradle.api.Plugin import org.gradle.api.internal.project.ProjectInternal class GreetingPlugin implements Plugin<ProjectInternal> { void apply(ProjectInternal project) { project.task('hello') << { println 'hello' } } }
|
ÔÚ src/main/resources´´½¨META-INF/gradle-pluginsĿ¼£¬´´½¨greetings.propertiesÎļþ£º
implementation-class=com.tencent.wecar.plugin.GreetingPlugin |
ÆäÖÐgreettings¾ÍÊÇÄãµÄ²å¼þid¡£
build.gradle
group 'com.tencent.wecar.plugin' version '1.1-SNAPSHOT'
buildscript { repositories { mavenLocal() } }
apply plugin: 'groovy' apply plugin: 'java'
repositories { mavenCentral() }
sourceSets { main { groovy { srcDirs = [ 'src/main/groovy', 'src/main/java' ] } // compile everything in src/ with groovy java { srcDirs = []}// no source dirs for the java compiler
} }
}
}
dependencies { //tasks.withType(Compile) { options.encoding = "UTF-8" } compile gradleApi() }
// custom tasks for creating source jars task sourcesJar(type: Jar, dependsOn:classes) { classifier = 'sources' from sourceSets.main.allSource }
// add source jar tasks as artifacts artifacts { archives sourcesJar }
// upload to local uploadArchives { repositories{ mavenLocal() } } |
ÔËÐÐuploadArchives·¢²¼µ½±¾µØ²Ö¿â£¬ÄÇô¾Í¿ÉÒÔÕÒµ½ÎÒÃÇ×Ô¼ºµÄ²å¼þÁË£¬ÓÉÓÚµ±ÖÐûÓÐÖ¸¶¨artifactId£¬ÄÇôÎÒÃǵIJå¼þµÄartifactId¾ÍÊÇÎÒÃǵŤ³ÌÃû³Æ£¬±ÈÈçÕâÀïÊÇdeployplugin¡£
ÄÇôÎÒÃÇÒªÔõôÒýÈëÕâ¸ö²å¼þÄØ£¿
Ê×ÏÈÒªÔÙbuildScriptÔö¼ÓÒÀÀµ£º
buildscript { repositories { mavenLocal() } dependencies { classpath 'com.tencent.wecar.plugin:deployplugin:1.1-SNAPSHOT' } } |
È»ºó£º
apply plugin: 'greetings' |
ÕâÑùÎÒÃǵÄtask ¡°hello¡±¾Í±»ÒýÈëÁË¡£
4¡¢GradleÔËÐÐÂý£¿
ÓùýGradleµÄÅóÓѶàÉÙ»á¸Ð¾õµ½Õâ»õÓÐʱºò»á±È½ÏÂý¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÏÂÃæµÄÈý¸öÊֶμÓËÙÄãµÄGradle¡£
- ²»ÓÃÖÐÑë²Ö¿â¡£Èç¹ûÄãµÄrepository ÅäÖõÄÊÇmavenCentral£¬·Å¿ªËü°É£¬È«ÊÀ½çµÄÈ˶¼ÔÚ×ÁÄ¥×ÅÔõôŰËü£¬Äã¾Í²»ÒªÏ¹²ôºÍÁË¡£ÊÔÊÔjCenter¡£
- Éý¼¶×îеÄGradle°æ±¾¡£Ä¿Ç°×îеİ汾ÊÇ2.4£¬Android Studio´Ó1.3¿ªÊ¼Ä¬ÈÏʹÓÃGradle2.4¡£
- ¿ªÆôGradleµÄµç¶¯Ð¡Âí´ï¡£ÔÚgradle.properties£¨ÑÛÊ죿û´í£¬¾ÍÊÇËü£¡£¡£©
ÀïÃæÌí¼ÓÏÂÃæµÄÅäÖ㺠Èç¹ûÄãµÄÈÎÎñûÓÐʱÐòÒªÇó£¬ÄÇô´ò¿ªÕâ¸öÑ¡Ïî¿ÉÒÔ²¢·¢´¦Àí¶à¸öÈÎÎñ£¬³ä·ÖÀûÓÃÓ²¼þ×ÊÔ´¡£¡£àÅ£¬Èç¹ûÄãµÄÊǵ¥ºËCPU¡£¡£µ±ÎÒû˵¡£¡£ org.gradle.parallel=true Õâ¸öÒ²¿ÉÒÔÔÚÃüÁîÐÐͨ¹ý²ÎÊýµÄÐÎʽÆô¶¯£¬3¸öСʱÓÐЧ¡£ÊØ»¤½ø³Ì¿ÉÒÔʹ±àÒëʱ¼ä´ó´óËõ¶Ì org.gradle.daemon=true Õâ¸ö¿´ÐèÇó°É£¬Gradle ÊÇÔËÐÐÔÚ Java ÐéÄâ»úÉϵģ¬Õâ¸öÖ¸¶¨ÁËÕâ¸öÐéÄâ»úµÄ¶ÑÄÚ´æ³õʼ»¯Îª256M£¬×î´óΪ1G¡£Èç¹ûÄãÄÚ´æÖ»ÓÐ2G£¬Äǵ±ÎÒû˵¡£¡£ org.gradle.jvmargs=-Xms256m -Xmx1024m
µ±È»£¬½¨ÒéµÄ·½Ê½ÊÇÔÚÄãµÄÓû§Ä¿Â¼ÏÂÃæµÄ.gradle/ÏÂÃæ´´½¨Ò»¸ögradle.properties£¬ÃâµÃ¿ÓÄãµÄ¶ÓÓÑ¡£¡£¡£
|