【问题标题】:Unable to start activity from extended activity given in jar无法从 jar 中给出的扩展活动开始活动
【发布时间】:2015-07-07 19:53:48
【问题描述】:

重新启动我的旧项目时,我遇到了奇怪的问题。

我有两个项目:

  • 提供扩展标准 Activity 的类的库。
  • 应使用上述类作为启动活动的应用程序。

当我尝试启动应用程序时遇到错误:

java.lang.RuntimeException: 
Unable to instantiate activity ComponentInfo{org.bsoftware.catengine.example/org.bsoftware.catengine.example.MainActivity}: 
java.lang.ClassNotFoundException: 
Didn't find class "org.bsoftware.catengine.example.MainActivity" on path: 
DexPathList[[zip file "/data/app/org.bsoftware.catengine.example-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

“只需​​修复清单”cmets 之前的信息很少 :)

  • 当它是一个 android 应用程序(一个项目)时,一切正常。

  • 当 MainActivity 从提供的 jar 文件扩展 CatEngineActivity 类(它是扩展 Activity 的抽象类)时应用程序崩溃。

  • 当 MainActivity 从标准 android lib 扩展 Activity 时,应用程序不会崩溃。

您知道在这种情况下出了什么问题吗?

库项目 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.bsoftware.catengine</groupId>
    <artifactId>cat-engine-core</artifactId>
    <version>0.1.0</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>4.1.1.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

库项目清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="eu.catengine.activity"
    android:versionCode="1"
    android:versionName="1.0.0" >
</manifest>

应用 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>eu.catengine</groupId>
    <artifactId>game-example</artifactId>
    <version>0.1.0</version>

    <properties>
        <!-- use UTF-8 for everything -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>4.1.1.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.bsoftware.catengine</groupId>
            <artifactId>cat-engine-core</artifactId>
            <version>0.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.9.0-rc.1</version>
                <configuration>
                    <sdk>
                        <platform>19</platform>
                    </sdk>
                    <deleteConflictingFiles>true</deleteConflictingFiles>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

应用清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.bsoftware.catengine.example"
    android:versionCode="1"
    android:versionName="1.0.0" >

    <application android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我也试过完整的包路径:

android:name="org.bsoftware.catengine.example.MainActivity"

可在此处找到概念证明: https://github.com/mbienkowski/problems/tree/feature/001

【问题讨论】:

    标签: java android maven runtimeexception library-project


    【解决方案1】:

    能否提供 MainActivity 和 CatEngineActivity 中的代码?

    我怀疑 CatEngineActivity 使用 xml 布局文件作为 setContentView()?如果是这样,那么原因是因为你的jar文件中没有打包android资源。您必须将 CatEngineActivity 打包为 AAR 文件,该文件将包含您的 android 资源。

    如果是这样,请在 AAR 上试用此入门工具包,看看是否有帮助。您可以忽略上传到 bintray。

    https://github.com/jimcoven/android-bintray-kit

    【讨论】:

    • 在我的情况下 setContentView() 不使用 xml 文件,它使用 GLSurfaceView 来渲染 OpenGL 视图。我现在不在家。我稍后会添加一些代码:)
    • 抱歉让您久等了。我刚刚编辑了第一篇文章并添加了指向 github 的小项目链接。
    • 我分叉了你的仓库,并修复了它。该库存在一些命名空间问题,并且在至少为其提供了受保护的访问修饰符后,正确调用了 loadCreate 方法。看看github.com/jimcoven/problems。只需使用 AndroidStudio 打开解决方案项目。
    • 遗憾的是,它对我不起作用。我在运行时仍然遇到同样的错误。无论如何,我有一个临时解决方案,即将我的 lib 项目打包为 libapk。对于每个人:只需 google apklib maven 示例,您就会得到它;)
    猜你喜欢
    • 1970-01-01
    • 2018-03-12
    • 2023-04-03
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多