Skip to content
This repository was archived by the owner on Apr 19, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import static com.android.SdkConstants.androidCmdName

interface AndroidCommand {
int update(String filter);
String list(String filter);
String listAll(String filter);
String listUpdates(String filter);

static final class Real implements AndroidCommand {
final Logger log = Logging.getLogger Real
Expand Down Expand Up @@ -45,10 +46,18 @@ interface AndroidCommand {
return process.waitFor()
}

@Override String list(String filter) {
@Override String listAll(String filter) {
listWithOptions(filter, ['-a', '-e'])
}

@Override String listUpdates(String filter) {
listWithOptions(filter, ['-e'])
}

String listWithOptions(String filter, def options) {
// -a == all
// -e == extended
def cmd = generateCommand('list', ['-a', '-e'])
def cmd = generateCommand('list', options)
def process = new ProcessBuilder(cmd)
.redirectErrorStream(true)
.start()
Expand All @@ -66,7 +75,7 @@ interface AndroidCommand {

def result = ''
output.split('----------').each {
if (it.contains(filter)) {
if (it.contains('"' + filter + '"')) {
result += it
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import static com.android.SdkConstants.FD_PLATFORMS
import static com.android.SdkConstants.FD_ADDONS
import static com.android.SdkConstants.FD_PLATFORM_TOOLS
import static com.android.SdkConstants.FD_SYSTEM_IMAGES
import static com.android.SdkConstants.FD_TOOLS

class PackageResolver {
static void resolve(Project project, File sdk) {
Expand All @@ -32,6 +33,7 @@ class PackageResolver {
final Logger log = Logging.getLogger PackageResolver
final Project project
final File sdk
final File toolsDir
final File buildToolsDir
final File platformToolsDir
final File platformsDir
Expand All @@ -45,6 +47,7 @@ class PackageResolver {
this.project = project
this.androidCommand = androidCommand

toolsDir = new File(sdk, FD_TOOLS)
buildToolsDir = new File(sdk, FD_BUILD_TOOLS)
platformToolsDir = new File(sdk, FD_PLATFORM_TOOLS)
platformsDir = new File(sdk, FD_PLATFORMS)
Expand All @@ -58,14 +61,45 @@ class PackageResolver {
}

def resolve() {
resolveBuildTools()
resolvePlatformTools()
resolveSdkTools() // These must come after platform tools as they depend on them.
resolveBuildTools()
resolveCompileVersion()
resolveSupportLibraryRepository()
resolvePlayServiceRepository()
resolveEmulator()
}

def resolveSdkTools() {
log.debug "Determining installed SDK tools version..."
def installedToolsRevision = getPackageRevision(toolsDir)

log.debug "Determining latest available SDK tools version..."
def toolsInfo = androidCommand.listUpdates 'tools'
def match = (toolsInfo =~ /Android SDK Tools, revision ([\d\.]+)/)
if (match) {
def latestToolsRevision = match[0][1]
log.debug "Latest available SDK tools version is $latestToolsRevision."

installedToolsRevision = installedToolsRevision.replace('.', '') as int
latestToolsRevision = latestToolsRevision.replace('.', '') as int
if (installedToolsRevision >= latestToolsRevision) {
log.lifecycle "Installed SDK tools are up-to-date."
return
}
} else {
log.lifecycle "No SDK tools available for update."
return
}

log.lifecycle "Updating SDK tools to latest version..."

def code = androidCommand.update "tools"
if (code != 0) {
throw new StopExecutionException("SDK tools download failed with code $code.")
}
}

def resolveBuildTools() {
def buildToolsRevision = project.android.buildToolsRevision
log.debug "Build tools version: $buildToolsRevision"
Expand Down Expand Up @@ -222,23 +256,10 @@ class PackageResolver {
needsDownload = true
log.lifecycle "Emulator $emulatorVersion $emulatorArchitecture missing. Downloading..."
} else {
def emulatorPropertiesFile = new File(emulatorDir, 'source.properties')
if (!emulatorPropertiesFile.canRead()) {
emulatorPropertiesFile = new File(alternativeEmulatorDir, 'source.properties')
if (!emulatorPropertiesFile.canRead()) {
throw new StopExecutionException('Could not read ' + emulatorPropertiesFile.absolutePath)
}
}

def emulatorProperties = new Properties()
emulatorProperties.load(new FileInputStream(emulatorPropertiesFile))
def emulatorRevision = emulatorProperties.getProperty('Pkg.Revision')
if (emulatorRevision == null) {
throw new StopExecutionException('Could not get the installed emulator revision for ' +
emulatorPackage)
}
log.debug "Determining installed emulator package version..."
def emulatorRevision = getPackageRevision(emulatorDir, alternativeEmulatorDir)

def currentEmulatorInfo = androidCommand.list emulatorPackage
def currentEmulatorInfo = androidCommand.listAll emulatorPackage
if (currentEmulatorInfo == null || currentEmulatorInfo.isEmpty()) {
throw new StopExecutionException('Could not get the current emulator revision for ' +
emulatorPackage)
Expand All @@ -265,6 +286,25 @@ class PackageResolver {
}
}

def getPackageRevision(File[] packageDirs) {
// Find the first readable source.properties file in the given directories.
def propertiesFileName = 'source.properties'
def propertiesFile = packageDirs.collect { new File(it, propertiesFileName) }.find { it.canRead() }
if (propertiesFile == null) {
throw new StopExecutionException("No readable $propertiesFileName found in any directory of $packageDirs.")
}

def properties = new Properties()
properties.load(new FileInputStream(propertiesFile))
def revision = properties.getProperty('Pkg.Revision')
if (revision == null) {
throw new StopExecutionException("Could not read the revision from ${propertiesFile.absolutePath}")
}

log.debug "Found revision $revision in ${propertiesFile.absolutePath}"
return revision
}

def findDependenciesWithGroup(String group) {
def deps = []
for (Configuration configuration : project.configurations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum SdkDownload {
}
}

final static SDK_VERSION_MAJOR = "24.4.1";
final static SDK_VERSION = "24.4.1";

final Logger log = Logging.getLogger SdkDownload
final String suffix
Expand All @@ -45,7 +45,7 @@ enum SdkDownload {

/** Download the SDK to {@code temp} and extract to {@code dest}. */
void download(File dest) {
def url = "http://dl.google.com/android/android-sdk_r$SDK_VERSION_MAJOR-$suffix.$ext"
def url = "http://dl.google.com/android/android-sdk_r$SDK_VERSION-$suffix.$ext"
log.debug "Downloading SDK from $url."

File temp = new File(dest.getParentFile(), 'android-sdk.temp')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class SdkResolver {
}

def downloadSdk(File target) {
log.lifecycle 'Android SDK not found. Downloading...'
log.lifecycle "Android SDK not found. Downloading version ${SdkDownload.SDK_VERSION}..."

// Download the SDK zip and extract it.
downloader.download target
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>

<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="com.example.sdkmanager"
/>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>

<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="com.example.sdkmanager"
/>
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ class PackageResolverTest {
packageResolver = new PackageResolver(project, fixture.sdk, androidCommand)
}

@FixtureName("outdated-sdk-tools")
@Test public void outdatedSdkToolsDownloaded() {
packageResolver.resolveSdkTools()
assertThat(androidCommand).containsExactly('list -e', 'update tools')
}

@FixtureName("up-to-date-sdk-tools")
@Test public void upToDateSdkToolsRecognized() {
packageResolver.resolveSdkTools()
assertThat(androidCommand).containsExactly('list -e')
}

@FixtureName("up-to-date-build-tools")
@Test public void upToDateBuildToolsRecognized() {
project.apply plugin: 'com.android.application'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ final class RecordingAndroidCommand extends ArrayList<String> implements Android
return nextReturnCode
}

@Override String list(String filter) {
@Override String listAll(String filter) {
add("list -a -e" as String)
return "id: 55 or \"sys-img-armeabi-v7a-android-19\"\n" +
" Type: SystemImage\n" +
" Desc: Android SDK Platform 4.4.2\n" +
" Revision 2\n" +
" Requires SDK Platform Android API 19\n"
}

@Override String listUpdates(String filter) {
add("list -e" as String)
return "id: 1 or \"tools\"\n" +
" Type: Tool\n" +
" Desc: Android SDK Tools, revision 24.4.1\n"
}
}