开发者

Android library: resources are merged, code is not

开发者 https://www.devze.com 2023-01-03 19:43 出处:网络
I\'m trying to set up an Android application with an Android library project dependence in Eclipse. I\'ve set up a project called AnTest and a library project called AnLib. I\'ve provided a reference

I'm trying to set up an Android application with an Android library project dependence in Eclipse. I've set up a project called AnTest and a library project called AnLib. I've provided a reference to AnLib in the project properties of AnTest. Now I'm trying to refer to some library code (a static public method) from AnTest. Eclipse throws "com.anlib.Hello cannot be resolved to a type".

And it's not that the reference is dangling - I can see the resources properly merged. But from within AnTest, the only class in package com.anlib is "R".

Please, what am I doing wrong? Some very basic step must be missing...

开发者_如何学编程

EDIT: I've added AnLib to the Java build path/Projects in AnTest project properties. Now it compiles, but running throws a NoClassDefFoundError exception...


After carefully looking at the sample projects (TicTacToe), I've noticed a little something under classpath. It's a linked folder to the library sources. Project properties of the app, Java build path, under "Source" click "Link source", provide a path to the library\src.

There's no magic after all.

EDIT: the NDK (JNI-based, native) libraries are not picked up by default, either. Gotta establish a linked folder called "libs" to the respective libs in the library project.

EDIT2: the behavior of native libraries seems to depend on the version of ADT and/or Eclipse. Also assets are not merged, but this latter bit is actually documented.

EDIT3: under the latest Android tools, libraries are not implemented as linked sources anymore. Please disregard this answer; it was true and relevant at the time, but now it's not.


I have this set-up, but without resources in the "library" project. My main project is an Android project, but the "library" one is just a standard Java project. This seems to work well.

Under the build path ("Projects" tab) for the main Android project I have added the library project.

The library project's .project file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>DataExchange</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.wst.common.project.facet.core.builder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.wst.validation.validationbuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
        <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
    </natures>
</projectDescription>

The Android project's .project file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>Project</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.ApkBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
    </natures>
</projectDescription>

I don't remember doing anything special beyond that in setting this up.

0

精彩评论

暂无评论...
验证码 换一张
取 消