使用Android Studio修补程序支持库

时间:2013-12-09 15:16:58

标签: android android-studio android-support-library

我一个月前将我的项目移到了Android Studio,我很高兴我做到了,尽管需要切换到一个新的(更强大的)构建系统(gradle)。我在Eclipse中已经知道的一件事,但我现在无法弄清楚如何实现,是修补支持库。我知道这听起来不是一个好习惯,但是一些代码行让我发疯,解决方案就是简单地修改它来解决我的问题。
我试图修改sdk的“。\ extras \ android \ m2repository \ com \ android \ support”目录中的代码,但这似乎不会影响真正用于编译的代码。
有关如何实现这一目标的任何想法吗?

修改

我尝试在我的项目中创建一个模块“SupportLibraryV4”,这就是当我尝试构建它时gradle告诉我的内容:

Error Code:
    1
Output:

    trouble processing "java/android/support/v4/R$anim.class":

    Ill-advised or mistaken usage of a core class (java.* or javax.*)
    when not building a core library.

    This is often due to inadvertently including a core library file
    in your application's project, when using an IDE (such as
    Eclipse). If you are sure you're not intentionally defining a
    core class, then this is the most likely explanation of what's
    going on.

    However, you might actually be trying to define a class in a core
    namespace, the source of which you may have taken, for example,
    from a non-Android virtual machine project. This will most
    assuredly not work. At a minimum, it jeopardizes the
    compatibility of your app with future versions of the platform.
    It is also often of questionable legality.

    If you really intend to build a core library -- which is only
    appropriate as part of creating a full virtual machine
    distribution, as opposed to compiling an application -- then use
    the "--core-library" option to suppress this error message.

    If you go ahead and use "--core-library" but are in fact
    building an application, then be forewarned that your application
    will still fail to build or run, at some point. Please be
    prepared for angry customers who find, for example, that your
    application ceases to function once they upgrade their operating
    system. You will be to blame for this problem.

    If you are legitimately using some code that happens to be in a
    core package, then the easiest safe alternative you have is to
    repackage that code. That is, move the classes in question into
    your own package namespace. This means that they will never be in
    conflict with core system classes. JarJar is a tool that may help
    you in this endeavor. If you find that you cannot do this, then
    that is an indication that the path you are on will ultimately
    lead to pain, suffering, grief, and lamentation.

    1 error; aborting

令人印象深刻!

4 个答案:

答案 0 :(得分:3)

Android sdk没有构建支持库所需的所有文件。 您需要从https://android.googlesource.com签出其他存储库:

  • platform / frameworks / support
  • platform / prebuilts / gradle-plugin
  • 平台/ prebuilts / MAVEN_REPO /机器人
  • 平台/ prebuilts / SDK
  • 平台/ prebuilts /工具

请保持目录结构与android存储库一样。

现在您可以更改支持库中的任何代码。如果您需要更改api v.4的支持库,请在“platform \ frameworks \ support \ v4”中执行此操作。对于构建修补版本的支持库,请使用gradle with next命令:

platform\frameworks\support\v4\gradle clean jar

可以在“platform \ out \ host \ gradle \ frameworks \ support \ v4 \ libs \”中找到已解决的jar。将它放到项目的libs文件夹中,并添加到build.gradle文件中。

答案 1 :(得分:1)

使用捆绑的gradle包装器而不是系统的gradle安装更新了Linux和OS X的答案2016:

https://android.googlesource.com签出以下存储库并保留目录结构:

  • platform / frameworks / support
  • platform / prebuilts / gradle-plugin
  • 平台/ prebuilts / MAVEN_REPO /机器人
  • 平台/ prebuilts / SDK
  • 平台/ prebuilts /工具
  • 平台/工具/外部/ gradle这个

修改库中的文件:

  • 更改platform/frameworks/support/
  • 中的文件

构建AAR

  • cd platform/frameworks/support
  • ./gradlew jarRelease
  • 生成的.aar位于platform/out/host/gradle/frameworks/support/<module>/build/outputs/aar/

添加到项目

  • 在应用的build.gradle
  • 旁边创建一个libs/文件夹
  • 将libs文件夹添加到build.gradle:repositories{ flatDir{ dirs 'libs' } }
  • 将.aar文件复制到libs/
  • 将aar添加到build.gradle中的依赖项部分,例如:dependencies { compile(name:'my_custom_supportlib_module', ext:'aar') }

模块已在项目中

当您修补其他模块所依赖的支持库模块时,您将在构建中使用它两次导致错误。这可以通过排除原始依赖关系来避免。

例如,您可以修补recyclerview-v7并添加

dependencies {
    compile(name:'recyclerview-v7-release', ext:'aar')
}

你必须像这样排除依赖。变化

compile "com.android.support:design:24.2.1"

compile("com.android.support:design:24.2.1") {
    exclude group: 'com.android.support', module: 'recyclerview-v7'
}

适用于所有依赖修补模块的模块。

答案 2 :(得分:0)

修补SupportLib并将其手动添加为jar:

  • 将SupportLib jar放入libs文件夹
  • 右键单击它并点击“添加为库”
  • 确保编译文件('libs / supportlib.jar')在build.gradle文件中
  • 做一个干净的构建

免责声明:Android Studio: Add jar as library?

答案 3 :(得分:0)

事实证明,Ilya Tretyakov的答案仅适用于没有资源的支持库部分,因为它们无法放入char

构建设计支持库的正确方法如下:

https://android.googlesource.com签出这些回购并保留文件结构:

  • 平台/框架/支撑
  • 平台/ prebuilts / gradle这个-插件
  • 平台/ prebuilts / MAVEN_REPO /机器人
  • 平台/ prebuilts / SDK
  • 平台/ prebuilts /工具

导航到平台/框架/支持/设计并编辑您想要的任何文件。现在使用.jar

重建所有内容

您可以在gradle clean assembleRelease

中找到生成的库文件support-design-release.aar

在项目中创建platform/out/host/gradle/frameworks/support/support-design/build/outputs/aar文件夹,然后修改app/libs

app/build.gradle

对项目进行干净的重建,一切都按预期工作