@@ -7,18 +7,77 @@ if (buildAsApplication) {
77 apply plugin : ' com.android.library'
88}
99
10- boolean existsABIDir (String abi ) {
10+ /**
11+ * Internal helper function
12+ */
13+ File getABIDir (String abi ) {
1114 String path = " ../../../../build-" + abi;
1215 File file = project. file(project. getLayout(). getBuildDirectory(). file(path));
13- return file. exists();
16+ return file;
17+ }
18+
19+ /**
20+ * Internal helper function
21+ */
22+ boolean existsABIDir (String abi ) {
23+ File file = getABIDir(abi);
24+ return file. exists() && file. isDirectory();
25+ }
26+
27+ /**
28+ * Internal helper function
29+ */
30+ static List<String > readInputStream (InputStream stream ) {
31+ InputStreamReader reader = new InputStreamReader (stream);
32+ return reader. readLines()
33+ }
34+
35+ /**
36+ * Internal helper function
37+ */
38+ static Tuple3<List<String > , List<String > , Integer > executePipeline (List<List<String > > commands ) {
39+ List<ProcessBuilder > builders = commands. collect { it -> new ProcessBuilder (it) };
40+
41+ List<Process > processes = ProcessBuilder . startPipeline(builders);
42+ Process last = processes. get(processes. size() - 1 );
43+
44+ int exitCode = last. waitFor();
45+
46+ List<String > stdout = readInputStream(last. getInputStream());
47+ List<String > stderr = readInputStream(last. getErrorStream());
48+
49+ return new Tuple3<List<String > , List<String > , Integer > (stdout, stderr, exitCode);
50+ }
51+
52+ /**
53+ * Internal helper function
54+ */
55+ static boolean isValidVersion (String version ) {
56+ if (version == null ) {
57+ return false ;
58+ }
59+
60+ List<String > parts = version. split(" \\ ." )
61+
62+ if (parts. size() != 3 ) {
63+ return false ;
64+ }
65+
66+ for (part in parts) {
67+ if (! part. isInteger()) {
68+ return false ;
69+ }
70+ }
71+
72+ return true ;
1473}
1574
1675/**
1776 * Read the Android ABI from user input.
1877 * supported ANDROID_ABIs are 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
19- * @return return
78+ * @return List<String>
2079 */
21- def List getAndroidABI () {
80+ List getAndroidABIs () {
2281 String property = project. findProperty(' ANDROID_ABI' )
2382
2483 List<String > supportedABIs = [" armeabi-v7a" , " arm64-v8a" , " x86" , " x86_64" ];
@@ -49,7 +108,7 @@ def List getAndroidABI() {
49108
50109 for (abi in AbiFilters ) {
51110 if (! existsABIDir(abi)) {
52- throw new Exception (' ERROR: build folder for abi \' ' + abi + ' \' doesn\ ' t exist: ' + file);
111+ throw new Exception (" ERROR: build folder for abi ' " + abi + " ' doesn't exist: " + file);
53112 }
54113 }
55114
@@ -60,8 +119,99 @@ def List getAndroidABI() {
60119 return AbiFilters
61120}
62121
122+
123+ /**
124+ * Determine the version
125+ * if you specify it explicitly, that will be used, otherwise meson introspect will be called
126+ * @return String
127+ */
128+ String getVersion () {
129+ String property = project. findProperty(' VERSION' )
130+
131+
132+ if (property != null ) {
133+ if (! isValidVersion(property)) {
134+ throw new Exception (' User defined version is invalid: ' + property);
135+ }
136+
137+ return property;
138+ }
139+
140+ // auto detect
141+
142+ List<String > abis = getAndroidABIs();
143+ List<String > versions = new ArrayList<String > ();
144+
145+ for (abi in abis) {
146+ File abiDir = getABIDir(abi);
147+
148+ Tuple3<List<String > , List<String > , Integer > versionResult = executePipeline(Arrays . asList(
149+ Arrays . asList(" meson" , " introspect" , " --projectinfo" , abiDir. getAbsolutePath()),
150+ Arrays . asList(" jq" , " -r" , " .version" )
151+ ));
152+
153+ if (versionResult. getV3() != 0 ) {
154+ throw new Exception (
155+ ' An error occured while trying to detect the version: process exited with exit code: ' +
156+ versionResult. getV3() +
157+ ' and stderr:\n ' +
158+ versionResult. getV2(). join(" \n " )
159+ );
160+ }
161+
162+ if (versionResult. getV2() != null && ! versionResult. getV2(). isEmpty()) {
163+ throw new Exception (' An error occured while trying to detect the version (code 1): ' + versionResult. getV2(). join(" \n " ));
164+ }
165+
166+ if (versionResult. getV1() == null || versionResult. getV1(). size() != 1 ) {
167+ throw new Exception (' An error occured while trying to detect the version (code 2): ' + versionResult. getV1(). join(" \n " ));
168+ }
169+
170+ String version = versionResult. getV1()[0 ];
171+
172+ if (! isValidVersion(version)) {
173+ throw new Exception (" Auto detection of version returned invalid version: '" + version + " '" );
174+ }
175+
176+ for (version1 in versions) {
177+ if (version != version1) {
178+ throw new Exception (" Recievd two versions, that didn't match: " + version1 + " != " + version);
179+ }
180+ }
181+
182+ versions. add(version);
183+
184+ }
185+
186+
187+ if (versions. size() == 0 ) {
188+ throw new Exception (" ERROR: at least one version has to be detected (at least one abi has to be returend by 'getAndroidABIs()')" );
189+ }
190+
191+ // we guarantee, that every string in there is the same, and that it has at least one entry
192+ return versions[0 ];
193+
194+ }
195+
196+ List<String > abisToUse = getAndroidABIs()
197+ String versionString = getVersion()
198+
199+ System . out. printf (" DEBUG: Using abis: %s%n" , abisToUse. join(" , " ))
200+ System . out. printf (" DEBUG: Using version: %s%n" , versionString)
201+
202+ java {
203+ toolchain {
204+ languageVersion = JavaLanguageVersion . of(21 )
205+ }
206+ }
207+
63208android {
64- compileSdk 34
209+ compileOptions {
210+ sourceCompatibility JavaVersion . VERSION_21
211+ targetCompatibility JavaVersion . VERSION_21
212+ }
213+
214+ compileSdkVersion 34
65215 ndkVersion " 26.3.11579264"
66216 defaultConfig {
67217 if (buildAsApplication) {
@@ -70,7 +220,7 @@ android {
70220 minSdkVersion 21
71221 targetSdkVersion 34
72222 versionCode 5
73- versionName " 0.5.4 "
223+ versionName(versionString)
74224 }
75225 buildTypes {
76226 release {
@@ -119,7 +269,7 @@ android {
119269 // Resets the list of ABIs for Gradle to create APKs for to none.
120270 reset()
121271 // Specifies a list of ABIs for Gradle to create APKs for.
122- include(* getAndroidABI() )
272+ include(* abisToUse )
123273 // Specifies that you don't want to also generate a universal APK that includes all ABIs.
124274 universalApk false
125275 }
0 commit comments