Skip to content

Commit d96eb08

Browse files
authored
Android library load fix (#706)
1 parent e30c320 commit d96eb08

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

android/crt/src/main/java/software/amazon/awssdk/crt/android/CrtPlatformImpl.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,47 @@
99
import software.amazon.awssdk.crt.BuildConfig;
1010
import software.amazon.awssdk.crt.CrtPlatform;
1111
import software.amazon.awssdk.crt.utils.PackageInfo;
12+
import java.util.Locale;
1213

1314
public class CrtPlatformImpl extends CrtPlatform {
1415
public String getOSIdentifier() {
1516
return "android";
1617
}
1718

19+
public String getArchIdentifier() {
20+
return System.getProperty("os.arch");
21+
}
22+
23+
private String normalize(String value) {
24+
if (value == null) {
25+
return "";
26+
}
27+
return value.toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", "");
28+
}
29+
30+
public String getResourcePath(String cRuntime, String libraryName) {
31+
// Internal folder structure of Android aar libraries are different from jar libraries
32+
String arch = normalize(System.getProperty("os.arch"));
33+
34+
if (arch.matches("^(x8664|amd64|ia32e|em64t|x64|x86_64)$")) {
35+
arch = "x86_64";
36+
} else if (arch.matches("^(x8632|x86|i[3-6]86|ia32|x32)$")) {
37+
arch = "x86";
38+
} else if (arch.startsWith("armeabi")) {
39+
if (arch.contains("v7")) {
40+
arch = "armeabi-v7a";
41+
} else {
42+
throw new RuntimeException("AWS CRT: architecture not supported on Android: " + arch);
43+
}
44+
} else if (arch.startsWith("arm64") || arch.startsWith("aarch64")) {
45+
arch = "arm64-v8a";
46+
} else {
47+
throw new RuntimeException("AWS CRT: architecture not supported on Android: " + arch);
48+
}
49+
50+
return "/lib/" + arch + "/" + libraryName;
51+
}
52+
1853
public PackageInfo.Version getVersion() {
1954
return new PackageInfo.Version(BuildConfig.VERSION_NAME);
2055
}

src/main/java/software/amazon/awssdk/crt/CRT.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,15 @@ private static void extractAndLoadLibrary(String path) {
308308

309309
// open a stream to read the shared lib contents from this JAR
310310
String libResourcePath = "/" + os + "/" + getArchIdentifier() + "/" + getCRuntime(os) + "/" + libraryName;
311+
// Check whether there is a platform specific resource path to use
312+
CrtPlatform platform = getPlatformImpl();
313+
if (platform != null){
314+
String platformLibResourcePath = platform.getResourcePath(getCRuntime(os), libraryName);
315+
if (platformLibResourcePath != null){
316+
libResourcePath = platformLibResourcePath;
317+
}
318+
}
319+
311320
try (InputStream in = CRT.class.getResourceAsStream(libResourcePath)) {
312321
if (in == null) {
313322
throw new IOException("Unable to open library in jar for AWS CRT: " + libResourcePath);
@@ -406,6 +415,7 @@ private static CrtPlatform findPlatformImpl() {
406415
String.format("software.amazon.awssdk.crt.test.%s.CrtPlatformImpl", getOSIdentifier()),
407416
// Search for android test impl specifically because getOSIdentifier will return "linux" on android
408417
"software.amazon.awssdk.crt.test.android.CrtPlatformImpl",
418+
"software.amazon.awssdk.crt.android.CrtPlatformImpl",
409419
// Fall back to crt
410420
String.format("software.amazon.awssdk.crt.%s.CrtPlatformImpl", getOSIdentifier()), };
411421
for (String platformImpl : platforms) {

src/main/java/software/amazon/awssdk/crt/CrtPlatform.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public String getArchIdentifier() {
2929
return System.getProperty("os.arch");
3030
}
3131

32+
// Get the library resource path to load the JNI library
33+
public String getResourcePath(String cRuntime, String libraryName) throws RuntimeException {
34+
return null;
35+
}
36+
3237
// Called one and only one time during setup for testing
3338
public void setupOnce() {}
3439

0 commit comments

Comments
 (0)