【发布时间】:2015-06-15 06:59:33
【问题描述】:
最近我正在尝试使用 android studio 而不是 eclipse,但有一件事我无法解决。
当我尝试将以下类作为 jar 导入时,会出现以下错误
班级:
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.ResourceId;
import com.google.api.services.youtube.model.SearchListResponse;
import com.google.api.services.youtube.model.SearchResult;
public class Search {
private static final long NUMBER_OF_VIDEOS_RETURNED = 25;
private static YouTube youtube;
public static void main(String args[]){
}
public static HashMap<String, String> search(String queryTerm, String apiKey) {
try {
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("youtube-cmdline-search-sample").build();
YouTube.Search.List search = youtube.search().list("id,snippet");
search.setKey(apiKey);
search.setQ(queryTerm);
search.setType("video");
search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
SearchListResponse searchResponse = search.execute();
List<SearchResult> searchResultList = searchResponse.getItems();
if (searchResultList != null) {
return prettyPrint(searchResultList.iterator(), queryTerm);
}
} catch (GoogleJsonResponseException e) {
System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
} catch (IOException e) {
System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
private static HashMap<String, String> prettyPrint(Iterator<SearchResult> iteratorSearchResults, String query) {
HashMap<String, String> m = new HashMap<String, String>();
if (!iteratorSearchResults.hasNext()) {
System.out.println(" There aren't any results for your query.");
}
while (iteratorSearchResults.hasNext()) {
SearchResult singleVideo = iteratorSearchResults.next();
ResourceId rId = singleVideo.getId();
if (rId.getKind().equals("youtube#video")) {
//Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().getDefault(); image url
m.put(singleVideo.getSnippet().getTitle() ,rId.getVideoId());
}
}
return m;
}
}
错误:
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:preDexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_05\bin\java.exe'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
堆栈跟踪:
FAILURE: Build failed with an exception.
* What went wrong:
Task 'compileDebug' is ambiguous in root project 'PROJECT_NAME'. Candidates are: 'compileDebugAidl', 'compileDebugAndroidT
estAidl', 'compileDebugAndroidTestJava', 'compileDebugAndroidTestNdk', 'compileDebugAndroidTestRenderscript', 'compileDebugAndr
oidTestSources', 'compileDebugJava', 'compileDebugNdk', 'compileDebugRenderscript', 'compileDebugSources', 'compileDebugUnitTes
tJava', 'compileDebugUnitTestSources'.
* Try:
Run gradlew tasks to get a list of available tasks. Run with --info or --debug option to get more log output.
现在,我尝试运行 stacktrace 和调试,但没有告诉我这个特定 jar 导致错误的原因。当我从 lib 文件夹和 gradle 中删除 jar 并导入其他 jar 时,它们可以正常工作。
我尝试将 jar 导入新项目并将该项目导出到新 jar,但即使之后它仍然给我同样的错误
【问题讨论】:
-
1) 看起来像纯 Java 代码。 Android 不使用
main函数或System.out流。只提取search方法,这可能是您唯一需要的方法。 2) 也许你应该使用 Java 1.7.0 SDK 来编译。 Android 不支持 Java 8。 -
Task 'compileDebug' is ambiguous in root project您只能将 android 应用程序与 android 库混合使用。在这个库模块的顶部可能有apply plugin: 'java'这样的东西,你必须用apply plugin: 'com.android.library'+ 替换它。
标签: java android jar android-studio android-gradle-plugin