导入谷歌播放-服务lib放到IntelliJ IDEA的12(13)IntelliJ、lib、IDEA

2023-09-11 20:16:25 作者:平凡可贵

我想导入(和使用)的谷歌播放服务库到现有的Andr​​oid项目,但我挣扎着什么必须是一个依赖问题。

I'd like to import (and use) the Google Play Services library into an existing Android project, but I am struggling with what must be a dependency problem.

写的谷歌所有的文件是基于Eclipse的。我们使用的IntelliJ IDEA。

All documentation written by google is based upon Eclipse. We use IntelliJ IDEA.

我知道的东西:

我知道如何导入模块,库中的IntelliJ添加依赖等。 我也知道,根据在谷歌入门指南,你必须添加谷歌的播放-服务-lib放到(Eclipse中)为项目(通过导入现有的code到工作区),并可以在的IntelliJ从现有的添加新的模块,这样做code。这是相同的过程,不同的名称。 我也明白了排序和导出选项卡确实在Eclipse(的IntelliJ确实太)。 I know how to import Modules, Libraries, add Dependencies, etc. in IntelliJ. I also know that according to the getting started guide at Google, you have to add the google-play-services-lib into (Eclipse) as a project (by importing existing code into the workspace), and that you can do this in IntelliJ by adding a new module from existing code. It's the same procedure, different name. I also understand what the Order and Export tab does in Eclipse (IntelliJ does it too).

东西:

在我复制(安全)在谷歌播放services_lib 文件夹从Android SDK我的项目的根。所以,我有/ myprj和/谷歌播放services_lib在同一水平(我也有/ facebooksdk和/ actionbarsherlock处于同一水平),并且进行了导入模块(从现有的来源)。然后点击旁边旁边旁边......(它的工作,这是公认的)。 I copied (for safety) the google-play-services_lib folder from the Android SDK to my project root. So I have /myprj and /google-play-services_lib at the same level (I also have /facebooksdk and /actionbarsherlock at the same level) and proceeded to "Import Module" (from existing source). Then clicked next next next… (it worked, it was recognized).

该项目结构看起来是这样的(名字故意模糊):

The project structure looks like this (name intentionally blurred):

在此正确添加一个模块(你可以在上面的截图中看到)。现在我需要添加依赖所以我主要的项目都知道这一点。我去我的的模块的,并在列表中,选择我的主要的模块的,然后的依赖的标签。我可以看到一些库(乔达时间,Android的查询,GSON等)和两个的的模块的 FacebookSDK 和 actionbarsherlock 。到目前为止,一切都很好。这就是当事情变得怪异。 This correctly added a module (as you can see in the above screenshot). Now I need to add the dependency so my main project knows about this. I go to my modules and in the list, select my main module and then dependencies tab. I can see a few libraries (Joda time, android-query, gson, etc.) and two modules: FacebookSDK and actionbarsherlock. So far so good. And this is when things get weird.

注意:在大多数情况下,我想检查和取消Export复选框,并尝试添加这对sherlockbar模块在同一时间,只是为了测试。我知道这些都是很多组合,但哦。

NOTE: In most cases I tried checking and unchecking the export checkbox and also tried adding this to the sherlockbar module at the same time, just for testing. I know these are a lot of combinations, but oh well.

1)添加一个JAR或目录(知道这是错误的,我试过)。并指出该.jar是内部的谷歌播放模块/库,命名为:谷歌播放services.jar。

1) Add a Jar or directory (knowing this was wrong, I tried). and pointed to the .jar that is inside the google play module /libs, named: google-play-services.jar.

2)添加库(然后将其添加到项目(S))

2) Add a Library (and then adding it to the project(s))

3)添加在模块依赖,(虽然有我的主要项目选择)。这将显示一个弹出这样的:

3) Add the Module Dependency, (while having my main project selected). This shows you a pop-up like this:

甚至寿我可以引用一个PlusClient我得到正确的进口,当我尝试添加SignInButton在我的布局XML,我得到一个错误。

Even tho I can reference a PlusClient and I get the right imports, when I try to add a SignInButton in my layout xml, I get an error.

有一个在粘贴整个XML布局没有意义的,因为它没有什么奇怪的,它只是一个与几个textviews RelativeLayout的。我加入了G +按键是这样的:

There's no point in pasting the whole XML Layout because it doesn't have anything strange, it's just a RelativeLayout with a few textviews. I added the g+ button like this:

  <com.google.android.gms.common.SignInButton
    android:id="@+id/sign_in_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/logo_login"
    android:layout_marginBottom="30dip" />

就像谷歌说。

Just like Google says.

第一个迹象的东西可能是乱序的事实是的IntelliJ警告你该元素是无效这里,但我认为这就是可视化设计,而不是一个真正的问题的限制。

