Skip to content

Commit

Permalink
Refactor AndroidSdk.
Browse files Browse the repository at this point in the history
  • Loading branch information
xian committed Nov 15, 2016
1 parent 0990966 commit cad6ffa
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 53 deletions.
66 changes: 66 additions & 0 deletions buildSrc/src/main/groovy/AndroidSdk.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class AndroidSdk implements Comparable<AndroidSdk> {
static final JELLY_BEAN = new AndroidSdk(16, "4.1.2_r1", 0, "1.6")
static final JELLY_BEAN_MR1 = new AndroidSdk(17, "4.2.2_r1.2", 0, "1.6")
static final JELLY_BEAN_MR2 = new AndroidSdk(18, "4.3_r2", 0, "1.6")
static final KITKAT = new AndroidSdk(19, "4.4_r1", 1, "1.7")
static final LOLLIPOP = new AndroidSdk(21, "5.0.0_r2", 1, "1.7")
static final LOLLIPOP_MR1 = new AndroidSdk(22, "5.1.1_r9", 1, "1.7")
static final M = new AndroidSdk(23, "6.0.0_r1", 0, "1.7")
static final N = new AndroidSdk(24, "7.0.0_r1", 0, "1.8")

private static final double jdkVersion = Double.parseDouble(System.getProperty("java.specification.version"));

static final List<AndroidSdk> ALL_SDKS = [
JELLY_BEAN, JELLY_BEAN_MR1, JELLY_BEAN_MR2, KITKAT,
LOLLIPOP, LOLLIPOP_MR1, M, // N
]
static final SUPPORTED_SDKS = ALL_SDKS.findAll { it.isSupportedOnThisJdk() }
static final MAX_SUPPORTED_SDK = Collections.max(SUPPORTED_SDKS)
static final MAX_SDK = Collections.max(ALL_SDKS)

static {
if (MAX_SUPPORTED_SDK != MAX_SDK) {
println "WARNING: Running with JDK $jdkVersion, max supported Android SDK is $MAX_SUPPORTED_SDK.apiLevel."
}
}

private final int apiLevel
private final String androidVersion
private final String frameworkSdkBuildVersion
private final String minJdkVersion

AndroidSdk(int apiLevel, String androidVersion, int frameworkSdkBuildVersion, String minJdkVersion) {
this.minJdkVersion = minJdkVersion
this.frameworkSdkBuildVersion = frameworkSdkBuildVersion
this.androidVersion = androidVersion
this.apiLevel = apiLevel
}

boolean isSupportedOnThisJdk() {
return jdkVersion >= Double.parseDouble(minJdkVersion)
}

String getCoordinates() {
return "org.robolectric:android-all:${androidVersion}-robolectric-${frameworkSdkBuildVersion}"
}

@Override
int compareTo(AndroidSdk other) {
return apiLevel - other.apiLevel
}

boolean equals(o) {
if (this.is(o)) return true
if (getClass() != o.class) return false

AndroidSdk that = (AndroidSdk) o

if (apiLevel != that.apiLevel) return false

return true
}

int hashCode() {
return apiLevel
}
}
13 changes: 0 additions & 13 deletions buildSrc/src/main/groovy/android-sdk.groovy

This file was deleted.

2 changes: 1 addition & 1 deletion robolectric-annotations/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dependencies {
// Compile dependencies
compileOnly "com.intellij:annotations:12.0"
compileOnly "org.robolectric:android-all:6.0.0_r1-robolectric-0"
compileOnly AndroidSdk.MAX_SDK.coordinates
}
4 changes: 2 additions & 2 deletions robolectric-resources/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ dependencies {
// Compile dependencies
compile "com.ximpleware:vtd-xml:2.11"
compile "com.google.guava:guava:19.0"
compileOnly "org.robolectric:android-all:6.0.0_r1-robolectric-0"
compileOnly AndroidSdk.MAX_SDK.coordinates
compileOnly "com.intellij:annotations:12.0"

// Testing dependencies
testCompile "junit:junit:4.8.2"
testCompile "org.hamcrest:hamcrest-core:1.3"
testCompile "org.assertj:assertj-core:2.0.0"
testCompile "com.google.testing.compile:compile-testing:0.6"
testRuntime "org.robolectric:android-all:6.0.0_r1-robolectric-0"
testRuntime AndroidSdk.MAX_SDK.coordinates
}
6 changes: 1 addition & 5 deletions robolectric-shadows/shadows-core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
def androidApi = hasProperty("androidApi") ? ext.androidApi.toInteger() : AndroidSdk.latestVersion
def robolectricVersion = AndroidSdk.versions[androidApi]
logger.info "Android API: $androidApi$robolectricVersion"

apply plugin: ShadowsPlugin

