将AppCompat和GooglePlayServices库添加到Ant项目

时间:2016-12-22 15:47:55

标签: android ant google-play-services android-appcompat

我们有Android项目,应该通过添加Android-support-v7-AppCompat和Google-Play-Services_lib库进行升级。 我们将它们添加到Ant脚本中:

<property name="android-support-v7-appcompat-folder" value="../android-support-v7-appcompat"/>
<property name="android-support-v7-appcompat-src-folder" value="${android-support-v7-appcompat-folder}/src"/>
<property name="android-support-v7-appcompat-resource-folder" value="${android-support-v7-appcompat-folder}/res"/>
<property name="android-support-v7-appcompat-libs-folder" value="${android-support-v7-appcompat-folder}/libs"/>

<property name="google-play-services-folder" value="../google-play-services_lib"/>
<property name="google-play-services-src-folder" value="${google-play-services-folder}/src"/>
<property name="google-play-services-resource-folder" value="${google-play-services-folder}/res"/>
<property name="google-play-services-libs-folder" value="${google-play-services-folder}/libs"/>

<target name="compile" depends="resource-src, aidl">
        <path id="project.javac.classpath">
            <path refid="project.all.jars.path" />
            <path refid="tested.project.classpath" />
        </path>
    <javac encoding="ascii" target="1.7" source="1.7"
            debug="true" extdirs="" includeantruntime="false"
            destdir="${out-classes}"
            bootclasspath="${android.jar}">
        <src path="${source-folder}" />
        <src path="${kxml-folder}" />
        <src path="${jzlib-folder}" />
        <src path="${bouncycastle-folder}" />
        <src path="${gen-folder}" />
        <src path="${android-support-v7-appcompat-src-folder}" />
        <src path="${google-play-services-src-folder}" />
        <classpath>
            <fileset dir="${android-support-v7-appcompat-libs-folder}" includes="*.jar"/>
            <fileset dir="${google-play-services-libs-folder}" includes="*.jar"/>
            <fileset dir="${external-libs-folder}" includes="*.jar"/>
            <fileset dir="${native-libs-folder}" includes="**/*.jar"/>
            <pathelement path="${main-out-classes}"/>
        </classpath>
     </javac>
</target>

<target name="dex" depends="compile">
    <apply executable="${sdk.buildTools.dir}\dx.bat" failonerror="true" parallel="true">
        <arg value="--dex" />
        <arg value="--output=${intermediate-dex-location}" />
        <arg path="${out-classes-location}" />
        <fileset dir="${android-support-v7-appcompat-libs-folder}" includes="*.jar"/>
        <fileset dir="${google-play-services-libs-folder}" includes="*.jar"/>
        <fileset dir="${external-libs-folder}" includes="*.jar"/>
        <fileset dir="${native-libs-folder}" includes="**/*.jar"/>
    </apply>
</target>

和Eclipse项目。我们可以使用Eclipse安装和运行我们的应用程序,没有任何问题。但是由Ant构建的.apk我们可以安装。尝试打开已安装的应用程序后,我们在logcat中遇到异常:

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {} com.my.android/com.my.android.Program:   java.lang.NullPointerException at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)     在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)     在android.app.ActivityThread.access $ 800(ActivityThread.java:135)     在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)     在android.os.Handler.dispatchMessage(Handler.java:102)at   android.os.Looper.loop(Looper.java:136)at   android.app.ActivityThread.main(ActivityThread.java:5001)at   java.lang.reflect.Method.invokeNative(Native Method)at   java.lang.reflect.Method.invoke(Method.java:515)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)at   dalvik.system.NativeStart.main(原生方法)

     

引起:java.lang.NullPointerException at   android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:457)     在   android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)     在   android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:244)     在   android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:114)     在com.my.android.Program.onCreate(Program.java:180)at   android.app.Activity.performCreate(Activity.java:5231)at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)     在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)

在给定的行中我们调用setContentView(splash); 我们检查了存在于断言中的图像文件,并正确初始化了Bitmap和ImageView。

ImageView splash = new ImageView(this);
splash.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));

splash.setBackgroundColor(Color.BLACK);
String splashUri = getSplashScreenUri();
Bitmap splashBitmap = ImageUtils.getImage(splashUri);
splash.setImageBitmap(splashBitmap);
splash.setScaleType(ScaleType.FIT_XY);

DebugLog.log("splashUri is " + splashUri); //correct path
DebugLog.log("splashBitmap is null " + (null == splashBitmap)); //false
DebugLog.log("splashBitmap height " + splashBitmap.getHeight()); //correct height

setContentView(splash); //line 180

Ant脚本我们错过了什么?

1 个答案:

答案 0 :(得分:0)

您可以查看此blog并检查是否遗漏了某些内容。

  

Play库必须包含在您的project.properties文件中,例如android.library.reference.1=../google-play-services_lib   因为它有需要包含的资源。但是,由于Google不再为Play库提供build.xml,因此Ant将无法构建。您可以通过运行build.xml生成Play库中的android update lib-project --path <playlib_path>

通常,您可以通过在活动getWindow().getDecorView()方法中调用super.onCreate()之前添加对onCreate()的调用来解决您的错误。您可以检查here以及之前报告过的fixed bug