The first sign that something may be out of order is the fact that IntelliJ warnings you that the element is "not valid here", but I assume that's a limitation of the visual designer rather than a real problem.

然而,当我运行该项目,我得到这样的:

However, when I run the project, I get this:

07-30 17:50:44.854: ERROR/ActivityThread(22324): Failed to inflate
    android.view.InflateException: Binary XML file line #22: Error inflating class com.google.android.gms.common.SignInButton
    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
    at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:134)
    at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:262)
    at com.myapp.ui.activities.LoginSignupActivity.onCreate(LoginSignupActivity.java:64)
    at android.app.Activity.performCreate(Activity.java:4465)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
    at android.app.ActivityThread.access$600(ActivityThread.java:132)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4575)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    at android.view.LayoutInflater.createView(LayoutInflater.java:586)
    ... 24 more
    Caused by: java.lang.NoClassDefFoundError: com.google.android.gms.R$drawable
    at com.google.android.gms.internal.aa.b(Unknown Source)
    at com.google.android.gms.internal.aa.a(Unknown Source)
    at com.google.android.gms.common.SignInButton.c(Unknown Source)
    at com.google.android.gms.common.SignInButton.d(Unknown Source)
    at com.google.android.gms.common.SignInButton.setStyle(Unknown Source)
    at com.google.android.gms.common.SignInButton.<init>(Unknown Source)
    at com.google.android.gms.common.SignInButton.<init>(Unknown Source)
    ... 27 more

下面是得到了我的注意行:

Here is the line that gets my attention:

产生的原因:java.lang.NoClassDefFoundError的:com.google.android.gms.R $绘制

下面的下一个例外是:

07-30 17:50:44.870: ERROR/AndroidRuntime(22324): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.ui.activities.LoginSignupActivity}: android.view.InflateException: Binary XML file line #22: Error inflating class com.google.android.gms.common.SignInButton
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
    at android.app.ActivityThread.access$600(ActivityThread.java:132)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4575)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class com.google.android.gms.common.SignInButton
    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
    at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:134)
    at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:262)
    at com.myapp.ui.activities.LoginSignupActivity.onCreate(LoginSignupActivity.java:64)
    at android.app.Activity.performCreate(Activity.java:4465)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
    ... 11 more
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    at android.view.LayoutInflater.createView(LayoutInflater.java:586)
    ... 24 more
    Caused by: java.lang.NoClassDefFoundError: com.google.android.gms.R$drawable
    at com.google.android.gms.internal.aa.b(Unknown Source)
    at com.google.android.gms.internal.aa.a(Unknown Source)
    at com.google.android.gms.common.SignInButton.c(Unknown Source)
    at com.google.android.gms.common.SignInButton.d(Unknown Source)
    at com.google.android.gms.common.SignInButton.setStyle(Unknown Source)
    at com.google.android.gms.common.SignInButton.<init>(Unknown Source)
    at com.google.android.gms.common.SignInButton.<init>(Unknown Source)
    ... 27 more

如果我从布局删除code,它的工作原理。

If I remove the code from the Layout, it works.

我的想法是,这必须与机器人处理R(有时物资跟不上)的方式,因此其可能不能被找到资源的按钮。

My idea is that this has to do with the way Android handles the R(esources) and therefore it may not be finding the resource for the button.

有没有人成功地使用?IntelliJ IDEA的(12+)与ActionbarSherlock和Google+库

Has anybody managed to use: Intellij Idea (12+) with ActionbarSherlock and Google+ Library?

如果是这样,如何​​? :)

If so, how? :)

推荐答案

由于历史的原因,我要复制我做什么,从的此处。非常感谢 @Hesam 谁发现了它。我将投票关闭整个问题。

For historical reasons, I'm going to reproduce what I did, taken from here. Many thanks to @Hesam who found it. I will vote to close the whole question.

注意的:快捷键是OS X和IntelliJ 12和13

Note: the shortcuts are for OS X and Intellij 12 and 13.

请确保您有最新的SDK / ADT工具和播放服务。他们现在再改变每而且通常不是向后兼容。

更新我不得不再次做到这一点,所以这是我做的打磨版本。

UPDATE I had to do this again, so this is the polished version of what I did.

您需要有一个拷贝(而不是直接从SDK文件夹中使用它),因为如果谷歌更新它,事情会在你更新的SDK突破,它的安全保留一份副本(虽然比较繁琐,因为你将有当一个新的版本出来手动更新)。我发现,这是更好地决定何时更新,而不是与神秘的编译错误感到惊讶。