shadows {
Expand Down Expand Up @@ -51,7 +47,7 @@ dependencies {
// Compile dependencies
compileOnly "com.intellij:annotations:12.0"
compile "com.almworks.sqlite4java:sqlite4java:0.282"
compileOnly("org.robolectric:android-all:${AndroidSdk.versions[AndroidSdk.latestVersion]}") { force = true }
compileOnly(AndroidSdk.MAX_SDK.coordinates) { force = true }
compile "com.ibm.icu:icu4j:53.1"

jni "com.github.axet.litedb:libsqlite:0.282-3:natives-mac-x86_64"
Expand Down
4 changes: 2 additions & 2 deletions robolectric-shadows/shadows-httpclient/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ dependencies {
// Compile dependencies
earlyRuntime "org.apache.httpcomponents:httpcore:4.0.1"
compile "org.apache.httpcomponents:httpclient:4.0.3"
compileOnly("org.robolectric:android-all:${AndroidSdk.versions[22]}") { force = true }
compileOnly(AndroidSdk.LOLLIPOP_MR1.coordinates) { force = true }

// Testing dependencies
testCompile "junit:junit:4.8.2"
testCompile "org.hamcrest:hamcrest-core:1.3"
testCompile "org.assertj:assertj-core:2.0.0"
testCompile "org.mockito:mockito-core:1.8.0"
testRuntime "org.robolectric:android-all:${AndroidSdk.versions[22]}"
testRuntime AndroidSdk.LOLLIPOP_MR1.coordinates
}

// change local artifact name to match dependencies
Expand Down
4 changes: 2 additions & 2 deletions robolectric-shadows/shadows-maps/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {
compile project(":robolectric")
compile project(":robolectric-shadows/shadows-core")

compileOnly "org.robolectric:android-all:6.0.0_r1-robolectric-0"
compileOnly AndroidSdk.MAX_SDK.coordinates
compile "com.ibm.icu:icu4j:53.1"

compileOnly "com.google.android.maps:maps:23_r1"
Expand All @@ -17,7 +17,7 @@ dependencies {
testCompile "org.hamcrest:hamcrest-core:1.3"
testCompile "org.assertj:assertj-core:2.0.0"
testCompile "org.mockito:mockito-core:1.8.0"
testRuntime "org.robolectric:android-all:6.0.0_r1-robolectric-0"
testRuntime AndroidSdk.MAX_SDK.coordinates
testRuntime "com.google.android.maps:maps:23_r1"
}

Expand Down
2 changes: 1 addition & 1 deletion robolectric-shadows/shadows-multidex/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies {

compileOnly "com.android.support:multidex:1.0.1"

compileOnly "org.robolectric:android-all:6.0.0_r1-robolectric-0"
compileOnly AndroidSdk.MAX_SDK.coordinates
}

// change local artifact name to match dependencies
Expand Down
4 changes: 2 additions & 2 deletions robolectric-shadows/shadows-play-services/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies {
compileOnly "com.google.android.gms:play-services-base:8.4.0"
compileOnly "com.google.android.gms:play-services-basement:8.4.0"

compileOnly "org.robolectric:android-all:6.0.0_r1-robolectric-0"
compileOnly AndroidSdk.MAX_SDK.coordinates

// Testing dependencies
testCompile "junit:junit:4.8.2"
Expand All @@ -23,7 +23,7 @@ dependencies {
testRuntime "com.google.android.gms:play-services-base:8.4.0"
testRuntime "com.google.android.gms:play-services-basement:8.4.0"

testRuntime "org.robolectric:android-all:6.0.0_r1-robolectric-0"
testRuntime AndroidSdk.MAX_SDK.coordinates
}

install {
Expand Down
4 changes: 2 additions & 2 deletions robolectric-shadows/shadows-support-v4/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {
compile project(":robolectric-shadows/shadows-core")

// Compile dependencies
compileOnly "org.robolectric:android-all:6.0.0_r1-robolectric-0"
compileOnly AndroidSdk.MAX_SDK.coordinates
compileOnly "com.android.support:support-v4:23.2.0"
compileOnly "com.android.support:internal_impl:23.2.0"

Expand All @@ -25,7 +25,7 @@ dependencies {
testCompile "org.mockito:mockito-core:1.8.0"

earlyTestRuntime "org.hamcrest:hamcrest-core:1.3"
testRuntime "org.robolectric:android-all:6.0.0_r1-robolectric-0"
testRuntime AndroidSdk.MAX_SDK.coordinates
testRuntime "com.android.support:support-v4:23.2.0"
testRuntime "com.android.support:internal_impl:23.2.0"
}
Expand Down
4 changes: 2 additions & 2 deletions robolectric-utils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dependencies {
// Compile dependencies
compileOnly "junit:junit:4.8.2"
compileOnly "org.hamcrest:hamcrest-core:1.3"
compileOnly "org.robolectric:android-all:6.0.0_r1-robolectric-0"
compileOnly AndroidSdk.MAX_SDK.coordinates

compile "org.ow2.asm:asm:5.0.1"
compile "org.ow2.asm:asm-commons:5.0.1"
Expand All @@ -14,5 +14,5 @@ dependencies {
testCompile "org.assertj:assertj-core:2.0.0"
testCompile "org.hamcrest:hamcrest-core:1.3"
testCompile "org.mockito:mockito-core:1.9.5"
testRuntime "org.robolectric:android-all:6.0.0_r1-robolectric-0"
testRuntime AndroidSdk.MAX_SDK.coordinates
}
4 changes: 2 additions & 2 deletions robolectric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ dependencies {
compile("org.apache.maven:maven-ant-tasks:2.1.3") {
exclude group: "junit", module: "junit"
}
compileOnly "org.robolectric:android-all:6.0.0_r1-robolectric-0"
compileOnly AndroidSdk.MAX_SDK.coordinates
compileOnly "junit:junit:4.8.2"

// Testing dependencies
testCompile "junit:junit:4.8.2"
testCompile "org.hamcrest:hamcrest-core:1.3"
testCompile "org.assertj:assertj-core:2.0.0"
testCompile "org.mockito:mockito-core:1.8.0"
testRuntime "org.robolectric:android-all:6.0.0_r1-robolectric-0"
testRuntime AndroidSdk.MAX_SDK.coordinates
}
47 changes: 28 additions & 19 deletions robolectric/src/main/java/org/robolectric/internal/SdkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,49 @@

import android.os.Build;
import org.robolectric.internal.dependency.DependencyJar;
import org.robolectric.util.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;

public class SdkConfig {
private static final String ROBOLECTRIC_VERSION;
private static final Map<Integer, SdkVersion> SUPPORTED_APIS;
private static final String ROBOLECTRIC_VERSION = getRobolectricVersion();

private static final Map<Integer, SdkVersion> SUPPORTED_APIS = Collections.unmodifiableMap(new HashMap<Integer, SdkVersion>() {
private final double jdkVersion = Double.parseDouble(System.getProperty("java.specification.version"));

{
addSdk(Build.VERSION_CODES.JELLY_BEAN, "4.1.2_r1", "0", "1.6");
addSdk(Build.VERSION_CODES.JELLY_BEAN_MR1, "4.2.2_r1.2", "0", "1.6");
addSdk(Build.VERSION_CODES.JELLY_BEAN_MR2, "4.3_r2", "0", "1.6");
addSdk(Build.VERSION_CODES.KITKAT, "4.4_r1", "1", "1.7");
addSdk(Build.VERSION_CODES.LOLLIPOP, "5.0.0_r2", "1", "1.7");
addSdk(Build.VERSION_CODES.LOLLIPOP_MR1, "5.1.1_r9", "1", "1.7");
addSdk(Build.VERSION_CODES.M, "6.0.0_r1", "0", "1.7");
// addSdk(Build.VERSION_CODES.N, "7.0.0_r1", "0", "1.8");
}

private void addSdk(int sdkVersion, String androidVersion, String frameworkSdkBuildVersion, String minJdkVersion) {
if (jdkVersion >= Double.parseDouble(minJdkVersion)) {
put(sdkVersion, new SdkVersion(androidVersion, frameworkSdkBuildVersion));
} else {
Logger.info("Android SDK %s not supported on JDK %s (it requires %s)", sdkVersion, jdkVersion, minJdkVersion);
}
}
});

public static final int FALLBACK_SDK_VERSION = Build.VERSION_CODES.JELLY_BEAN;
public static final int MAX_SDK_VERSION = Build.VERSION_CODES.M;
public static final int MAX_SDK_VERSION = Collections.max(getSupportedApis());

private final int apiLevel;
private final SdkVersion sdkVersion;

static {
SUPPORTED_APIS = new HashMap<>();
addSdk(Build.VERSION_CODES.JELLY_BEAN, "4.1.2_r1", "0");
addSdk(Build.VERSION_CODES.JELLY_BEAN_MR1, "4.2.2_r1.2", "0");
addSdk(Build.VERSION_CODES.JELLY_BEAN_MR2, "4.3_r2", "0");
addSdk(Build.VERSION_CODES.KITKAT, "4.4_r1", "1");
addSdk(Build.VERSION_CODES.LOLLIPOP, "5.0.0_r2", "1");
addSdk(Build.VERSION_CODES.LOLLIPOP_MR1, "5.1.1_r9", "1");
addSdk(Build.VERSION_CODES.M, "6.0.0_r1", "0");
ROBOLECTRIC_VERSION = getRobolectricVersion();
}

public static void addSdk(int sdkVersion, String androidVersion, String robolectricVersion) {
SUPPORTED_APIS.put(sdkVersion, new SdkVersion(androidVersion, robolectricVersion));
}

public static Set<Integer> getSupportedApis() {
return SUPPORTED_APIS.keySet();
}
Expand Down

0 comments on commit cad6ffa

Please sign in to comment.