å½ä½ åå¸ä¸ä¸ªåºç¨ä¹åï¼(åå³äºå
·ä½çåå¸æ¶é´)å¯è½æ²¡è¿å 个æ Android ç³»ç»å°±åå¸äºä¸ä¸ªæ°çæ¬ãè¿å¯¹ä½ çåºç¨æå³çä»ä¹ï¼ææä¸è¥¿é½ä¸è½ç¨äºï¼
å«æ
å¿ï¼ååå
¼å®¹æ¯ Android é常å
³æ³¨çäºæ
ãç¨æ·å¨å级å°æ°ç Android çæ¶åï¼ç¨ä»¥åçæ¬ç SDK æ建çç°æåºç¨åºè¯¥ä¸ä¼åºé®é¢ãè¿å°±æ¯ compileSdkVersion, minSdkVersion å targetSdkVersion çä½ç¨ï¼ä»ä»¬åå«æ§å¶å¯ä»¥ä½¿ç¨åªäº API ï¼è¦æ±ç API 级å«æ¯ä»ä¹ï¼ä»¥ååºç¨çå
¼å®¹æ¨¡å¼ã
compileSdkVersion
compileSdkVersion åè¯ Gradle ç¨åªä¸ª Android SDK çæ¬ç¼è¯ä½ çåºç¨ã使ç¨ä»»ä½æ°æ·»å ç API å°±éè¦ä½¿ç¨å¯¹åº Level ç Android SDKã
éè¦å¼ºè°çæ¯ä¿®æ¹ compileSdkVersion ä¸ä¼æ¹åè¿è¡æ¶çè¡ä¸ºãå½ä½ ä¿®æ¹äº compileSdkVersion çæ¶åï¼å¯è½ä¼åºç°æ°çç¼è¯è¦åãç¼è¯é误ï¼ä½æ°ç compileSdkVersion ä¸ä¼è¢«å
å«å° APK ä¸ï¼å®çº¯ç²¹åªæ¯å¨ç¼è¯çæ¶å使ç¨ãï¼ä½ ççåºè¯¥ä¿®å¤è¿äºè¦åï¼ä»ä»¬çåºç°ä¸å®æ¯æåå çï¼
å æ¤æ们强çæ¨èæ»æ¯ä½¿ç¨ææ°ç SDK è¿è¡ç¼è¯ãå¨ç°æ代ç ä¸ä½¿ç¨æ°çç¼è¯æ£æ¥å¯ä»¥è·å¾å¾å¤å¥½å¤ï¼é¿å
æ°å¼ç¨ç API ï¼å¹¶ä¸ä¸ºä½¿ç¨æ°ç API å好åå¤ã
注æï¼å¦æä½¿ç¨ Support Library ï¼
é£ä¹ä½¿ç¨ææ°åå¸ç Support Library å°±éè¦ä½¿ç¨ææ°ç SDK ç¼è¯ãä¾å¦ï¼è¦ä½¿ç¨ 23.1.1 çæ¬ç Support
Library ï¼compileSdkVersion å°±å¿
éè³å°æ¯ 23 ï¼å¤§çæ¬å·è¦ä¸è´ï¼ï¼ãé常ï¼æ°çç Support Library
éçæ°çç³»ç»çæ¬èåå¸ï¼å®ä¸ºç³»ç»æ°å¢å ç API åæ°ç¹æ§æä¾å
¼å®¹æ§æ¯æã
minSdkVersion
å¦æ compileSdkVersion 设置为å¯ç¨çææ° APIï¼é£ä¹ minSdkVersion åæ¯åºç¨å¯ä»¥è¿è¡çæä½è¦æ±ãminSdkVersion æ¯ Google Play ååºç¨æ¥å¤æç¨æ·è®¾å¤æ¯å¦å¯ä»¥å®è£
æ个åºç¨çæ å¿ä¹ä¸ã
å¨å¼åæ¶ minSdkVersion ä¹èµ·å°ä¸ä¸ªéè¦è§è²ï¼lint é»è®¤ä¼å¨é¡¹ç®ä¸è¿è¡ï¼å®å¨ä½ 使ç¨äºé«äº minSdkVersion ç API æ¶ä¼è¦åä½ ï¼å¸®ä½ é¿å
è°ç¨ä¸åå¨ç API çè¿è¡æ¶é®é¢ãå¦æåªå¨è¾é«çæ¬çç³»ç»ä¸æ使ç¨æäº APIï¼é常使ç¨è¿è¡æ¶æ£æ¥ç³»ç»çæ¬çæ¹å¼è§£å³ã
请记ä½ï¼ä½ æ使ç¨çåºï¼å¦ Support Library æ Google Play servicesï¼
å¯è½æä»ä»¬èªå·±ç minSdkVersion ãä½ çåºç¨è®¾ç½®ç minSdkVersion å¿
é大äºçäºè¿äºåºç minSdkVersion
ãä¾å¦æä¸ä¸ªåºï¼å®ä»¬ç minSdkVersion åå«æ¯ 4, 7 å 9 ï¼é£ä¹ä½ ç minSdkVersion å¿
éè³å°æ¯ 9
æè½ä½¿ç¨å®ä»¬ãå¨å°æ°æ
åµä¸ï¼ä½ ä»ç¶æ³ç¨ä¸ä¸ªæ¯ä½ åºç¨ç minSdkVersion
è¿é«çåºï¼å¤çææçè¾¹ç¼æ
åµï¼ç¡®ä¿å®åªå¨è¾æ°çå¹³å°ä¸ä½¿ç¨ï¼ï¼ä½ å¯ä»¥ä½¿ç¨ tools:overrideLibrary æ è®°ï¼ä½è¯·åå½»åºçæµè¯ï¼
å½ä½ å³å®ä½¿ç¨ä»ä¹ minSdkVersion æ¶åï¼ä½ åºè¯¥åèå½åç Android åå¸ç»è®¡ï¼å®æ¾ç¤ºäºæè¿ 7 天ææè®¿é® Google Play ç设å¤ä¿¡æ¯ãä»ä»¬å°±æ¯ä½ æåºç¨åå¸å° Google Play æ¶çæ½å¨ç¨æ·ãæç»è¿æ¯ä¸ä¸ªåä¸å³çé®é¢ï¼åå³äºä¸ºäºæ¯æé¢å¤ 3% ç设å¤ï¼ç¡®ä¿æä½³ä½éªèä»åºçå¼ååæµè¯ææ¬æ¯å¦å¼å¾ã
å½ç¶ï¼å¦ææ个æ°ç API æ¯ä½ æ´ä¸ªåºç¨çå
³é®ï¼é£ä¹ç¡®å® minSdkVersion çå¼å°±æ¯è¾å®¹æäºãä¸è¿è¦è®°å¾ 14 亿设å¤ä¸ç 0.7ï¼
ä¹æ¯ä¸ªä¸å°çæ°åã
targetSdkVersion
ä¸ä¸ªçæ¬å·ä¸ææ趣çå°±æ¯ targetSdkVersion äºã targetSdkVersion æ¯ Android æä¾ååå
¼å®¹ç主è¦ä¾æ®ï¼å¨åºç¨ç targetSdkVersion 没ææ´æ°ä¹åç³»ç»ä¸ä¼åºç¨ææ°çè¡ä¸ºååãè¿å
è®¸ä½ å¨éåºæ°çè¡ä¸ºååä¹åå°±å¯ä»¥ä½¿ç¨æ°ç API ï¼å ä¸ºä½ å·²ç»æ´æ°äº compileSdkVersion ä¸æ¯åï¼ï¼ã
targetSdkVersion ææ示ç许å¤è¡ä¸ºååé½è®°å½å¨ VERSION_CODES ææ¡£ä¸äºï¼ä½æ¯ææææçç»èä¹é½åå¨æ¯æ¬¡åå¸çå¹³å°äº®ç¹ä¸äºï¼å¨è¿ä¸ª API Level 表ä¸å¯ä»¥æ¹ä¾¿å°æ¾å°ç¸åºçé¾æ¥ã
ä¾å¦ï¼Android 6.0 ååææ¡£ä¸è°äº target 为 API 23 æ¶ä¼å¦ä½æä½ çåºç¨è½¬æ¢å°è¿è¡æ¶æé模åä¸ï¼Android 4.4 è¡ä¸ºååéè¿°äº target 为 API 19 å以ä¸æ¶ä½¿ç¨ set() å setRepeating() 设置 alarm ä¼æææ ·çè¡ä¸ºååã
ç±äºæäºè¡ä¸ºçåå对ç¨æ·æ¯é常ææ¾çï¼å¼ç¨ç menu æé®ï¼è¿è¡æ¶æéçï¼ï¼æä»¥å° target æ´æ°ä¸ºææ°ç SDK æ¯ææåºç¨é½åºè¯¥ä¼å
å¤ççäºæ
ãä½è¿ä¸æå³çä½ ä¸å®è¦ä½¿ç¨æææ°å¼å
¥çåè½ï¼ä¹ä¸æå³çä½ å¯ä»¥ä¸åä»»ä½æµè¯å°±ç²ç®å°æ´æ° targetSdkVersion ï¼è¯·ä¸å®å¨æ´æ° targetSdkVersion ä¹ååæµè¯ï¼ä½ çç¨æ·ä¼æè°¢ä½ çã
Gradle å SDK çæ¬
æ以设置æ£ç¡®ç compileSdkVersion, minSdkVersion å targetSdkVersion å¾éè¦ãå¦ä½ ææ³ï¼ Gradle å Android Studio é½å¨æ建系ç»ä¸éæäºå®ä»¬ãå¨ä½ ç模åç build.gradle æ件ä¸ï¼ä¹å¯ä»¥å¨ Android Studio ç项ç®ç»æé项ä¸ï¼è®¾ç½®ï¼
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.checkyourtargetsdk"
minSdkVersion 7
targetSdkVersion 23
versionCode 1
versionName â1.0â
}
}
ç¼è¯æ¶ç¨å°ç compileSdkVersion æ¯åæ建工å
·çæ¬ä¸èµ·è®¾ç½®ç Android 设置ä¹ä¸ãå
¶ä»ä¸¤ä¸ªç¨æä¸åï¼ä»ä»¬å¨æ建åä½(build variant)çé£é声æãdefaultConfig æ¯æææ建åä½çåºç¡ï¼ä¹æ¯è®¾ç½®è¿äºé»è®¤å¼çå°æ¹ãä½ å¯ä»¥æ³è±¡å¨ä¸ä¸ªæ´å¤æçç³»ç»ä¸ï¼åºç¨çæäºçæ¬å¯è½ä¼æä¸åç minSdkVersion ã
minSdkVersion å targetSdkVersion ä¸ compileSdkVersion çå¦ä¸ä¸ªä¸åä¹å¤æ¯å®ä»¬ä¼è¢«å
å«è¿æç»ç APK æ件ä¸ï¼å¦æä½ æ¥ççæç AndroidManifest.xml æ件ï¼ä½ ä¼çå°ç±»ä¼¼ä¸é¢è¿æ ·çæ ç¾ï¼
<uses-sdk android:targetSdkVersion="23" android:minSdkVersion="7" />
å¦æä½ å¨ manifest æ件ä¸æ工设置ï¼ä½ ä¼åç° Gradle å¨æ建æ¶ä¼å¿½ç¥å®ä»¬ï¼å°½ç®¡å
¶å®æ建系ç»å¯è½ä¼æç¡®ä¾èµå®ä»¬ï¼ã
综åæ¥ç
å¦æä½ æç
§ä¸é¢ç¤ºä¾é£æ ·é
ç½®ï¼ä½ ä¼åç°è¿ä¸ä¸ªå¼çå
³ç³»æ¯ï¼
minSdkVersion <= targetSdkVersion <= compileSdkVersion
è¿ç§ç´è§æ¯åççï¼å¦æ compileSdkVersion æ¯ä½ çæ大å¼ï¼minSdkVersion æ¯æå°å¼ï¼é£ä¹æ大å¼å¿
éè³å°åæå°å¼ä¸æ ·å¤§ä¸ target å¿
éå¨äºè
ä¹é´ã
çæ³ä¸ï¼å¨ç¨³å®ç¶æä¸ä¸è
çå
³ç³»åºè¯¥æ´åè¿æ ·ï¼
minSdkVersion (lowest possible) <=
targetSdkVersion == compileSdkVersion (latest SDK)
ç¨è¾ä½ç minSdkVersion æ¥è¦çæ大ç人群ï¼ç¨ææ°ç SDK 设置 target å compile æ¥è·å¾æ好çå¤è§åè¡ä¸ºã#BuildBetterApps
å
³äºæ¬æçå
容æ¨å¯ä»¥åä¸æ们 Google+ å¸åä¸ç讨论ï¼å
³æ³¨æ们ç Android Development Patterns ä¿¡æ¯æµè·å¾æ´å¤ä¿¡æ¯ã
温馨提示:内容为网友见解,仅供参考