Skip to content

Commit

Permalink
Android library load fix (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbSteveK authored Nov 6, 2023
1 parent e30c320 commit d96eb08
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,47 @@
import software.amazon.awssdk.crt.BuildConfig;
import software.amazon.awssdk.crt.CrtPlatform;
import software.amazon.awssdk.crt.utils.PackageInfo;
import java.util.Locale;

public class CrtPlatformImpl extends CrtPlatform {
public String getOSIdentifier() {
return "android";
}

public String getArchIdentifier() {
return System.getProperty("os.arch");
}

private String normalize(String value) {
if (value == null) {
return "";
}
return value.toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", "");
}

public String getResourcePath(String cRuntime, String libraryName) {
// Internal folder structure of Android aar libraries are different from jar libraries
String arch = normalize(System.getProperty("os.arch"));

if (arch.matches("^(x8664|amd64|ia32e|em64t|x64|x86_64)$")) {
arch = "x86_64";
} else if (arch.matches("^(x8632|x86|i[3-6]86|ia32|x32)$")) {
arch = "x86";
} else if (arch.startsWith("armeabi")) {
if (arch.contains("v7")) {
arch = "armeabi-v7a";
} else {
throw new RuntimeException("AWS CRT: architecture not supported on Android: " + arch);
}
} else if (arch.startsWith("arm64") || arch.startsWith("aarch64")) {
arch = "arm64-v8a";
} else {
throw new RuntimeException("AWS CRT: architecture not supported on Android: " + arch);
}

return "/lib/" + arch + "/" + libraryName;
}

public PackageInfo.Version getVersion() {
return new PackageInfo.Version(BuildConfig.VERSION_NAME);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/software/amazon/awssdk/crt/CRT.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@ private static void extractAndLoadLibrary(String path) {

// open a stream to read the shared lib contents from this JAR
String libResourcePath = "/" + os + "/" + getArchIdentifier() + "/" + getCRuntime(os) + "/" + libraryName;
// Check whether there is a platform specific resource path to use
CrtPlatform platform = getPlatformImpl();
if (platform != null){
String platformLibResourcePath = platform.getResourcePath(getCRuntime(os), libraryName);
if (platformLibResourcePath != null){
libResourcePath = platformLibResourcePath;
}
}

try (InputStream in = CRT.class.getResourceAsStream(libResourcePath)) {
if (in == null) {
throw new IOException("Unable to open library in jar for AWS CRT: " + libResourcePath);
Expand Down Expand Up @@ -406,6 +415,7 @@ private static CrtPlatform findPlatformImpl() {
String.format("software.amazon.awssdk.crt.test.%s.CrtPlatformImpl", getOSIdentifier()),
// Search for android test impl specifically because getOSIdentifier will return "linux" on android
"software.amazon.awssdk.crt.test.android.CrtPlatformImpl",
"software.amazon.awssdk.crt.android.CrtPlatformImpl",
// Fall back to crt
String.format("software.amazon.awssdk.crt.%s.CrtPlatformImpl", getOSIdentifier()), };
for (String platformImpl : platforms) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/software/amazon/awssdk/crt/CrtPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public String getArchIdentifier() {
return System.getProperty("os.arch");
}

// Get the library resource path to load the JNI library
public String getResourcePath(String cRuntime, String libraryName) throws RuntimeException {
return null;
}

// Called one and only one time during setup for testing
public void setupOnce() {}

Expand Down

0 comments on commit d96eb08

Please sign in to comment.