java - 无法从 jar 中给出的扩展 Activity 启动 Activity

标签 java android maven runtimeexception library-project

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

我有两个项目:

  • 提供扩展标准 Activity 的类的库。
  • 应使用上述类作为启动 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]]

“只需​​修复 list ”评论之前的信息很少:)

  • 当它是一个 Android 应用程序(一个项目)时,一切都运行良好。

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

  • 当 MainActivity 从标准 Android 库扩展 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>

库项目 list :

<?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>

应用程序 list :

<?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

最佳答案

您能提供MainActivity和CatEngineActivity中的代码吗?

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

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

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

关于java - 无法从 jar 中给出的扩展 Activity 启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31278088/

相关文章:

java - 节点在列表中复制自身

java - 我应该在 Java 8 Streams 中使用共享可变变量更新吗

java - 使用maven创建带有xml文件的jar文件

java - 修改 IntelliJ 为 Maven 项目的 Tomcat 或 Jetty 配置的资源创建符号链接(symbolic link)

maven - 当 IntelliJ 'updates' 成为 maven repo 时实际发生了什么?

java - 在屏幕上随机生成一个圆圈,并使其变为绿色或红色

android - 在 CardView 中显示 TextView

java - Android 应用程序中的 http 响应中缺少 cookie

java - 使用 onPostExecute 推送 MySQL 数据时出错

java - 自 3.1.0 以来,使用 Maven 生成 QueryDSL 类失败