aboutsummaryrefslogtreecommitdiff
path: root/builder/src/main/java/com/android/builder/PlatformSdkParser.java
blob: 3325ea0075ffc24cf1bd28d9f908fa1d43894b3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.builder;

import com.android.SdkConstants;
import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.builder.internal.FakeAndroidTarget;
import com.android.sdklib.BuildToolInfo;
import com.android.sdklib.IAndroidTarget;
import com.android.sdklib.repository.FullRevision;
import com.android.utils.ILogger;
import com.google.common.collect.Lists;

import java.io.File;
import java.util.List;

/**
 * Implementation of {@link SdkParser} for the SDK prebuilds in the Android source tree.
 */
public class PlatformSdkParser implements SdkParser {
    private final String mPlatformRootFolder;

    private boolean mInitialized = false;
    private IAndroidTarget mTarget;
    private BuildToolInfo mBuildToolInfo;

    private File mHostTools;
    private File mZipAlign;
    private File mAdb;

    public PlatformSdkParser(@NonNull String sdkLocation) {
        mPlatformRootFolder = sdkLocation;
    }

    @Override
    public void initParser(@NonNull String target,
                           @NonNull FullRevision buildToolRevision,
                           @NonNull ILogger logger) {
        if (!mInitialized) {
            mTarget = new FakeAndroidTarget(mPlatformRootFolder, target);

            mBuildToolInfo = new BuildToolInfo(buildToolRevision, new File(mPlatformRootFolder),
                    new File(getHostToolsFolder(), SdkConstants.FN_AAPT),
                    new File(getHostToolsFolder(), SdkConstants.FN_AIDL),
                    new File(mPlatformRootFolder, "prebuilts/sdk/tools/dx"),
                    new File(mPlatformRootFolder, "prebuilts/sdk/tools/lib/dx.jar"),
                    new File(getHostToolsFolder(), SdkConstants.FN_RENDERSCRIPT),
                    new File(mPlatformRootFolder, "prebuilts/sdk/renderscript/include"),
                    new File(mPlatformRootFolder, "prebuilts/sdk/renderscript/clang-include"),
                    new File(getHostToolsFolder(), SdkConstants.FN_BCC_COMPAT),
                    new File(getHostToolsFolder(), "arm-linux-androideabi-ld"),
                    new File(getHostToolsFolder(), "i686-linux-android-ld"),
                    new File(getHostToolsFolder(), "mipsel-linux-android-ld"));
            mInitialized = true;
        }
    }

    @NonNull
    @Override
    public IAndroidTarget getTarget() {
        if (!mInitialized) {
            throw new IllegalStateException("SdkParser was not initialized.");
        }
        return mTarget;
    }

    @NonNull
    @Override
    public BuildToolInfo getBuildTools() {
        if (!mInitialized) {
            throw new IllegalStateException("SdkParser was not initialized.");
        }
        return mBuildToolInfo;
    }

    @Override
    @NonNull
    public String getAnnotationsJar() {
        String host;
        if (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_DARWIN) {
            host = "darwin-x86";
        } else if (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_LINUX) {
            host = "linux";
        } else {
            throw new IllegalStateException("Windows is not supported for platform development");
        }

        return mPlatformRootFolder + "/out/host/" + host + "/framework/annotations.jar";
    }

    @Override
    @Nullable
    public FullRevision getPlatformToolsRevision() {
        return new FullRevision(99);
    }

    @Override
    @NonNull
    public File getZipAlign() {
        if (mZipAlign == null) {
            mZipAlign = new File(getHostToolsFolder(), SdkConstants.FN_ZIPALIGN);
        }

        return mZipAlign;
    }

    @Override
    @NonNull
    public File getAdb() {
        if (mAdb == null) {

            if (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_DARWIN) {
                mAdb = new File(mPlatformRootFolder, "out/host/darwin-x86/bin/adb");
            } else if (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_LINUX) {
                mAdb = new File(mPlatformRootFolder, "out/host/linux-x86/bin/adb");
            } else {
                throw new IllegalStateException(
                        "Windows is not supported for platform development");
            }
        }

        return mAdb;
    }

    @NonNull
    @Override
    public List<File> getRepositories() {
        List<File> repositories = Lists.newArrayList();
        repositories.add(new File(mPlatformRootFolder + "/prebuilts/sdk/m2repository"));

        return repositories;
    }

    private File getHostToolsFolder() {
        if (mHostTools == null) {
            File tools = new File(mPlatformRootFolder, "prebuilts/sdk/tools");
            if (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_DARWIN) {
                mHostTools = new File(tools, "darwin");
            } else if (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_LINUX) {
                mHostTools = new File(tools, "linux");
            } else {
                throw new IllegalStateException(
                        "Windows is not supported for platform development");
            }

            if (!mHostTools.isDirectory()) {
                throw new IllegalStateException("Host tools folder missing: " +
                        mHostTools.getAbsolutePath());
            }
        }
        return mHostTools;
    }
}