当前位置: 首页 > 工具软件 > Scala Android > 使用案例 >

使用scala编写Android项目

姬阳曜
2023-12-01

开发环境

Idea 10.5

Maven 3

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>com.example</groupId>
    <artifactId>scala</artifactId>
    <version>1.0</version>
    <packaging>apk</packaging>
    <name>scala</name>
    <repositories>
        <repository>
            <id>scala-tools.org</id>
            <name>Scala-tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>scala-tools.org</id>
            <name>Scala-tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </pluginRepository>
    </pluginRepositories>
    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>2.2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android-test</artifactId>
            <version>2.2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.8.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>2.13.1</version>
                <executions>
                    <execution>
                        <id>scala-compile-first</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>scala-test-compile</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.pyx4me</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <version>2.0.4</version>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>proguard</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <maxMemory>256m</maxMemory>
                    <injar>android-classes</injar>
                    <libs>
                        <lib>${rt.jar.path}</lib>
                    </libs>
                    <obfuscate>false</obfuscate>
                    <options>
                        <option>-keep public class * extends android.app.Activity</option>
                        <option>-keep public class * extends android.app.Application</option>
                        <option>-keep public class * extends android.app.Service</option>
                        <option>-keep public class * extends android.content.BroadcastReceiver</option>
                        <option>-keep public class * extends android.content.ContentProvider</option>
                        <option>-dontskipnonpubliclibraryclasses</option>
                        <option>-dontoptimize</option>
                        <option>-printmapping map.txt</option>
                        <option>-printseeds seed.txt</option>
                        <option>-ignorewarnings</option>
                    </options>
                </configuration>
            </plugin>

            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>maven-android-plugin</artifactId>
                <version>2.8.4</version>
                <configuration>
                    <sdk>
                        <platform>8</platform>
                    </sdk>
                    <emulator>
                        <avd>Android2.2</avd>
                    </emulator>
                    <deleteConflictingFiles>true</deleteConflictingFiles>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3</version>
            </plugin>
        </plugins>
    </build>

         <properties>
                <rt.jar.path>${env.JAVA_HOME}/jre/lib/rt.jar</rt.jar.path>
                <jsse.jar.path>${env.JAVA_HOME}/jre/lib/jsse.jar</jsse.jar.path>
            </properties>

</project>


如果遇到以下问题

[ERROR] Failed to execute goal com.jayway.maven.plugins.android.generation2:maven-android-plugin:2.8.4:dex (default-dex) on project scala: MojoExecutionException: ANDROID-040-001: Could not execute: Command = cmd.exe /X /C "D:\Java\androidSdk\platform-tools\dx.bat --dex --output=D:\Java\IDEA\Project\scalatest\scala\target\classes.dex D:\Java\IDEA\Project\scalatest\scala\target\android-classes", Result = 2 -> [Help 1]

是因为pom.xml文件中缺少proguard组件(shift!为啥一定要混淆。。不混淆就不能用啦)

 类似资料: