@@ -2,30 +2,52 @@ package com.github.cnrture.quickprojectwizard.projectwizard.recipes
22
33import com.android.tools.idea.wizard.template.ModuleTemplateData
44
5+ /* *
6+ * Extension function to get minimum API level from ModuleTemplateData
7+ * Uses reflection to handle different API versions and changes
8+ */
59fun ModuleTemplateData.getMinApiLevel (): Int {
610 return try {
11+ // Try modern API first
712 val minApi = this .apis.minApi
13+
14+ // Try getApiLevel method
815 val apiLevelMethod = minApi.javaClass.getDeclaredMethod(" getApiLevel" )
16+ apiLevelMethod.isAccessible = true
917 apiLevelMethod.invoke(minApi) as Int
1018 } catch (_: Exception ) {
1119 try {
20+ // Fallback to getApi method
1221 val minApi = this .apis.minApi
1322 val apiMethod = minApi.javaClass.getDeclaredMethod(" getApi" )
23+ apiMethod.isAccessible = true
1424 apiMethod.invoke(minApi) as Int
1525 } catch (_: Exception ) {
1626 try {
27+ // Fallback to apiLevel field
1728 val minApi = this .apis.minApi
1829 val apiLevelField = minApi.javaClass.getDeclaredField(" apiLevel" )
1930 apiLevelField.isAccessible = true
2031 apiLevelField.get(minApi) as Int
2132 } catch (_: Exception ) {
2233 try {
34+ // Fallback to api field
2335 val minApi = this .apis.minApi
2436 val apiField = minApi.javaClass.getDeclaredField(" api" )
2537 apiField.isAccessible = true
2638 apiField.get(minApi) as Int
2739 } catch (_: Exception ) {
28- 23
40+ try {
41+ // Try toString parsing as last resort
42+ val minApi = this .apis.minApi
43+ val apiString = minApi.toString()
44+ val regex = Regex (" \\ d+" )
45+ val match = regex.find(apiString)
46+ match?.value?.toIntOrNull() ? : 23
47+ } catch (_: Exception ) {
48+ // Default to API 23 (Android 6.0) as safe fallback
49+ 23
50+ }
2951 }
3052 }
3153 }
0 commit comments