You need to have a copy (as opposed to using it directly from the SDK folder), because if Google updates it, things are going to break when you update your SDK, it's safer to keep a copy (albeit more tedious because you will have to manually update when a new version comes out). I found out that it's better to decide when to update than to be surprised with cryptic compilation errors.

考虑到这一点,复制库旁边的项目。你应该有一个文件夹结构,看起来像:

With that in mind, copy the library alongside your project. You should have a folder structure that looks like:

/事/ yourmainmodule /

/事/ any_other_module /

/something/modules.xml

/something/README.md(如果你使用的GitHub):)

该库位于

Android的SDK/演员/谷歌/ google_play_services / libproject /谷歌播放services_lib

Android的SDK是,无论你存储在SDK,当你下载了它。谷歌改变了这种飘飞,所以只要找到它,它会在那里。

ANDROID-SDK is wherever you stored the SDK when you downloaded it. Google changes this every now and then, so just find it, it will be there.

复制的整个文件夹,使您的项目现在看起来是这样的:

Copy that entire folder so your project now looks like this:

/事/ yourmainmodule /

/事/ any_other_module /

/事/谷歌播放services_lib / &LT; - 这是新的

/something/google-play-services_lib/ <-- THIS IS THE NEW ONE

/something/modules.xml

/something/README.md

好,现在让我们将它导入的IntelliJ-IDEA的正确方法:

Good, now let's import it to IntelliJ-IDEA the right way:

1)选择你的项目/主模块从项目面板(⌘1 ),并点击⌘↓,以打开项目结构窗口或者只是做文件 - >项目结构

1) Select your project/main module from project panel (⌘ 1) and hit ⌘ ↓ in order to open project structure window or just do File -> Project Structure.

2)选择模块从左侧面板中,然后按+键(或⌘ñ),然后选择的导入模块的,并导航到在谷歌播放services_lib 您刚才复制的旁边的项目的模块文件夹。 ( /事/ )。

2) Select Modules from left panel, then hit + button (or ⌘ N) then select "import module" and navigate to the google-play-services_lib that you just copied next to your project's module folder. (/something/).

该向导会询问一系列问题,正好碰到旁边,默认都很好,但看这是怎么回事,明白发生了什么,这是Java,Android的和IntelliJ一个核心概念。实验,如果你不知道什么是每个对话是说(为什么有些人可能是空的)。

The wizard will ask a series of questions, just hit next, defaults are fine, but read what's going on, understand what's happening, this is a core concept in Java, Android and Intellij. Experiment if you don't know what each dialog is saying (or why some may be empty).

大功告成后,谷歌播放项目将被添加到模块的一起与您的项目。

After you're done, the google play Project will be added to Modules alongside with your project.

不要关闭这个窗口(或者,如果你没有再次打开它)。

Don't close this window (or open it again if you did).

3)仍然在的模块的,选择你的项目/模块,然后选择的依赖的从右边的面板(这是一个标签的),最后点击 + 按钮底部(或⌘ñ这将工作,如果你拥有的在此焦点的选项​​卡),然后从弹出的选择的 3模块依赖... 的,然后选择谷歌播放services_lib,你在previous步骤中添加。它应该在那里。如果不是的话,你没有在步骤添加模块2

3) Still in the modules, select your project/module, then select "Dependencies" from right panel (it's a tab) and finally Click + button at the bottom (or ⌘ N which will work if you have the focus in this tab), then from the popup chose "3 Module dependencies..." and select "google-play-services_lib", which you added in the previous step. It should be there. If it's not, you didn't add the module in step 2.

4)最后,再次单击+按钮(或⌘ñ),但是这一次选择 1罐或目录... 的 ,定位到上述路径的/库文件夹,选择谷歌播放services.jar。

4) Finally, Click + button again (or ⌘ N) but this time select "1 jars or directories...", navigate to the "/libs" folder of the above path and select "google-play-services.jar".

5),单击确定,关闭项目结构窗口。

5) Click ok and close project structure window.

如果你这样做,你的主模块应具有相关性,看起来像这样:

If you did this, your main module should have a dependency that looks like this:

另外,还要确保在项目结构的窗口, FACETS 标签中,谷歌播放服务具有库模块检查。它是一个库模块

Also make sure that in the Project Structure window, FACETS Tab, the Google Play Services has the "Library Module" checked. It is a library module.

注意:请确保您没有添加谷歌播放services_lib项目作为库中的库(模块下左图)。如果你有,你可以去库,并简单地将其删除。谷歌播放库不应该出现在库

Note: Make sure you have not added "google-play-services_lib" project as library in "Libraries" (left panel under Modules). If you have, you can go to "Libraries and simply remove it. Google Play Library Should Not Appear In Libraries.

信用卡:本文来自这个计算器回答

Credit: this text comes from this StackOverflow answer